Add token representation to remaining arguments

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.
This commit is contained in:
Patrik Svensson 2024-04-23 14:04:00 +02:00 committed by Patrik Svensson
parent 95bff47b85
commit 71f762f646
6 changed files with 22 additions and 26 deletions

View File

@ -302,11 +302,7 @@ internal class CommandTreeParser
var valueToken = stream.Peek(); var valueToken = stream.Peek();
if (valueToken?.TokenKind == CommandTreeToken.Kind.String) if (valueToken?.TokenKind == CommandTreeToken.Kind.String)
{ {
var parseValue = true; bool parseValue = token is not { TokenKind: CommandTreeToken.Kind.ShortOption, IsGrouped: true };
if (token.TokenKind == CommandTreeToken.Kind.ShortOption && token.IsGrouped)
{
parseValue = false;
}
if (context.State == State.Normal && parseValue) if (context.State == State.Normal && parseValue)
{ {
@ -333,7 +329,7 @@ internal class CommandTreeParser
{ {
value = stream.Consume(CommandTreeToken.Kind.String)?.Value; 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 // Prevent the option and it's non-boolean value from being added to
// mapped parameters (otherwise an exception will be thrown later // mapped parameters (otherwise an exception will be thrown later
@ -364,14 +360,14 @@ internal class CommandTreeParser
// In relaxed parsing mode? // In relaxed parsing mode?
if (context.ParsingMode == ParsingMode.Relaxed) if (context.ParsingMode == ParsingMode.Relaxed)
{ {
context.AddRemainingArgument(token.Value, value); context.AddRemainingArgument(token.Representation, value);
} }
} }
} }
} }
else else
{ {
context.AddRemainingArgument(token.Value, parseValue ? valueToken.Value : null); context.AddRemainingArgument(token.Representation, parseValue ? valueToken.Value : null);
} }
} }
else else
@ -379,7 +375,7 @@ internal class CommandTreeParser
if (parameter == null && // Only add tokens which have not been matched to a command parameter if (parameter == null && // Only add tokens which have not been matched to a command parameter
(context.State == State.Remaining || context.ParsingMode == ParsingMode.Relaxed)) (context.State == State.Remaining || context.ParsingMode == ParsingMode.Relaxed))
{ {
context.AddRemainingArgument(token.Value, null); context.AddRemainingArgument(token.Representation, null);
} }
} }

View File

@ -171,7 +171,7 @@ internal static class CommandTreeTokenizer
} }
// Encountered a separator? // Encountered a separator?
if (current == '=' || current == ':') if (current is '=' or ':')
{ {
break; break;
} }
@ -184,7 +184,7 @@ internal static class CommandTreeTokenizer
var value = current.ToString(CultureInfo.InvariantCulture); var value = current.ToString(CultureInfo.InvariantCulture);
result.Add(result.Count == 0 result.Add(result.Count == 0
? new CommandTreeToken(CommandTreeToken.Kind.ShortOption, position, value, $"-{value}") ? 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)) else if (result.Count == 0 && char.IsDigit(current))
{ {

View File

@ -89,7 +89,7 @@ public sealed partial class CommandAppTests
//cat.Name.ShouldBe("Kitty"); //<-- Should normally be correct, but instead name will be added to the remaining arguments (see below). //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.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("name", values: new[] { "Kitty", }); result.Context.ShouldHaveRemainingArgument("--name", values: new[] { "Kitty", });
} }
[Fact] [Fact]

View File

@ -56,7 +56,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.Remaining.Parsed.Count.ShouldBe(1); 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); result.Context.Remaining.Raw.Count.ShouldBe(0);
} }
@ -80,7 +80,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.Remaining.Parsed.Count.ShouldBe(1); 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); result.Context.Remaining.Raw.Count.ShouldBe(0);
} }
@ -189,10 +189,10 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.Remaining.Parsed.Count.ShouldBe(4); result.Context.Remaining.Parsed.Count.ShouldBe(4);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar", "baz" }); result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { "bar", "baz" });
result.Context.ShouldHaveRemainingArgument("b", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("-b", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("a", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("-a", values: new[] { (string)null });
result.Context.ShouldHaveRemainingArgument("r", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("-r", values: new[] { (string)null });
} }
[Fact] [Fact]
@ -280,7 +280,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.Remaining.Parsed.Count.ShouldBe(1); 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 result.Context.Remaining.Raw.Count.ShouldBe(0); // nb. there are no "raw" remaining arguments on the command line
} }

View File

@ -51,7 +51,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Output.ShouldBe(string.Empty); result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument("version", new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("--version", new[] { (string)null });
} }
[Fact] [Fact]
@ -70,7 +70,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Output.ShouldBe(string.Empty); result.Output.ShouldBe(string.Empty);
result.Context.ShouldHaveRemainingArgument("version", new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("--version", new[] { (string)null });
} }
[Fact] [Fact]

View File

@ -215,7 +215,7 @@ public sealed partial class CommandAppTests
dog.Name.ShouldBe("\" -Rufus --' "); dog.Name.ShouldBe("\" -Rufus --' ");
}); });
result.Context.Remaining.Parsed.Count.ShouldBe(1); 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] [Fact]
@ -844,7 +844,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
result.Context.Remaining.Parsed.Count.ShouldBe(1); result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar" }); result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { "bar" });
} }
[Fact] [Fact]
@ -875,8 +875,8 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
result.Context.Remaining.Parsed.Count.ShouldBe(2); result.Context.Remaining.Parsed.Count.ShouldBe(2);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { "bar" }); result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { "bar" });
result.Context.ShouldHaveRemainingArgument("f", values: new[] { "baz" }); result.Context.ShouldHaveRemainingArgument("-f", values: new[] { "baz" });
result.Context.Remaining.Raw.Count.ShouldBe(5); result.Context.Remaining.Raw.Count.ShouldBe(5);
result.Context.Remaining.Raw.ShouldBe(new[] result.Context.Remaining.Raw.ShouldBe(new[]
{ {
@ -909,7 +909,7 @@ public sealed partial class CommandAppTests
// Then // Then
result.Context.ShouldNotBeNull(); result.Context.ShouldNotBeNull();
result.Context.Remaining.Parsed.Count.ShouldBe(1); result.Context.Remaining.Parsed.Count.ShouldBe(1);
result.Context.ShouldHaveRemainingArgument("foo", values: new[] { (string)null }); result.Context.ShouldHaveRemainingArgument("--foo", values: new[] { (string)null });
} }
[Fact] [Fact]