mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 17:02:51 +08:00
parent
f561d71e4e
commit
5cf41725a5
@ -0,0 +1,77 @@
|
|||||||
|
using Shouldly;
|
||||||
|
using Spectre.Console.Cli;
|
||||||
|
using Spectre.Console.Testing;
|
||||||
|
using Spectre.Console.Tests.Data;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Spectre.Console.Tests.Unit.Cli
|
||||||
|
{
|
||||||
|
public sealed partial class CommandAppTests
|
||||||
|
{
|
||||||
|
public sealed class Remaining
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Should_Register_Remaining_Parsed_Arguments_With_Context()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var app = new CommandAppFixture();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||||
|
{
|
||||||
|
animal.AddCommand<DogCommand>("dog");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var (result, _, ctx, _) = app.Run(new[]
|
||||||
|
{
|
||||||
|
"animal", "4", "dog", "12", "--",
|
||||||
|
"--foo", "bar", "--foo", "baz",
|
||||||
|
"-bar", "\"baz\"", "qux",
|
||||||
|
"foo bar baz qux",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then
|
||||||
|
ctx.Remaining.Parsed.Count.ShouldBe(4);
|
||||||
|
ctx.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
|
||||||
|
ctx.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
|
||||||
|
ctx.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
|
||||||
|
ctx.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Register_Remaining_Raw_Arguments_With_Context()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var app = new CommandAppFixture();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||||
|
{
|
||||||
|
animal.AddCommand<DogCommand>("dog");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var (result, _, ctx, _) = app.Run(new[]
|
||||||
|
{
|
||||||
|
"animal", "4", "dog", "12", "--",
|
||||||
|
"--foo", "bar", "-bar", "\"baz\"", "qux",
|
||||||
|
"foo bar baz qux",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then
|
||||||
|
ctx.Remaining.Raw.Count.ShouldBe(6);
|
||||||
|
ctx.Remaining.Raw[0].ShouldBe("--foo");
|
||||||
|
ctx.Remaining.Raw[1].ShouldBe("bar");
|
||||||
|
ctx.Remaining.Raw[2].ShouldBe("-bar");
|
||||||
|
ctx.Remaining.Raw[3].ShouldBe("baz");
|
||||||
|
ctx.Remaining.Raw[4].ShouldBe("qux");
|
||||||
|
ctx.Remaining.Raw[5].ShouldBe("foo bar baz qux");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -74,6 +74,9 @@ namespace Spectre.Console.Cli.Internal
|
|||||||
tokens.Add(ScanString(context, reader));
|
tokens.Add(ScanString(context, reader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush remaining tokens
|
||||||
|
context.FlushRemaining();
|
||||||
}
|
}
|
||||||
|
|
||||||
return position;
|
return position;
|
||||||
@ -145,7 +148,6 @@ namespace Spectre.Console.Cli.Internal
|
|||||||
|
|
||||||
// Add to the context
|
// Add to the context
|
||||||
context.AddRemaining(quotedString);
|
context.AddRemaining(quotedString);
|
||||||
context.FlushRemaining();
|
|
||||||
|
|
||||||
return new CommandTreeToken(
|
return new CommandTreeToken(
|
||||||
CommandTreeToken.Kind.String,
|
CommandTreeToken.Kind.String,
|
||||||
|
@ -20,17 +20,10 @@ namespace Spectre.Console.Cli.Internal
|
|||||||
public void AddRemaining(char character)
|
public void AddRemaining(char character)
|
||||||
{
|
{
|
||||||
if (Mode == CommandTreeTokenizer.Mode.Remaining)
|
if (Mode == CommandTreeTokenizer.Mode.Remaining)
|
||||||
{
|
|
||||||
if (char.IsWhiteSpace(character))
|
|
||||||
{
|
|
||||||
FlushRemaining();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
_builder.Append(character);
|
_builder.Append(character);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void AddRemaining(string text)
|
public void AddRemaining(string text)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user