Skip to content

Commit

Permalink
Add token representation to remaining arguments
Browse files Browse the repository at this point in the history
Before, when adding parsed information to the IRemainingArguments.Parsed,
we used the name of the parsed option ('foo') instead of it's
representation ('--foo'). This commit fixes that.
  • Loading branch information
patriksvensson committed Apr 23, 2024
1 parent 95bff47 commit 71f762f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 26 deletions.
14 changes: 5 additions & 9 deletions src/Spectre.Console.Cli/Internal/Parsing/CommandTreeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,7 @@ private void ParseOptionValue(
var valueToken = stream.Peek();
if (valueToken?.TokenKind == CommandTreeToken.Kind.String)
{
var parseValue = true;
if (token.TokenKind == CommandTreeToken.Kind.ShortOption && token.IsGrouped)
{
parseValue = false;
}
bool parseValue = token is not { TokenKind: CommandTreeToken.Kind.ShortOption, IsGrouped: true };

if (context.State == State.Normal && parseValue)
{
Expand All @@ -333,7 +329,7 @@ private void ParseOptionValue(
{
value = stream.Consume(CommandTreeToken.Kind.String)?.Value;

context.AddRemainingArgument(token.Value, value);
context.AddRemainingArgument(token.Representation, value);

// Prevent the option and it's non-boolean value from being added to
// mapped parameters (otherwise an exception will be thrown later
Expand Down Expand Up @@ -364,22 +360,22 @@ private void ParseOptionValue(
// In relaxed parsing mode?
if (context.ParsingMode == ParsingMode.Relaxed)
{
context.AddRemainingArgument(token.Value, value);
context.AddRemainingArgument(token.Representation, value);
}
}
}
}
else
{
context.AddRemainingArgument(token.Value, parseValue ? valueToken.Value : null);
context.AddRemainingArgument(token.Representation, parseValue ? valueToken.Value : null);
}
}
else
{
if (parameter == null && // Only add tokens which have not been matched to a command parameter
(context.State == State.Remaining || context.ParsingMode == ParsingMode.Relaxed))
{
context.AddRemainingArgument(token.Value, null);
context.AddRemainingArgument(token.Representation, null);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static IEnumerable<CommandTreeToken> ScanShortOptions(CommandTreeTokeniz
}

// Encountered a separator?
if (current == '=' || current == ':')
if (current is '=' or ':')
{
break;
}
Expand All @@ -184,7 +184,7 @@ private static IEnumerable<CommandTreeToken> ScanShortOptions(CommandTreeTokeniz
var value = current.ToString(CultureInfo.InvariantCulture);
result.Add(result.Count == 0
? new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position, value, $"-{value}")
: new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position + result.Count, value, value));
: new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position + result.Count, value, $"-{value}"));
}
else if (result.Count == 0 && char.IsDigit(current))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void Should_Be_Unable_To_Parse_Default_Command_Arguments_Relaxed_Parsing(
//cat.Name.ShouldBe("Kitty"); //<-- Should normally be correct, but instead name will be added to the remaining arguments (see below).
});
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("name", values: new[] { "Kitty", });
result.Context.ShouldHaveRemainingArgument("--name", values: new[] { "Kitty", });
}

[Fact]
Expand Down
14 changes: 7 additions & 7 deletions test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Remaining.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Should_Add_Unknown_Flags_To_Remaining_Arguments_RelaxedParsing(strin

// Then
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument(unknownFlag.TrimStart('-'), values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument(unknownFlag, values: new[] { (string)null });
result.Context.Remaining.Raw.Count.ShouldBe(0);
}

Expand All @@ -80,7 +80,7 @@ public void Should_Add_Unknown_Flags_When_Grouped_To_Remaining_Arguments_Relaxed

// Then
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("-r", values: new[] { (string)null });
result.Context.Remaining.Raw.Count.ShouldBe(0);
}

Expand Down Expand Up @@ -189,10 +189,10 @@ public void Should_Register_Remaining_Parsed_Arguments_With_Context()

// Then
result.Context.Remaining.Parsed.Count.ShouldBe(4);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" });
result.Context.ShouldHaveRemainingArgument("b", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("a", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("r", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { "bar", "baz" });
result.Context.ShouldHaveRemainingArgument("-b", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("-a", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("-r", values: new[] { (string)null });
}

[Fact]
Expand Down Expand Up @@ -280,7 +280,7 @@ public void Should_Convert_Flags_To_Remaining_Arguments_If_Cannot_Be_Assigned(bo

// Then
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("good-boy", values: new[] { "Please be good Rufus!" });
result.Context.ShouldHaveRemainingArgument("--good-boy", values: new[] { "Please be good Rufus!" });

result.Context.Remaining.Raw.Count.ShouldBe(0); // nb. there are no "raw" remaining arguments on the command line
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void Should_Execute_Command_Not_Output_Application_Version_To_The_Console

// Then
result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument("version", new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("--version", new[] { (string)null });
}

[Fact]
Expand All @@ -70,7 +70,7 @@ public void Should_Execute_Default_Command_Not_Output_Application_Version_To_The

// Then
result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument("version", new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("--version", new[] { (string)null });
}

[Fact]
Expand Down
10 changes: 5 additions & 5 deletions test/Spectre.Console.Cli.Tests/Unit/CommandAppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void Should_Preserve_Quotes_Hyphen_Delimiters_Spaces()
dog.Name.ShouldBe("\" -Rufus --' ");
});
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("order-by", values: new[] { "\"-size\"", " ", string.Empty });
result.Context.ShouldHaveRemainingArgument("--order-by", values: new[] { "\"-size\"", " ", string.Empty });
}

[Fact]
Expand Down Expand Up @@ -844,7 +844,7 @@ public void Should_Add_Unknown_Option_To_Remaining_Arguments_In_Relaxed_Mode()
// Then
result.Context.ShouldNotBeNull();
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar" });
result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { "bar" });
}

[Fact]
Expand Down Expand Up @@ -875,8 +875,8 @@ public void Should_Add_Unknown_Option_To_Remaining_Arguments_In_Strict_Mode()
// Then
result.Context.ShouldNotBeNull();
result.Context.Remaining.Parsed.Count.ShouldBe(2);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar" });
result.Context.ShouldHaveRemainingArgument("f", values: new[] { "baz" });
result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { "bar" });
result.Context.ShouldHaveRemainingArgument("-f", values: new[] { "baz" });
result.Context.Remaining.Raw.Count.ShouldBe(5);
result.Context.Remaining.Raw.ShouldBe(new[]
{
Expand Down Expand Up @@ -909,7 +909,7 @@ public void Should_Add_Unknown_Boolean_Option_To_Remaining_Arguments_In_Relaxed_
// Then
result.Context.ShouldNotBeNull();
result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { (string)null });
}

[Fact]
Expand Down

0 comments on commit 71f762f

Please sign in to comment.