Skip to content

Commit 465f9e5

Browse files
authored
Making argument internal to option (#1148)
* WIP making argument internal to option * Fixing NRT warning * Fixing end to end test app * Updating tests
1 parent 6493867 commit 465f9e5

31 files changed

+416
-1350
lines changed

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_CustomScenarios.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public void SetupOneOptWithNestedCommand()
2020
{
2121
var rootCommand = new Command("root_command");
2222
var nestedCommand = new Command("nested_command");
23-
var option = new Option("-opt1")
24-
{
25-
Argument = new Argument<int>(() => 123)
26-
};
23+
var option = new Option<int>("-opt1", () => 123);
2724
nestedCommand.AddOption(option);
2825
rootCommand.AddCommand(nestedCommand);
2926

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Directives_Suggest.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,8 @@ public void Setup()
2626

2727
var eatCommand = new Command("eat")
2828
{
29-
new Option("--fruit")
30-
{
31-
Argument = new Argument<string>()
32-
{
33-
Suggestions = {"apple", "banana", "cherry" }
34-
}
35-
},
36-
new Option("--vegetable")
37-
{
38-
Argument = new Argument<string>()
39-
{
40-
Suggestions = {"asparagus", "broccoli", "carrot" }
41-
}
42-
}
29+
new Option<string>("--fruit").AddSuggestions("apple", "banana", "cherry"),
30+
new Option<string>("--vegetable").AddSuggestions("asparagus", "broccoli", "carrot")
4331
};
4432

4533
_testParser = new CommandLineBuilder(eatCommand)

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@ public class Perf_Parser_Options_Bare
2121
private IEnumerable<Option> GenerateTestOptions(int count, IArgumentArity arity)
2222
=> Enumerable.Range(0, count)
2323
.Select(i =>
24-
new Option($"-option{i}")
24+
new Option($"-option{i}", arity: arity)
2525
{
26-
Description = $"Description for -option {i} ....",
27-
Argument = new Argument
28-
{
29-
Arity = arity
30-
}
26+
Description = $"Description for -option {i} ...."
3127
}
3228
);
3329

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_With_Arguments.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ public class Perf_Parser_Options_With_Arguments
1919

2020
private IEnumerable<Option> GenerateTestOptions(int count, IArgumentArity arity)
2121
=> Enumerable.Range(0, count)
22-
.Select(i => new Option($"-option{i}")
22+
.Select(i => new Option($"-option{i}", arity: arity)
2323
{
24-
Description = $"Description for -option {i} ....",
25-
Argument = new Argument
26-
{
27-
Arity = arity
28-
}
24+
Description = $"Description for -option {i} ...."
2925
}
3026
);
3127

src/System.CommandLine.Benchmarks/CommandLine/Perf_Suggestions.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ private IEnumerable<Option> GenerateOptionsArray(int count)
3737
[GlobalSetup(Target = nameof(SuggestionsFromSymbol))]
3838
public void Setup_FromSymbol()
3939
{
40-
_testSymbol = new Option("--hello")
41-
{
42-
Argument = new Argument
43-
{
44-
Arity = ArgumentArity.ExactlyOne,
45-
Suggestions = { GenerateSuggestionsArray(TestSuggestionsCount) }
46-
}
47-
};
40+
_testSymbol = new Option("--hello", arity: ArgumentArity.ExactlyOne)
41+
.AddSuggestions(GenerateSuggestionsArray(TestSuggestionsCount));
4842
}
4943

5044
[Benchmark]

src/System.CommandLine.DragonFruit.Tests/ConfigureFromMethodTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public void Options_are_not_built_for_infrastructure_types_exposed_by_method_par
220220
var options = handlerMethod.BuildOptions();
221221

222222
options.Should()
223-
.NotContain(o => o.Argument.ArgumentType == type);
223+
.NotContain(o => o.ArgumentType == type);
224224
}
225225

226226
[Fact]

src/System.CommandLine.DragonFruit/CommandLine.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,24 +290,16 @@ public static IEnumerable<Option> BuildOptions(this MethodInfo method)
290290

291291
public static Option BuildOption(this ParameterDescriptor parameter)
292292
{
293-
var argument = new Argument
294-
{
295-
ArgumentType = parameter.ValueType
296-
};
297-
293+
Func<object> getDefaultValue = null;
298294
if (parameter.HasDefaultValue)
299295
{
300-
argument.SetDefaultValueFactory(parameter.GetDefaultValue);
296+
getDefaultValue = parameter.GetDefaultValue;
301297
}
302-
303-
var option = new Option(
298+
return new Option(
304299
parameter.BuildAlias(),
305-
parameter.ValueName)
306-
{
307-
Argument = argument
308-
};
309-
310-
return option;
300+
parameter.ValueName,
301+
parameter.ValueType,
302+
getDefaultValue);
311303
}
312304

313305
private static string GetDefaultXmlDocsFileLocation(Assembly assembly)

src/System.CommandLine.Hosting.Tests/HostingTests.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ public static void UseHost_binds_parsed_arguments_to_options()
201201
MyOptions options = null;
202202

203203
var rootCmd = new RootCommand();
204-
rootCmd.AddOption(
205-
new Option($"-{nameof(MyOptions.MyArgument)}")
206-
{ Argument = new Argument<int>() }
207-
);
204+
rootCmd.AddOption(new Option<int>($"-{nameof(MyOptions.MyArgument)}"));
208205
rootCmd.Handler = CommandHandler.Create((IHost host) =>
209206
{
210207
options = host.Services

src/System.CommandLine.Suggest.Tests/EndToEndTestApp/Program.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,10 @@ static async Task Main(string[] args)
1313
{
1414
var rootCommand = new RootCommand
1515
{
16-
new Option("--apple" )
17-
{
18-
Argument = new Argument<string>()
19-
},
20-
new Option("--banana")
21-
{
22-
Argument = new Argument<string>()
23-
},
24-
new Option("--cherry")
25-
{
26-
Argument = new Argument<string>()
27-
},
28-
new Option("--durian")
29-
{
30-
Argument = new Argument<string>()
31-
}
16+
new Option<string>("--apple" ),
17+
new Option<string>("--banana"),
18+
new Option<string>("--cherry"),
19+
new Option<string>("--durian")
3220
};
3321

3422
rootCommand.Handler = CommandHandler.Create(typeof(Program).GetMethod(nameof(Run)));

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,11 @@ public async Task Custom_argument_parser_is_only_called_once()
367367
{
368368
Handler = CommandHandler.Create<int>(Run)
369369
};
370-
command.AddOption(new Option("--value")
370+
command.AddOption(new Option<int>("--value", result =>
371371
{
372-
Argument = new Argument<int>(result =>
373-
{
374-
callCount++;
375-
return int.Parse(result.Tokens.Single().Value);
376-
})
377-
});
372+
callCount++;
373+
return int.Parse(result.Tokens.Single().Value);
374+
}));
378375

379376
await command.InvokeAsync("--value 42");
380377

@@ -439,14 +436,11 @@ public void When_custom_conversion_fails_then_an_option_does_not_accept_further_
439436
var command = new Command("the-command")
440437
{
441438
new Argument<string>(),
442-
new Option("-x")
443-
{
444-
Argument = new Argument<string>(argResult =>
439+
new Option<string>("-x", argResult =>
445440
{
446441
argResult.ErrorMessage = "nope";
447442
return default;
448443
})
449-
}
450444
};
451445

452446
var result = command.Parse("the-command -x nope yep");
@@ -459,9 +453,7 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
459453
{
460454
var command = new Command("the-command")
461455
{
462-
new Option(new[] { "-o", "--one" })
463-
{
464-
Argument = new Argument<int>(argumentResult =>
456+
new Option<int>(new[] { "-o", "--one" }, argumentResult =>
465457
{
466458
if (int.TryParse(argumentResult.Tokens.Select(t => t.Value).Single(), out var value))
467459
{
@@ -472,7 +464,6 @@ public void When_argument_cannot_be_parsed_as_the_specified_type_then_getting_va
472464

473465
return default;
474466
})
475-
}
476467
};
477468

478469
var result = command.Parse("the-command -o not-an-int");

0 commit comments

Comments
 (0)