Skip to content

Commit be7ff04

Browse files
committed
change Parser constructor that accepts Symbol to accept Command
1 parent 7ff5435 commit be7ff04

File tree

6 files changed

+53
-38
lines changed

6 files changed

+53
-38
lines changed

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,42 @@ public void Parse_delegate_is_called_once_per_parse_operation()
500500
i.Should().Be(2);
501501
}
502502

503+
[Theory] // https://github.com/dotnet/command-line-api/issues/1294
504+
[InlineData("", "option-is-implicit")]
505+
[InlineData("--bananas", "argument-is-implicit")]
506+
[InlineData("--bananas argument-is-specified", "argument-is-specified")]
507+
public void Parse_delegate_is_called_when_Option_Arity_allows_zero_tokens(string commandLine, string expectedValue)
508+
{
509+
var opt = new Option<string>(
510+
"--bananas",
511+
parseArgument: result =>
512+
{
513+
if (result.Tokens.Count == 0)
514+
{
515+
if (result.Parent is OptionResult { IsImplicit: true })
516+
{
517+
return "option-is-implicit";
518+
}
519+
520+
return "argument-is-implicit";
521+
}
522+
else
523+
{
524+
return result.Tokens[0].Value;
525+
}
526+
}, isDefault: true)
527+
{
528+
Arity = ArgumentArity.ZeroOrOne
529+
};
530+
531+
var rootCommand = new RootCommand
532+
{
533+
opt
534+
};
535+
536+
rootCommand.Parse(commandLine).ValueForOption(opt).Should().Be(expectedValue);
537+
}
538+
503539
[Theory]
504540
[InlineData("1 2 3 4 5 6 7 8")]
505541
[InlineData("-o 999 1 2 3 4 5 6 7 8")]

src/System.CommandLine.Tests/Binding/ModelBinderTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,8 @@ public void PropertyExpression_can_be_bound_to_argument()
493493
public void Option_argument_is_bound_to_longest_constructor()
494494
{
495495
var option = new Option<int>("--int-property");
496-
var parser = new Parser(option);
497496

498-
var bindingContext = new BindingContext(parser.Parse("--int-property 42"));
497+
var bindingContext = new BindingContext(option.Parse("--int-property 42"));
499498
var binder = new ModelBinder<ClassWithMultipleCtor>();
500499
var instance = binder.CreateInstance(bindingContext) as ClassWithMultipleCtor;
501500

src/System.CommandLine.Tests/Binding/TypeConversionTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ public void Argument_infers_arity_of_IEnumerable_types_as_OneOrMore(Type type)
121121
public void Argument_bool_will_default_to_true_when_no_argument_is_passed()
122122
{
123123
var option = new Option<bool>("-x");
124-
var parser = new Parser(option);
125124

126-
var result = parser.Parse("-x");
125+
var result = option.Parse("-x");
127126

128127
result.Errors.Should().BeEmpty();
129128
result.GetValueForOption(option).Should().Be(true);

src/System.CommandLine.Tests/ParsingValidationTests.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ public ParsingValidationTests(ITestOutputHelper output)
2525
[Fact]
2626
public void When_an_option_accepts_only_specific_arguments_but_a_wrong_one_is_supplied_then_an_informative_error_is_returned()
2727
{
28-
var parser = new Parser(
29-
new Option("-x", arity: ArgumentArity.ExactlyOne)
30-
.FromAmong("this", "that", "the-other-thing"));
31-
32-
var result = parser.Parse("-x none-of-those");
28+
var option = new Option("-x", arity: ArgumentArity.ExactlyOne)
29+
.FromAmong("this", "that", "the-other-thing");
30+
31+
var result = option.Parse("-x none-of-those");
3332

3433
result.Errors
3534
.Select(e => e.Message)
@@ -44,9 +43,7 @@ public void When_an_option_has_en_error_then_the_error_has_a_reference_to_the_op
4443
var option = new Option("-x", arity: ArgumentArity.ExactlyOne)
4544
.FromAmong("this", "that");
4645

47-
var parser = new Parser(option);
48-
49-
var result = parser.Parse("-x something_else");
46+
var result = option.Parse("-x something_else");
5047

5148
result.Errors
5249
.Where(e => e.SymbolResult != null)
@@ -57,9 +54,9 @@ public void When_an_option_has_en_error_then_the_error_has_a_reference_to_the_op
5754
[Fact]
5855
public void When_a_required_argument_is_not_supplied_then_an_error_is_returned()
5956
{
60-
var parser = new Parser(new Option("-x", arity: ArgumentArity.ExactlyOne));
57+
var option = new Option("-x", arity: ArgumentArity.ExactlyOne);
6158

62-
var result = parser.Parse("-x");
59+
var result = option.Parse("-x");
6360

6461
result.Errors
6562
.Should()
@@ -894,10 +891,9 @@ public void A_command_with_subcommands_is_valid_to_invoke_if_it_has_a_handler()
894891
[Fact]
895892
public void When_an_option_has_a_default_value_it_is_not_valid_to_specify_the_option_without_an_argument()
896893
{
897-
var parser = new Parser(
898-
new Option<int>("-x", () => 123));
894+
var option = new Option<int>("-x", () => 123);
899895

900-
var result = parser.Parse("-x");
896+
var result = option.Parse("-x");
901897

902898
result.Errors
903899
.Select(e => e.Message)

src/System.CommandLine/CommandLineConfiguration.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CommandLineConfiguration
2222
/// <summary>
2323
/// Initializes a new instance of the CommandLineConfiguration class.
2424
/// </summary>
25-
/// <param name="symbol">The symbol to parse.</param>
25+
/// <param name="command">The symbol to parse.</param>
2626
/// <param name="enablePosixBundling"><see langword="true"/> to enable POSIX bundling; otherwise, <see langword="false"/>.</param>
2727
/// <param name="enableDirectives"><see langword="true"/> to enable directive parsing; otherwise, <see langword="false"/>.</param>
2828
/// <param name="enableLegacyDoubleDashBehavior">Enables the legacy behavior of the <c>--</c> token, which is to ignore parsing of subsequent tokens and place them in the <see cref="ParseResult.UnparsedTokens"/> list.</param>
@@ -32,7 +32,7 @@ public class CommandLineConfiguration
3232
/// <param name="helpBuilderFactory">Provide a custom help builder.</param>
3333
/// <param name="configureHelp">Configures the help builder.</param>
3434
public CommandLineConfiguration(
35-
Symbol symbol,
35+
Command command,
3636
bool enablePosixBundling = true,
3737
bool enableDirectives = true,
3838
bool enableLegacyDoubleDashBehavior = false,
@@ -42,27 +42,11 @@ public CommandLineConfiguration(
4242
Func<BindingContext, IHelpBuilder>? helpBuilderFactory = null,
4343
Action<IHelpBuilder>? configureHelp = null)
4444
{
45-
if (symbol is null)
46-
{
47-
throw new ArgumentNullException(nameof(symbol));
48-
}
49-
50-
if (symbol is Command rootCommand)
51-
{
52-
RootCommand = rootCommand;
53-
}
54-
else
55-
{
56-
rootCommand = new RootCommand();
57-
58-
rootCommand.Add(symbol);
59-
60-
RootCommand = rootCommand;
61-
}
45+
RootCommand = command ?? throw new ArgumentNullException(nameof(command));
6246

6347
_symbols.Add(RootCommand);
6448

65-
AddGlobalOptionsToChildren(rootCommand);
49+
AddGlobalOptionsToChildren(command);
6650

6751
EnableLegacyDoubleDashBehavior = enableLegacyDoubleDashBehavior;
6852
EnablePosixBundling = enablePosixBundling;

src/System.CommandLine/Parsing/Parser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public Parser(CommandLineConfiguration configuration)
2222
}
2323
}
2424

25-
public Parser(Symbol symbol) : this(new CommandLineConfiguration(symbol))
25+
/// <param name="command">The root command for the parser.</param>
26+
public Parser(Command command) : this(new CommandLineConfiguration(command))
2627
{
2728
}
2829

0 commit comments

Comments
 (0)