Skip to content

Commit dbb4cd8

Browse files
committed
Merge branch 'main' into cleanupp
2 parents 00a2915 + 259d24f commit dbb4cd8

File tree

80 files changed

+1338
-1635
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1338
-1635
lines changed

src/Common/ArgumentBuilder.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal static class ArgumentBuilder
99

1010
static ArgumentBuilder()
1111
{
12-
_ctor = typeof(Argument<string>).GetConstructor(new[] { typeof(string), typeof(string) });
12+
_ctor = typeof(Argument<string>).GetConstructor(new[] { typeof(string) });
1313
}
1414

1515
public static Argument CreateArgument(Type valueType, string name = "value")
@@ -19,10 +19,10 @@ public static Argument CreateArgument(Type valueType, string name = "value")
1919
#if NET6_0_OR_GREATER
2020
var ctor = (ConstructorInfo)argumentType.GetMemberWithSameMetadataDefinitionAs(_ctor);
2121
#else
22-
var ctor = argumentType.GetConstructor(new[] { typeof(string), typeof(string) });
22+
var ctor = argumentType.GetConstructor(new[] { typeof(string) });
2323
#endif
2424

25-
return (Argument)ctor.Invoke(new object[] { name, null });
25+
return (Argument)ctor.Invoke(new object[] { name });
2626
}
2727

2828
internal static Argument CreateArgument(ParameterInfo argsParam)
@@ -32,10 +32,20 @@ internal static Argument CreateArgument(ParameterInfo argsParam)
3232
return CreateArgument(argsParam.ParameterType, argsParam.Name);
3333
}
3434

35-
var argumentType = typeof(Argument<>).MakeGenericType(argsParam.ParameterType);
35+
var argumentType = typeof(Bridge<>).MakeGenericType(argsParam.ParameterType);
3636

37-
var ctor = argumentType.GetConstructor(new[] { typeof(string), argsParam.ParameterType, typeof(string) });
37+
var ctor = argumentType.GetConstructor(new[] { typeof(string), argsParam.ParameterType });
3838

39-
return (Argument)ctor.Invoke(new object[] { argsParam.Name, argsParam.DefaultValue, null });
39+
return (Argument)ctor.Invoke(new object[] { argsParam.Name, argsParam.DefaultValue });
40+
}
41+
42+
private sealed class Bridge<T> : Argument<T>
43+
{
44+
public Bridge(string name, T defaultValue)
45+
: base(name)
46+
{
47+
// this type exists only for an easy T => Func<ArgumentResult, T> transformation
48+
DefaultValueFactory = (_) => defaultValue;
49+
}
4050
}
4151
}

src/Common/OptionBuilder.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@ internal static class OptionBuilder
1111

1212
static OptionBuilder()
1313
{
14-
_ctor = typeof(Option<string>).GetConstructor(new[] { typeof(string), typeof(string) });
14+
_ctor = typeof(Option<string>).GetConstructor(new[] { typeof(string), typeof(string[]) });
1515
}
1616

17-
public static Option CreateOption(string name, Type valueType, string description = null)
17+
internal static Option CreateOption(string name, Type valueType, string description = null)
1818
{
1919
var optionType = typeof(Option<>).MakeGenericType(valueType);
2020

2121
#if NET6_0_OR_GREATER
2222
var ctor = (ConstructorInfo)optionType.GetMemberWithSameMetadataDefinitionAs(_ctor);
2323
#else
24-
var ctor = optionType.GetConstructor(new[] { typeof(string), typeof(string) });
24+
var ctor = optionType.GetConstructor(new[] { typeof(string), typeof(string[]) });
2525
#endif
2626

27-
var option = (Option)ctor.Invoke(new object[] { name, description });
27+
var option = (Option)ctor.Invoke(new object[] { name, Array.Empty<string>() });
28+
29+
option.Description = description;
2830

2931
return option;
3032
}
3133

32-
public static Option CreateOption(string name, Type valueType, string description, Func<object> defaultValueFactory)
34+
internal static Option CreateOption(string name, Type valueType, string description, Func<object> defaultValueFactory)
3335
{
3436
if (defaultValueFactory == null)
3537
{
@@ -45,13 +47,14 @@ public static Option CreateOption(string name, Type valueType, string descriptio
4547
return option;
4648
}
4749

48-
private class Bridge<T> : Option<T>
50+
private sealed class Bridge<T> : Option<T>
4951
{
5052
public Bridge(string name, Func<object> defaultValueFactory, string description)
51-
: base(name,
52-
() => (T)defaultValueFactory(), // this type exists only for an easy Func<object> => Func<T> transformation
53-
description)
53+
: base(name)
5454
{
55+
// this type exists only for an easy Func<object> => Func<T> transformation
56+
DefaultValueFactory = (_) => (T)defaultValueFactory();
57+
Description = description;
5558
}
5659
}
5760
}

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

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,14 @@ System.CommandLine
1010
public System.Object GetDefaultValue()
1111
public System.String ToString()
1212
public class Argument<T> : Argument, IValueDescriptor<T>, System.CommandLine.Binding.IValueDescriptor
13-
.ctor()
14-
.ctor(System.String name, System.String description = null)
15-
.ctor(System.String name, Func<T> defaultValueFactory, System.String description = null)
16-
.ctor(System.String name, T defaultValue, System.String description = null)
17-
.ctor(Func<T> defaultValueFactory)
18-
.ctor(System.String name, Func<System.CommandLine.Parsing.ArgumentResult,T> parse, System.Boolean isDefault = False, System.String description = null)
19-
.ctor(Func<System.CommandLine.Parsing.ArgumentResult,T> parse, System.Boolean isDefault = False)
13+
.ctor(System.String name)
14+
public Func<System.CommandLine.Parsing.ArgumentResult,T> CustomParser { get; set; }
15+
public Func<System.CommandLine.Parsing.ArgumentResult,T> DefaultValueFactory { get; set; }
2016
public System.Boolean HasDefaultValue { get; }
2117
public System.Type ValueType { get; }
2218
public System.Void AcceptLegalFileNamesOnly()
2319
public System.Void AcceptLegalFilePathsOnly()
2420
public System.Void AcceptOnlyFromAmong(System.String[] values)
25-
public System.Void SetDefaultValue(T value)
26-
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
27-
public System.Void SetDefaultValueFactory(Func<System.CommandLine.Parsing.ArgumentResult,T> defaultValueFactory)
2821
public struct ArgumentArity : System.ValueType, System.IEquatable<ArgumentArity>
2922
public static ArgumentArity ExactlyOne { get; }
3023
public static ArgumentArity OneOrMore { get; }
@@ -42,8 +35,9 @@ System.CommandLine
4235
public static Argument<System.IO.DirectoryInfo> AcceptExistingOnly(this Argument<System.IO.DirectoryInfo> argument)
4336
public static Argument<System.IO.FileSystemInfo> AcceptExistingOnly(this Argument<System.IO.FileSystemInfo> argument)
4437
public static Argument<T> AcceptExistingOnly<T>(this Argument<T> argument)
45-
public class Command : IdentifierSymbol, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable
38+
public class Command : Symbol, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable
4639
.ctor(System.String name, System.String description = null)
40+
public System.Collections.Generic.ICollection<System.String> Aliases { get; }
4741
public System.Collections.Generic.IList<Argument> Arguments { get; }
4842
public System.Collections.Generic.IEnumerable<Symbol> Children { get; }
4943
public ICommandHandler Handler { get; set; }
@@ -75,7 +69,7 @@ System.CommandLine
7569
public CommandLineBuilder UseEnvironmentVariableDirective()
7670
public CommandLineBuilder UseExceptionHandler(System.Action<System.Exception,System.CommandLine.Invocation.InvocationContext> onException = null, System.Nullable<System.Int32> errorExitCode = null)
7771
public CommandLineBuilder UseHelp(System.Nullable<System.Int32> maxWidth = null)
78-
public CommandLineBuilder UseHelp(System.String[] helpAliases)
72+
public CommandLineBuilder UseHelp(System.String name, System.String[] helpAliases)
7973
public CommandLineBuilder UseHelp(System.Action<System.CommandLine.Help.HelpContext> customize, System.Nullable<System.Int32> maxWidth = null)
8074
public CommandLineBuilder UseHelpBuilder(System.Func<System.CommandLine.Binding.BindingContext,System.CommandLine.Help.HelpBuilder> getHelpBuilder)
8175
public CommandLineBuilder UseParseDirective(System.Int32 errorExitCode = 1)
@@ -84,7 +78,7 @@ System.CommandLine
8478
public CommandLineBuilder UseTokenReplacer(System.CommandLine.Parsing.TryReplaceToken replaceToken)
8579
public CommandLineBuilder UseTypoCorrections(System.Int32 maxLevenshteinDistance = 3)
8680
public CommandLineBuilder UseVersionOption()
87-
public CommandLineBuilder UseVersionOption(System.String[] aliases)
81+
public CommandLineBuilder UseVersionOption(System.String name, System.String[] aliases)
8882
public class CommandLineConfiguration
8983
public static CommandLineBuilder CreateBuilder(Command rootCommand)
9084
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableDirectives = True, System.Boolean enableTokenReplacement = True, System.Collections.Generic.IReadOnlyList<System.CommandLine.Invocation.InvocationMiddleware> middlewarePipeline = null, System.Func<System.CommandLine.Binding.BindingContext,System.CommandLine.Help.HelpBuilder> helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
@@ -137,32 +131,24 @@ System.CommandLine
137131
public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context)
138132
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null)
139133
public interface IConsole : System.CommandLine.IO.IStandardError, System.CommandLine.IO.IStandardIn, System.CommandLine.IO.IStandardOut
140-
public abstract class IdentifierSymbol : Symbol
141-
public System.Collections.Generic.IReadOnlyCollection<System.String> Aliases { get; }
142-
public System.Void AddAlias(System.String alias)
143-
public System.Boolean HasAlias(System.String alias)
144-
public abstract class Option : IdentifierSymbol, System.CommandLine.Binding.IValueDescriptor
134+
public abstract class Option : Symbol, System.CommandLine.Binding.IValueDescriptor
135+
public System.Collections.Generic.ICollection<System.String> Aliases { get; }
145136
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
146137
public System.Boolean AppliesToSelfAndChildren { get; set; }
147-
public System.String ArgumentHelpName { get; set; }
148138
public ArgumentArity Arity { get; set; }
149139
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
140+
public System.String HelpName { get; set; }
150141
public System.Boolean IsRequired { get; set; }
151142
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.OptionResult>> Validators { get; }
152143
public System.Type ValueType { get; }
153144
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
154145
public class Option<T> : Option, IValueDescriptor<T>, System.CommandLine.Binding.IValueDescriptor
155-
.ctor(System.String name, System.String description = null)
156-
.ctor(System.String[] aliases, System.String description = null)
157-
.ctor(System.String name, Func<System.CommandLine.Parsing.ArgumentResult,T> parseArgument, System.Boolean isDefault = False, System.String description = null)
158-
.ctor(System.String[] aliases, Func<System.CommandLine.Parsing.ArgumentResult,T> parseArgument, System.Boolean isDefault = False, System.String description = null)
159-
.ctor(System.String name, Func<T> defaultValueFactory, System.String description = null)
160-
.ctor(System.String[] aliases, Func<T> defaultValueFactory, System.String description = null)
146+
.ctor(System.String name, System.String[] aliases)
147+
public Func<System.CommandLine.Parsing.ArgumentResult,T> CustomParser { get; set; }
148+
public Func<System.CommandLine.Parsing.ArgumentResult,T> DefaultValueFactory { get; set; }
161149
public System.Void AcceptLegalFileNamesOnly()
162150
public System.Void AcceptLegalFilePathsOnly()
163151
public System.Void AcceptOnlyFromAmong(System.String[] values)
164-
public System.Void SetDefaultValue(T value)
165-
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
166152
public static class OptionValidation
167153
public static Option<System.IO.FileInfo> AcceptExistingOnly(this Option<System.IO.FileInfo> option)
168154
public static Option<System.IO.DirectoryInfo> AcceptExistingOnly(this Option<System.IO.DirectoryInfo> option)
@@ -194,7 +180,7 @@ System.CommandLine
194180
public abstract class Symbol
195181
public System.String Description { get; set; }
196182
public System.Boolean IsHidden { get; set; }
197-
public System.String Name { get; set; }
183+
public System.String Name { get; }
198184
public System.Collections.Generic.IEnumerable<Symbol> Parents { get; }
199185
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
200186
public System.String ToString()
@@ -258,9 +244,9 @@ System.CommandLine.Help
258244
public static System.String GetArgumentDefaultValue(System.CommandLine.Argument argument)
259245
public static System.String GetArgumentDescription(System.CommandLine.Argument argument)
260246
public static System.String GetArgumentUsageLabel(System.CommandLine.Argument argument)
261-
public static System.String GetIdentifierSymbolDescription(System.CommandLine.IdentifierSymbol symbol)
262-
public static System.String GetIdentifierSymbolUsageLabel(System.CommandLine.IdentifierSymbol symbol, HelpContext context)
247+
public static System.String GetCommandUsageLabel(System.CommandLine.Command symbol)
263248
public static System.Collections.Generic.IEnumerable<System.Action<HelpContext>> GetLayout()
249+
public static System.String GetOptionUsageLabel(System.CommandLine.Option symbol)
264250
public static System.Action<HelpContext> OptionsSection()
265251
public static System.Action<HelpContext> SubcommandsSection()
266252
public static System.Action<HelpContext> SynopsisSection()
@@ -350,12 +336,14 @@ System.CommandLine.Parsing
350336
public System.Collections.Generic.IEnumerable<SymbolResult> Children { get; }
351337
public System.CommandLine.Command Command { get; }
352338
public Token Token { get; }
339+
public System.String ToString()
353340
public class OptionResult : SymbolResult
354341
public System.Boolean IsImplicit { get; }
355342
public System.CommandLine.Option Option { get; }
356343
public Token Token { get; }
357344
public System.Object GetValueOrDefault()
358345
public T GetValueOrDefault<T>()
346+
public System.String ToString()
359347
public class ParseError
360348
public System.String Message { get; }
361349
public SymbolResult SymbolResult { get; }
@@ -374,7 +362,6 @@ System.CommandLine.Parsing
374362
public OptionResult FindResultFor(System.CommandLine.Option option)
375363
public T GetValue<T>(Argument<T> argument)
376364
public T GetValue<T>(Option<T> option)
377-
public System.String ToString()
378365
public class Token, System.IEquatable<Token>
379366
public static System.Boolean op_Equality(Token left, Token right)
380367
public static System.Boolean op_Inequality(Token left, Token right)

src/System.CommandLine.ApiCompatibility.Tests/LocalizationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void ErrorMessages_AreLocalized(string cultureName, string expectedMessag
2121

2222
Command command = new(CommandName)
2323
{
24-
new Argument<string>()
24+
new Argument<string>("arg")
2525
};
2626

2727
ParseResult parseResult = command.Parse(CommandName);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void SetupOneOptWithNestedCommand()
2121
{
2222
_rootCommand = new Command("root_command");
2323
var nestedCommand = new Command("nested_command");
24-
var option = new Option<int>("-opt1", () => 123);
24+
var option = new Option<int>("-opt1") { DefaultValueFactory = (_) => 123 };
2525
nestedCommand.Options.Add(option);
2626
_rootCommand.Subcommands.Add(nestedCommand);
2727

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Simple.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class Perf_Parser_Simple
2424

2525
private static RootCommand BuildCommand()
2626
{
27-
Option<bool> boolOption = new(new[] { "--bool", "-b" }, "Bool option");
28-
Option<string> stringOption = new(new[] { "--string", "-s" }, "String option");
27+
Option<bool> boolOption = new("--bool", "-b") { Description = "Bool option" };
28+
Option<string> stringOption = new("--string", "-s") { Description = "String option" };
2929

3030
RootCommand command = new()
3131
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task It_shows_help_text_based_on_XML_documentation_comments()
6363
.Contain("<args> These are arguments")
6464
.And.Contain("Arguments:");
6565
stdOut.Should()
66-
.ContainAll("--name <name>", "Specifies the name option")
66+
.ContainAll("--name", "Specifies the name option")
6767
.And.Contain("Options:");
6868
stdOut.Should()
6969
.Contain($"Description:{Environment.NewLine} Normal summary");
@@ -87,7 +87,7 @@ public async Task When_XML_documentation_comment_contains_a_para_tag_then_help_i
8787
.Contain("<args> These are arguments")
8888
.And.Contain("Arguments:");
8989
stdOut.Should()
90-
.ContainAll("--name <name>", "Specifies the name option")
90+
.ContainAll("--name", "Specifies the name option")
9191
.And.Contain("Options:");
9292
stdOut.Should()
9393
.Contain($"Description:{Environment.NewLine} Help for the test program{Environment.NewLine} More help for the test program{Environment.NewLine}");
@@ -111,7 +111,7 @@ public async Task When_XML_documentation_comment_contains_a_para_tag_and_some_te
111111
.Contain("<args> These are arguments")
112112
.And.Contain("Arguments:");
113113
stdOut.Should()
114-
.ContainAll("--name <name>", "Specifies the name option")
114+
.ContainAll("--name", "Specifies the name option")
115115
.And.Contain("Options:");
116116
stdOut.Should()
117117
.Contain($"Description:{Environment.NewLine} Help for the test program{Environment.NewLine} More help for the test program{Environment.NewLine}");
@@ -132,7 +132,7 @@ public void It_synchronously_shows_help_text_based_on_XML_documentation_comments
132132
var stdOut = _terminal.Out.ToString();
133133

134134
stdOut.Should()
135-
.ContainAll("--name <name>","name [default: Bruce]")
135+
.ContainAll("--name","name [default: Bruce]")
136136
.And.Contain("Options:");
137137
}
138138

src/System.CommandLine.DragonFruit/CommandLine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ private static bool HasAliasIgnoringPrefix(Option option, string alias)
310310
{
311311
ReadOnlySpan<char> rawAlias = alias.AsSpan(GetPrefixLength(alias));
312312

313+
if (MemoryExtensions.Equals(option.Name.AsSpan(GetPrefixLength(option.Name)), rawAlias, StringComparison.CurrentCulture))
314+
{
315+
return true;
316+
}
317+
313318
foreach (string existingAlias in option.Aliases)
314319
{
315320
if (MemoryExtensions.Equals(existingAlias.AsSpan(GetPrefixLength(existingAlias)), rawAlias, StringComparison.CurrentCulture))

0 commit comments

Comments
 (0)