Skip to content

Commit 0adeb26

Browse files
authored
Moves and renames (#1951)
* Rename to AcceptExistingOnly * Rename to AcceptOnlyFromAmong * Move non-generic extensions to be instance methods * Rename to AcceptLegalFile[Names|Paths]Only * Align arguments to the word completion * Address feedback: remove the Extensions suffix * Fix release build: one rename was missing * Missing ApiCompat changes * Missing ApiCompat changes RE: Arity on option
1 parent cfdadc3 commit 0adeb26

17 files changed

+444
-513
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ System.CommandLine
55
public System.Boolean HasDefaultValue { get; }
66
public System.String HelpName { get; set; }
77
public System.Type ValueType { get; }
8+
public Argument AcceptLegalFileNamesOnly()
9+
public Argument AcceptLegalFilePathsOnly()
10+
public Argument AcceptOnlyFromAmong(System.String[] values)
11+
public Argument AddCompletions(System.String[] completions)
12+
public Argument AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
13+
public Argument AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
814
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
915
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
1016
public System.Object GetDefaultValue()
17+
public ParseResult Parse(System.String commandLine)
18+
public ParseResult Parse(System.String[] args)
1119
public System.Void SetDefaultValue(System.Object value)
1220
public System.Void SetDefaultValueFactory(System.Func<System.Object> defaultValueFactory)
1321
public System.Void SetDefaultValueFactory(System.Func<System.CommandLine.Parsing.ArgumentResult,System.Object> defaultValueFactory)
@@ -32,19 +40,11 @@ System.CommandLine
3240
public System.Boolean Equals(ArgumentArity other)
3341
public System.Boolean Equals(System.Object obj)
3442
public System.Int32 GetHashCode()
35-
public static class ArgumentExtensions
36-
public static TArgument AddCompletions<TArgument>(this TArgument argument, System.String[] values)
37-
public static TArgument AddCompletions<TArgument>(this TArgument argument, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
38-
public static TArgument AddCompletions<TArgument>(this TArgument argument, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> complete)
39-
public static Argument<System.IO.FileInfo> ExistingOnly(this Argument<System.IO.FileInfo> argument)
40-
public static Argument<System.IO.DirectoryInfo> ExistingOnly(this Argument<System.IO.DirectoryInfo> argument)
41-
public static Argument<System.IO.FileSystemInfo> ExistingOnly(this Argument<System.IO.FileSystemInfo> argument)
42-
public static Argument<T> ExistingOnly<T>(this Argument<T> argument)
43-
public static TArgument FromAmong<TArgument>(this TArgument argument, System.String[] values)
44-
public static TArgument LegalFileNamesOnly<TArgument>(this TArgument argument)
45-
public static TArgument LegalFilePathsOnly<TArgument>(this TArgument argument)
46-
public static ParseResult Parse(this Argument argument, System.String commandLine)
47-
public static ParseResult Parse(this Argument argument, System.String[] args)
43+
public static class ArgumentValidation
44+
public static Argument<System.IO.FileInfo> AcceptExistingOnly(this Argument<System.IO.FileInfo> argument)
45+
public static Argument<System.IO.DirectoryInfo> AcceptExistingOnly(this Argument<System.IO.DirectoryInfo> argument)
46+
public static Argument<System.IO.FileSystemInfo> AcceptExistingOnly(this Argument<System.IO.FileSystemInfo> argument)
47+
public static Argument<T> AcceptExistingOnly<T>(this Argument<T> argument)
4848
public class Command : IdentifierSymbol, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable
4949
.ctor(System.String name, System.String description = null)
5050
public System.Collections.Generic.IReadOnlyList<Argument> Arguments { get; }
@@ -109,7 +109,7 @@ System.CommandLine
109109
.ctor()
110110
.ctor(System.String message, System.Exception innerException)
111111
public static class CompletionSourceExtensions
112-
public static System.Void Add(this System.Collections.Generic.ICollection<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
112+
public static System.Void Add(this System.Collections.Generic.ICollection<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
113113
public static System.Void Add(this System.Collections.Generic.ICollection<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.String[] completions)
114114
public static class ConsoleExtensions
115115
public static System.Void Write(this IConsole console, System.String value)
@@ -193,9 +193,17 @@ System.CommandLine
193193
public ArgumentArity Arity { get; set; }
194194
public System.Boolean IsRequired { get; set; }
195195
public System.Type ValueType { get; }
196+
public Option AcceptLegalFileNamesOnly()
197+
public Option AcceptLegalFilePathsOnly()
198+
public Option AcceptOnlyFromAmong(System.String[] values)
199+
public Option AddCompletions(System.String[] completions)
200+
public Option AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
201+
public Option AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
196202
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
197203
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
198204
public System.Boolean HasAliasIgnoringPrefix(System.String alias)
205+
public ParseResult Parse(System.String commandLine)
206+
public ParseResult Parse(System.String[] args)
199207
public System.Void SetDefaultValue(System.Object value)
200208
public System.Void SetDefaultValueFactory(System.Func<System.Object> defaultValueFactory)
201209
public class Option<T> : Option, IValueDescriptor<T>, System.CommandLine.Binding.IValueDescriptor
@@ -205,19 +213,11 @@ System.CommandLine
205213
.ctor(System.String[] aliases, Func<System.CommandLine.Parsing.ArgumentResult,T> parseArgument, System.Boolean isDefault = False, System.String description = null)
206214
.ctor(System.String name, Func<T> defaultValueFactory, System.String description = null)
207215
.ctor(System.String[] aliases, Func<T> defaultValueFactory, System.String description = null)
208-
public static class OptionExtensions
209-
public static TOption AddCompletions<TOption>(this TOption option, System.String[] values)
210-
public static TOption AddCompletions<TOption>(this TOption option, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> complete)
211-
public static TOption AddCompletions<TOption>(this TOption option, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> complete)
212-
public static Option<System.IO.FileInfo> ExistingOnly(this Option<System.IO.FileInfo> option)
213-
public static Option<System.IO.DirectoryInfo> ExistingOnly(this Option<System.IO.DirectoryInfo> option)
214-
public static Option<System.IO.FileSystemInfo> ExistingOnly(this Option<System.IO.FileSystemInfo> option)
215-
public static Option<T> ExistingOnly<T>(this Option<T> option)
216-
public static TOption FromAmong<TOption>(this TOption option, System.String[] values)
217-
public static TOption LegalFileNamesOnly<TOption>(this TOption option)
218-
public static TOption LegalFilePathsOnly<TOption>(this TOption option)
219-
public static ParseResult Parse(this Option option, System.String commandLine)
220-
public static ParseResult Parse(this Option option, System.String[] args)
216+
public static class OptionValidation
217+
public static Option<System.IO.FileInfo> AcceptExistingOnly(this Option<System.IO.FileInfo> option)
218+
public static Option<System.IO.DirectoryInfo> AcceptExistingOnly(this Option<System.IO.DirectoryInfo> option)
219+
public static Option<System.IO.FileSystemInfo> AcceptExistingOnly(this Option<System.IO.FileSystemInfo> option)
220+
public static Option<T> AcceptExistingOnly<T>(this Option<T> option)
221221
public class ParseResult
222222
public System.CommandLine.Parsing.CommandResult CommandResult { get; }
223223
public System.Collections.Generic.IReadOnlyDictionary<System.String,System.Collections.Generic.IReadOnlyList<System.String>> Directives { get; }

src/System.CommandLine.Suggest/SuggestionDispatcher.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ public SuggestionDispatcher(ISuggestionRegistration suggestionRegistration, ISug
8989

9090
private Command GetCommand { get; }
9191

92-
private Option<FileInfo> ExecutableOption { get; } =
93-
new Option<FileInfo>(new[] { "-e", "--executable" }, "The executable to call for suggestions")
94-
.LegalFilePathsOnly();
92+
private Option<FileInfo> ExecutableOption { get; } = GetExecutableOption();
93+
94+
private static Option<FileInfo> GetExecutableOption()
95+
{
96+
var option = new Option<FileInfo>(new[] { "-e", "--executable" }, "The executable to call for suggestions");
97+
option.AcceptLegalFilePathsOnly();
98+
99+
return option;
100+
}
95101

96102
private Command ListCommand { get; }
97103

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ public void OnlyTake_can_pass_on_all_tokens_from_a_single_arity_argument_to_anot
769769
public void Argument_of_enum_can_limit_enum_members_as_valid_values()
770770
{
771771
var argument = new Argument<ConsoleColor>()
772-
.FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
772+
.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
773773
Command command = new("set-color")
774774
{
775775
argument

src/System.CommandLine.Tests/CompletionContextTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public void When_position_is_greater_than_input_length_in_a_string_command_line_
112112
var command = new Command("the-command")
113113
{
114114
new Argument<string>(),
115-
new Option<string>("--option1").FromAmong("apple", "banana", "cherry", "durian"),
115+
new Option<string>("--option1").AcceptOnlyFromAmong("apple", "banana", "cherry", "durian"),
116116
new Option<string>("--option2")
117117
};
118118

@@ -178,7 +178,7 @@ public void When_position_is_unspecified_in_array_command_line_and_final_token_m
178178
{
179179
var command = new Command("the-command")
180180
{
181-
new Option<string>("--option1").FromAmong("apple", "banana", "cherry", "durian"),
181+
new Option<string>("--option1").AcceptOnlyFromAmong("apple", "banana", "cherry", "durian"),
182182
new Option<string>("--option2"),
183183
new Argument<string>()
184184
};

src/System.CommandLine.Tests/CompletionTests.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ public void Parser_options_can_supply_context_sensitive_matches()
438438
{
439439
var parser = new RootCommand
440440
{
441-
new Option<string>("--bread").FromAmong("wheat", "sourdough", "rye"),
442-
new Option<string>("--cheese").FromAmong("provolone", "cheddar", "cream cheese")
441+
new Option<string>("--bread").AcceptOnlyFromAmong("wheat", "sourdough", "rye"),
442+
new Option<string>("--cheese").AcceptOnlyFromAmong("provolone", "cheddar", "cream cheese")
443443
};
444444

445445
var commandLine = "--bread";
@@ -543,8 +543,8 @@ public void Argument_completions_can_be_based_on_the_proximate_option()
543543
var parser = new Parser(
544544
new Command("outer")
545545
{
546-
new Option<string>("--one").FromAmong("one-a", "one-b"),
547-
new Option<string>("--two").FromAmong("two-a", "two-b")
546+
new Option<string>("--one").AcceptOnlyFromAmong("one-a", "one-b"),
547+
new Option<string>("--two").AcceptOnlyFromAmong("two-a", "two-b")
548548
});
549549

550550
var commandLine = "outer --two";
@@ -642,11 +642,11 @@ public void When_caller_does_the_tokenizing_then_argument_completions_are_based_
642642
var command = new Command("outer")
643643
{
644644
new Option<string>("one")
645-
.FromAmong("one-a", "one-b", "one-c"),
645+
.AcceptOnlyFromAmong("one-a", "one-b", "one-c"),
646646
new Option<string>("two")
647-
.FromAmong("two-a", "two-b", "two-c"),
647+
.AcceptOnlyFromAmong("two-a", "two-b", "two-c"),
648648
new Option<string>("three")
649-
.FromAmong("three-a", "three-b", "three-c")
649+
.AcceptOnlyFromAmong("three-a", "three-b", "three-c")
650650
};
651651

652652
var parser = new CommandLineBuilder(new RootCommand
@@ -669,11 +669,11 @@ public void When_parsing_from_array_then_argument_completions_are_based_on_the_p
669669
var command = new Command("outer")
670670
{
671671
new Option<string>("one")
672-
.FromAmong("one-a", "one-b", "one-c"),
672+
.AcceptOnlyFromAmong("one-a", "one-b", "one-c"),
673673
new Option<string>("two")
674-
.FromAmong("two-a", "two-b", "two-c"),
674+
.AcceptOnlyFromAmong("two-a", "two-b", "two-c"),
675675
new Option<string>("three")
676-
.FromAmong("three-a", "three-b", "three-c")
676+
.AcceptOnlyFromAmong("three-a", "three-b", "three-c")
677677
};
678678

679679
var result = command.Parse("outer two b");
@@ -691,15 +691,15 @@ public void When_parsing_from_text_then_argument_completions_are_based_on_the_pr
691691
{
692692
new Command("one")
693693
{
694-
new Argument<string>().FromAmong("one-a", "one-b", "one-c")
694+
new Argument<string>().AcceptOnlyFromAmong("one-a", "one-b", "one-c")
695695
},
696696
new Command("two")
697697
{
698-
new Argument<string>().FromAmong("two-a", "two-b", "two-c")
698+
new Argument<string>().AcceptOnlyFromAmong("two-a", "two-b", "two-c")
699699
},
700700
new Command("three")
701701
{
702-
new Argument<string>().FromAmong("three-a", "three-b", "three-c")
702+
new Argument<string>().AcceptOnlyFromAmong("three-a", "three-b", "three-c")
703703
}
704704
};
705705

@@ -718,15 +718,15 @@ public void When_parsing_from_array_then_argument_completions_are_based_on_the_p
718718
{
719719
new Command("one")
720720
{
721-
new Argument<string>().FromAmong("one-a", "one-b", "one-c")
721+
new Argument<string>().AcceptOnlyFromAmong("one-a", "one-b", "one-c")
722722
},
723723
new Command("two")
724724
{
725-
new Argument<string>().FromAmong("two-a", "two-b", "two-c")
725+
new Argument<string>().AcceptOnlyFromAmong("two-a", "two-b", "two-c")
726726
},
727727
new Command("three")
728728
{
729-
new Argument<string>().FromAmong("three-a", "three-b", "three-c")
729+
new Argument<string>().AcceptOnlyFromAmong("three-a", "three-b", "three-c")
730730
}
731731
};
732732

@@ -743,8 +743,8 @@ public void When_parsing_from_text_if_the_proximate_option_is_completed_then_com
743743
{
744744
var command = new RootCommand
745745
{
746-
new Option<string>("--framework").FromAmong("net7.0"),
747-
new Option<string>("--language").FromAmong("C#"),
746+
new Option<string>("--framework").AcceptOnlyFromAmong("net7.0"),
747+
new Option<string>("--language").AcceptOnlyFromAmong("C#"),
748748
new Option<string>("--langVersion")
749749
};
750750
var parser = new CommandLineBuilder(command).Build();
@@ -760,8 +760,8 @@ public void When_parsing_from_array_if_the_proximate_option_is_completed_then_co
760760
{
761761
var command = new RootCommand
762762
{
763-
new Option<string>("--framework").FromAmong("net7.0"),
764-
new Option<string>("--language").FromAmong("C#"),
763+
new Option<string>("--framework").AcceptOnlyFromAmong("net7.0"),
764+
new Option<string>("--language").AcceptOnlyFromAmong("C#"),
765765
new Option<string>("--langVersion")
766766
};
767767
var parser = new CommandLineBuilder(command).Build();

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ public void Option_of_boolean_defaults_to_false_when_not_specified()
375375
public void Option_of_enum_can_limit_enum_members_as_valid_values()
376376
{
377377
var option = new Option<ConsoleColor>("--color")
378-
.FromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
378+
.AcceptOnlyFromAmong(ConsoleColor.Red.ToString(), ConsoleColor.Green.ToString());
379379

380380
var result = option.Parse("--color Fuschia");
381381

src/System.CommandLine.Tests/ParseDiagramTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void Parse_result_diagram_displays_unmatched_tokens()
3434
{
3535
var command = new Command("command")
3636
{
37-
new Option<string>("-x").FromAmong("arg1", "arg2", "arg3")
37+
new Option<string>("-x").AcceptOnlyFromAmong("arg1", "arg2", "arg3")
3838
};
3939

4040
var result = command.Parse("command -x ar");

src/System.CommandLine.Tests/ParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public void Parser_root_Options_can_be_specified_multiple_times_and_their_argume
351351
public void Options_can_be_specified_multiple_times_and_their_arguments_are_collated()
352352
{
353353
var animalsOption = new Option<string[]>(new[] { "-a", "--animals" })
354-
.FromAmong("dog", "cat", "sheep");
354+
.AcceptOnlyFromAmong("dog", "cat", "sheep");
355355
var vegetablesOption = new Option<string[]>(new[] { "-v", "--vegetables" });
356356
var parser = new Parser(
357357
new Command("the-command") {

0 commit comments

Comments
 (0)