Skip to content

Commit d50118a

Browse files
authored
remove obsolete Command.Argument and Command.GetValueOrDefault (#724)
1 parent 4562e72 commit d50118a

File tree

13 files changed

+61
-124
lines changed

13 files changed

+61
-124
lines changed

src/System.CommandLine.Suggest/SuggestionDispatcher.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,21 @@ Command ListCommand() =>
7171
c => c.Out.WriteLine(ShellPrefixesToMatch(_suggestionRegistration)))
7272
};
7373

74-
Command CompleteScriptCommand() =>
75-
new Command("script")
74+
Command CompleteScriptCommand()
75+
{
76+
var command = new Command("script")
7677
{
77-
Argument = new Argument<ShellType>
78+
new Argument<ShellType>
7879
{
7980
Name = nameof(ShellType)
80-
},
81-
Description = "Print complete script for specific shell",
82-
Handler = CommandHandler.Create<IConsole, ShellType>(SuggestionShellScriptHandler.Handle)
81+
}
8382
};
83+
84+
command.Handler = CommandHandler.Create<IConsole, ShellType>(SuggestionShellScriptHandler.Handle);
85+
command.Description = "Print complete script for specific shell";
86+
87+
return command;
88+
}
8489

8590
Command RegisterCommand()
8691
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void Command_arguments_are_bound_by_name_to_constructor_parameters(
6161

6262
var command = new Command("the-command")
6363
{
64-
Argument = new Argument
64+
new Argument
6565
{
6666
Name = "value",
6767
ArgumentType = type
@@ -146,7 +146,7 @@ public void Command_arguments_are_bound_by_name_to_property_setters(
146146

147147
var command = new Command("the-command")
148148
{
149-
Argument = new Argument
149+
new Argument
150150
{
151151
Name = "value",
152152
ArgumentType = type

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ public async Task Command_arguments_are_bound_by_name_to_handler_method_paramete
147147
.GetCommandHandler();
148148

149149
var command = new Command("the-command")
150-
{
151-
Argument = new Argument
152-
{
153-
Name = "value",
154-
ArgumentType = type
155-
}
156-
};
150+
{
151+
new Argument
152+
{
153+
Name = "value",
154+
ArgumentType = type
155+
}
156+
};
157157

158158
var console = new TestConsole();
159159

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

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ public void Command_argument_with_arity_of_one_can_be_bound_without_custom_conve
7676
{
7777
var option = new Command("the-command")
7878
{
79-
new Argument<FileInfo>()
79+
new Argument<FileInfo>("the-arg")
8080
};
8181

8282
var file = new FileInfo(Path.Combine(new DirectoryInfo("temp").FullName, "the-file.txt"));
8383
var result = option.Parse($"{file.FullName}");
8484

8585
result.CommandResult
86-
.GetValueOrDefault()
86+
.GetArgumentValueOrDefault("the-arg")
8787
.Should()
8888
.BeOfType<FileInfo>()
8989
.Which
@@ -95,18 +95,18 @@ public void Command_argument_with_arity_of_one_can_be_bound_without_custom_conve
9595
[Fact]
9696
public void Command_argument_with_arity_of_zero_or_one_when_type_has_a_constructor_that_takes_a_single_string_returns_null_when_argument_is_not_provided()
9797
{
98-
var option = new Command("the-command")
98+
var command = new Command("the-command")
9999
{
100-
new Argument<FileInfo>
100+
new Argument<FileInfo>("the-arg")
101101
{
102102
Arity = ArgumentArity.ZeroOrOne
103103
}
104104
};
105105

106-
var result = option.Parse("");
106+
var result = command.Parse("");
107107

108108
result.CommandResult
109-
.GetValueOrDefault()
109+
.GetArgumentValueOrDefault("the-arg")
110110
.Should()
111111
.BeNull();
112112
}
@@ -492,18 +492,6 @@ public void By_default_an_option_that_allows_multiple_arguments_and_is_passed_on
492492
.BeEquivalentTo(new[] { "arg1" });
493493
}
494494

495-
[Fact]
496-
public void The_default_value_of_a_command_with_no_arguments_is_null()
497-
{
498-
var result = new Command("x").Parse("").CommandResult;
499-
500-
var valueOrDefault = result.GetValueOrDefault();
501-
502-
valueOrDefault
503-
.Should()
504-
.BeNull();
505-
}
506-
507495
[Theory]
508496
[InlineData("c -a o c c")]
509497
[InlineData("c c -a o c")]
@@ -515,18 +503,18 @@ public void When_command_argument_has_arity_greater_than_one_it_captures_argumen
515503
new Option("-a")
516504
{
517505
Argument = new Argument<string>()
506+
},
507+
new Argument<string>("the-arg")
508+
{
509+
Arity = ArgumentArity.ZeroOrMore
518510
}
519511
};
520512

521-
command.Argument = new Argument<string>
522-
{
523-
Arity = ArgumentArity.ZeroOrMore
524-
};
525513

526514
var result = command.Parse(commandLine);
527515

528516
result.CommandResult
529-
.GetValueOrDefault()
517+
.GetArgumentValueOrDefault("the-arg")
530518
.Should()
531519
.BeEquivalentTo(new[] { "c", "c", "c" });
532520
}
@@ -633,14 +621,14 @@ public void A_default_value_with_a_custom_constructor_can_be_specified_for_a_com
633621

634622
var command = new Command("something")
635623
{
636-
new Argument<DirectoryInfo>(() => directoryInfo)
624+
new Argument<DirectoryInfo>("the-arg", () => directoryInfo)
637625
};
638626

639627
var result = command.Parse("something");
640628

641629
result.Errors.Should().BeEmpty();
642630

643-
var value = result.CommandResult.GetValueOrDefault();
631+
var value = result.CommandResult.GetArgumentValueOrDefault("the-arg");
644632

645633
value.Should().Be(directoryInfo);
646634
}
@@ -663,21 +651,6 @@ public void An_option_argument_with_a_default_argument_can_be_converted_to_the_r
663651
value.Should().Be(123);
664652
}
665653

666-
[Fact]
667-
public void A_command_argument_with_a_default_value_can_be_converted_to_the_requested_type()
668-
{
669-
var command = new Command("something")
670-
{
671-
new Argument<string>(() => "123")
672-
};
673-
674-
var result = command.Parse("something");
675-
676-
var value = result.CommandResult.GetValueOrDefault<int>();
677-
678-
value.Should().Be(123);
679-
}
680-
681654
[Fact]
682655
public void Specifying_an_option_argument_overrides_the_default_value()
683656
{

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation and contributors. All rights reserved.
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
@@ -332,7 +332,7 @@ public void Options_do_not_get_unbundled_unless_all_resulting_options_would_be_v
332332
outer.AddOption(new Option("-a"));
333333
var inner = new Command("inner")
334334
{
335-
Argument = new Argument
335+
new Argument
336336
{
337337
Arity = ArgumentArity.ZeroOrMore
338338
}
@@ -1298,12 +1298,15 @@ public void Commands_can_have_default_argument_values()
12981298
{
12991299
var command = new Command("command")
13001300
{
1301-
new Argument<string>(() => "default")
1301+
new Argument<string>("the-arg", () => "default")
13021302
};
13031303

13041304
ParseResult result = command.Parse("command");
13051305

1306-
result.CommandResult.GetValueOrDefault().Should().Be("default");
1306+
result.CommandResult
1307+
.GetArgumentValueOrDefault("the-arg")
1308+
.Should()
1309+
.Be("default");
13071310
}
13081311

13091312
[Fact]
@@ -1347,23 +1350,21 @@ public void When_an_option_with_a_default_value_is_not_matched_then_the_option_r
13471350
[Fact]
13481351
public void Command_default_argument_value_does_not_override_parsed_value()
13491352
{
1350-
DirectoryInfo receivedArg = null;
1351-
13521353
var command = new Command("inner")
13531354
{
1354-
Argument = new Argument<DirectoryInfo>(() => new DirectoryInfo(Directory.GetCurrentDirectory()))
1355+
new Argument<DirectoryInfo>(() => new DirectoryInfo(Directory.GetCurrentDirectory()))
13551356
{
1356-
Name = "arg"
1357-
},
1358-
Handler = CommandHandler.Create<DirectoryInfo>(arg => receivedArg = arg)
1357+
Name = "the-arg"
1358+
}
13591359
};
1360+
command.Handler = CommandHandler.Create<DirectoryInfo>(arg => { } );
13601361

13611362
var result = command.Parse("the-directory");
13621363

13631364
_output.WriteLine(result.ToString());
13641365

13651366
result.CommandResult
1366-
.GetValueOrDefault<DirectoryInfo>()
1367+
.GetArgumentValueOrDefault<DirectoryInfo>("the-arg")
13671368
.Name
13681369
.Should()
13691370
.Be("the-directory");
@@ -1712,7 +1713,7 @@ public void Command_argument_arity_can_be_a_fixed_value_greater_than_1()
17121713
{
17131714
var command = new Command("the-command")
17141715
{
1715-
Argument = new Argument
1716+
new Argument
17161717
{
17171718
Arity = new ArgumentArity(3, 3)
17181719
}
@@ -1733,7 +1734,7 @@ public void Command_argument_arity_can_be_a_range_with_a_lower_bound_greater_tha
17331734
{
17341735
var command = new Command("the-command")
17351736
{
1736-
Argument = new Argument
1737+
new Argument
17371738
{
17381739
Arity = new ArgumentArity(3, 5)
17391740
}
@@ -1764,7 +1765,7 @@ public void When_command_arguments_are_fewer_than_minimum_arity_then_an_error_is
17641765
{
17651766
var command = new Command("the-command")
17661767
{
1767-
Argument = new Argument
1768+
new Argument
17681769
{
17691770
Arity = new ArgumentArity(2, 3)
17701771
}
@@ -1783,7 +1784,7 @@ public void When_command_arguments_are_greater_than_maximum_arity_then_an_error_
17831784
{
17841785
var command = new Command("the-command")
17851786
{
1786-
Argument = new Argument
1787+
new Argument
17871788
{
17881789
Arity = new ArgumentArity(2, 3)
17891790
}

src/System.CommandLine.Tests/ResponseFileTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void When_response_file_is_specified_it_loads_command_arguments_from_resp
8282

8383
var result = new RootCommand
8484
{
85-
Argument = new Argument<string[]>()
85+
new Argument<string[]>()
8686
}
8787
.Parse($"@{responseFile}");
8888

@@ -104,7 +104,7 @@ public void Response_file_can_provide_subcommand_arguments()
104104
{
105105
new Command("subcommand")
106106
{
107-
Argument = new Argument<string[]>()
107+
new Argument<string[]>()
108108
}
109109
}
110110
.Parse($"subcommand @{responseFile}");
@@ -124,7 +124,7 @@ public void Response_file_can_provide_subcommand()
124124
{
125125
new Command("subcommand")
126126
{
127-
Argument = new Argument<string[]>()
127+
new Argument<string[]>()
128128
}
129129
}
130130
.Parse($"@{responseFile} one two three");
@@ -147,7 +147,7 @@ public void When_response_file_is_specified_it_loads_subcommand_arguments_from_r
147147
{
148148
new Command("subcommand")
149149
{
150-
Argument = new Argument<string[]>()
150+
new Argument<string[]>()
151151
}
152152
}
153153
.Parse($"subcommand @{responseFile}");

src/System.CommandLine.Tests/SuggestDirectiveTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ public async Task It_writes_suggestions_for_partial_option_and_subcommand_aliase
243243
new Command("child"),
244244
new Option("--option1"),
245245
new Option("--option2"),
246+
new Argument<string>()
246247
};
247-
command.Argument = new Argument<string>();
248248

249249
var console = new TestConsole();
250250

src/System.CommandLine.Tests/SuggestionTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public void When_a_subcommand_has_been_specified_then_its_sibling_commands_will_
189189
public void When_a_subcommand_has_been_specified_then_its_sibling_options_will_be_suggested()
190190
{
191191
var command = new RootCommand("parent")
192-
{
193-
new Command("child"),
194-
new Option("--parent-option")
195-
};
196-
command.Argument = new Argument<string>();
192+
{
193+
new Command("child"),
194+
new Option("--parent-option"),
195+
new Argument<string>()
196+
};
197197

198198
var commandLine = "child";
199199
var parseResult = command.Parse(commandLine);

src/System.CommandLine/Argument{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public Argument() : base(null)
1818
ArgumentType = typeof(T);
1919
}
2020

21-
public Argument(string name, T defaultValue) : this(name)
21+
public Argument(string name, Func<T> defaultValue) : this(name)
2222
{
23-
SetDefaultValue(defaultValue);
23+
SetDefaultValue(() => defaultValue());
2424
}
2525

2626
public Argument(Func<T> defaultValue) : this()

src/System.CommandLine/Command.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,6 @@ public Command(string name, string description = null) : base(new[] { name }, de
1515
{
1616
}
1717

18-
[Obsolete("Use the Arguments property instead")]
19-
public virtual Argument Argument
20-
{
21-
get => Arguments.SingleOrDefault() ??
22-
Argument.None;
23-
set
24-
{
25-
foreach (var argument in Arguments.ToArray())
26-
{
27-
Children.Remove(argument);
28-
}
29-
30-
AddArgumentInner(value);
31-
}
32-
}
33-
3418
public IEnumerable<Argument> Arguments => Children.OfType<Argument>();
3519

3620
public IEnumerable<Option> Options => Children.OfType<Option>();
@@ -57,10 +41,6 @@ public virtual Argument Argument
5741

5842
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
5943

60-
#pragma warning disable 618
61-
IArgument ICommand.Argument => Argument;
62-
#pragma warning restore 618
63-
6444
IEnumerable<IArgument> ICommand.Arguments => Arguments;
6545

6646
IEnumerable<IOption> ICommand.Options => Options;

0 commit comments

Comments
 (0)