Skip to content

Commit 4185c60

Browse files
authored
make CommandLineConfiguration mutable, remove CommandLineBuilder (#2107)
* make CommandLineConfiguration mutable, remove CommandLineBuilder * RootCommand should define Help and Version options by default * introduce CommandLineConfiguration.Parse to avoid issues when not passing the config instance for config.RootCommand.Parse(args, $config)
1 parent 22905db commit 4185c60

File tree

53 files changed

+810
-1202
lines changed

Some content is hidden

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

53 files changed

+810
-1202
lines changed

samples/HostingPlayground/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.CommandLine.NamingConventionBinder;
44
using Microsoft.Extensions.Hosting;
55
using Microsoft.Extensions.Logging;
6-
using System.CommandLine.Parsing;
76
using System.Threading.Tasks;
87
using Microsoft.Extensions.DependencyInjection;
98
using static HostingPlayground.HostingPlaygroundLogEvents;
@@ -21,19 +20,17 @@ static Task Main(string[] args) => BuildCommandLine()
2120
services.AddSingleton<IGreeter, Greeter>();
2221
});
2322
})
24-
.UseDefaults()
25-
.Build()
2623
.InvokeAsync(args);
2724

28-
private static CommandLineBuilder BuildCommandLine()
25+
private static CommandLineConfiguration BuildCommandLine()
2926
{
3027
var root = new RootCommand(@"$ dotnet run --name 'Joe'"){
3128
new Option<string>("--name"){
3229
IsRequired = true
3330
}
3431
};
3532
root.Action = CommandHandler.Create<GreeterOptions, IHost>(Run);
36-
return new CommandLineBuilder(root);
33+
return new CommandLineConfiguration(root);
3734
}
3835

3936
private static void Run(GreeterOptions options, IHost host)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ System.CommandLine.Hosting
77
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder)
88
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.HostBuilderContext context)
99
public static System.CommandLine.Command UseCommandHandler<THandler>(this System.CommandLine.Command command)
10-
public static System.CommandLine.CommandLineBuilder UseHost(this System.CommandLine.CommandLineBuilder builder, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
11-
public static System.CommandLine.CommandLineBuilder UseHost(this System.CommandLine.CommandLineBuilder builder, System.Func<System.String[],Microsoft.Extensions.Hosting.IHostBuilder> hostBuilderFactory, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
10+
public static System.CommandLine.CommandLineConfiguration UseHost(this System.CommandLine.CommandLineConfiguration builder, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
11+
public static System.CommandLine.CommandLineConfiguration UseHost(this System.CommandLine.CommandLineConfiguration builder, System.Func<System.String[],Microsoft.Extensions.Hosting.IHostBuilder> hostBuilderFactory, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
1212
public static Microsoft.Extensions.Hosting.IHostBuilder UseInvocationLifetime(this Microsoft.Extensions.Hosting.IHostBuilder host, System.CommandLine.Invocation.InvocationContext invocation, System.Action<InvocationLifetimeOptions> configureOptions = null)
1313
public class InvocationLifetime, Microsoft.Extensions.Hosting.IHostLifetime
1414
.ctor(Microsoft.Extensions.Options.IOptions<InvocationLifetimeOptions> options, Microsoft.Extensions.Hosting.IHostEnvironment environment, Microsoft.Extensions.Hosting.IHostApplicationLifetime applicationLifetime, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null)

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

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,24 @@ System.CommandLine
5555
public ParseResult Parse(System.String commandLine, CommandLineConfiguration configuration = null)
5656
public System.Void SetAction(System.Action<System.CommandLine.Invocation.InvocationContext> action)
5757
public System.Void SetAction(System.Func<System.CommandLine.Invocation.InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> action)
58-
public class CommandLineBuilder
58+
public class CommandLineConfiguration
5959
.ctor(Command rootCommand)
60-
public Command Command { get; }
6160
public System.Collections.Generic.List<Directive> Directives { get; }
62-
public CommandLineConfiguration Build()
63-
public CommandLineBuilder CancelOnProcessTermination(System.Nullable<System.TimeSpan> timeout = null)
64-
public CommandLineBuilder EnablePosixBundling(System.Boolean value = True)
65-
public CommandLineBuilder UseDefaults()
66-
public CommandLineBuilder UseEnvironmentVariableDirective()
67-
public CommandLineBuilder UseExceptionHandler(System.Func<System.Exception,System.CommandLine.Invocation.InvocationContext,System.Int32> onException = null, System.Int32 errorExitCode = 1)
68-
public CommandLineBuilder UseHelp(System.Nullable<System.Int32> maxWidth = null)
69-
public CommandLineBuilder UseHelp(System.String name, System.String[] helpAliases)
70-
public CommandLineBuilder UseParseDirective(System.Int32 errorExitCode = 1)
71-
public CommandLineBuilder UseParseErrorReporting(System.Int32 errorExitCode = 1)
72-
public CommandLineBuilder UseSuggestDirective()
73-
public CommandLineBuilder UseTokenReplacer(System.CommandLine.Parsing.TryReplaceToken replaceToken)
74-
public CommandLineBuilder UseTypoCorrections(System.Int32 maxLevenshteinDistance = 3)
75-
public CommandLineBuilder UseVersionOption()
76-
public CommandLineBuilder UseVersionOption(System.String name, System.String[] aliases)
77-
public class CommandLineConfiguration
78-
public static CommandLineBuilder CreateBuilder(Command rootCommand)
79-
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableTokenReplacement = True, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
80-
public System.Collections.Generic.IReadOnlyList<Directive> Directives { get; }
81-
public System.Boolean EnablePosixBundling { get; }
82-
public System.Boolean EnableTokenReplacement { get; }
61+
public System.Boolean EnableDefaultExceptionHandler { get; set; }
62+
public System.Boolean EnableParseErrorReporting { get; set; }
63+
public System.Boolean EnablePosixBundling { get; set; }
64+
public System.Boolean EnableTypoCorrections { get; set; }
8365
public System.IO.TextWriter Error { get; set; }
8466
public System.IO.TextWriter Output { get; set; }
67+
public System.Nullable<System.TimeSpan> ProcessTerminationTimeout { get; set; }
68+
public System.CommandLine.Parsing.TryReplaceToken ResponseFileTokenReplacer { get; set; }
8569
public Command RootCommand { get; }
8670
public System.Int32 Invoke(System.String commandLine)
8771
public System.Int32 Invoke(System.String[] args)
8872
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.String commandLine, System.Threading.CancellationToken cancellationToken = null)
8973
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.String[] args, System.Threading.CancellationToken cancellationToken = null)
74+
public ParseResult Parse(System.Collections.Generic.IReadOnlyList<System.String> args)
75+
public ParseResult Parse(System.String commandLine)
9076
public System.Void ThrowIfInvalid()
9177
public class CommandLineConfigurationException : System.Exception, System.Runtime.Serialization.ISerializable
9278
.ctor(System.String message)
@@ -164,6 +150,12 @@ System.CommandLine
164150
public System.Collections.Generic.IEnumerable<Symbol> Parents { get; }
165151
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
166152
public System.String ToString()
153+
public class VersionOption : Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
154+
.ctor()
155+
.ctor(System.String name, System.String[] aliases)
156+
public CliAction Action { get; set; }
157+
public System.Boolean Equals(System.Object obj)
158+
public System.Int32 GetHashCode()
167159
System.CommandLine.Binding
168160
public interface IValueDescriptor
169161
public System.Boolean HasDefaultValue { get; }
@@ -229,8 +221,8 @@ System.CommandLine.Help
229221
public System.IO.TextWriter Output { get; }
230222
public System.CommandLine.ParseResult ParseResult { get; }
231223
public class HelpOption : System.CommandLine.Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
232-
.ctor(System.String name, System.String[] aliases)
233224
.ctor()
225+
.ctor(System.String name, System.String[] aliases)
234226
public System.CommandLine.CliAction Action { get; set; }
235227
public System.Boolean Equals(System.Object obj)
236228
public System.Int32 GetHashCode()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void SetupOneOptWithNestedCommand()
2626
_rootCommand.Subcommands.Add(nestedCommand);
2727

2828
_testSymbolsAsString = "root_command nested_command -opt1 321";
29-
_configuration = CommandLineConfiguration.CreateBuilder(_rootCommand).UseDefaults().Build();
29+
_configuration = new CommandLineConfiguration(_rootCommand);
3030
}
3131

3232
[Benchmark]

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ public void Setup()
3131
vegetableOption
3232
};
3333

34-
_configuration = new CommandLineBuilder(eatCommand)
35-
.UseSuggestDirective()
36-
.Build();
37-
_configuration.Output = System.IO.TextWriter.Null;
34+
_configuration = new CommandLineConfiguration(eatCommand)
35+
{
36+
Directives = { new SuggestDirective() },
37+
Output = System.IO.TextWriter.Null
38+
};
3839
}
3940

4041
[Params(

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_NestedCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void SetupRootCommand()
6262
}
6363

6464
_rootCommand = rootCommand;
65-
_configuration = CommandLineConfiguration.CreateBuilder(rootCommand).UseDefaults().Build();
65+
_configuration = new CommandLineConfiguration(rootCommand);
6666
}
6767

6868
[Benchmark]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@ public void SetupParserFromOptions_Parse()
6363
}
6464

6565
[Benchmark]
66-
public ParseResult ParserFromOptions_Parse() => _testConfiguration.RootCommand.Parse(_testSymbolsAsString, _testConfiguration);
66+
public ParseResult ParserFromOptions_Parse() => _testConfiguration.Parse(_testSymbolsAsString);
6767
}
6868
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ public void SetupParserFromOptionsWithArguments_Parse()
5959
}
6060

6161
[Benchmark]
62-
public ParseResult ParserFromOptionsWithArguments_Parse() => _configuration.RootCommand.Parse(_testSymbolsAsString, _configuration);
62+
public ParseResult ParserFromOptionsWithArguments_Parse() => _configuration.Parse(_testSymbolsAsString);
6363
}
6464
}

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_ParseResult.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public Perf_Parser_ParseResult()
2121
{
2222
var option = new Option<bool>("-opt");
2323

24-
_configuration =
25-
new CommandLineBuilder(new RootCommand { option })
26-
.UseParseDirective()
27-
.Build();
24+
_configuration = new CommandLineConfiguration(new RootCommand { option })
25+
{
26+
Directives = { new ParseDirective() }
27+
};
2828
}
2929

3030
public IEnumerable<string> GenerateTestInputs()
@@ -39,12 +39,12 @@ public IEnumerable<string> GenerateTestInputs()
3939

4040
public IEnumerable<object> GenerateTestParseResults()
4141
=> GenerateTestInputs()
42-
.Select(input => new BdnParam<ParseResult>(_configuration.RootCommand.Parse(input, _configuration), input));
42+
.Select(input => new BdnParam<ParseResult>(_configuration.Parse(input), input));
4343

4444
[Benchmark]
4545
[ArgumentsSource(nameof(GenerateTestInputs))]
4646
public ParseResult ParseResult_Directives(string input)
47-
=> _configuration.RootCommand.Parse(input, _configuration);
47+
=> _configuration.Parse(input);
4848

4949
[Benchmark]
5050
[ArgumentsSource(nameof(GenerateTestParseResults))]

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BenchmarkDotNet.Attributes;
2-
using System.CommandLine.Parsing;
32
using System.Threading.Tasks;
43

54
namespace System.CommandLine.Benchmarks.CommandLine
@@ -17,10 +16,10 @@ public class Perf_Parser_Simple
1716
public Task<int> DefaultsAsync() => BuildCommand().Parse(Args).InvokeAsync();
1817

1918
[Benchmark]
20-
public int MinimalSync() => new CommandLineBuilder(BuildCommand()).Build().Invoke(Args);
19+
public int MinimalSync() => BuildMinimalConfig(BuildCommand()).Invoke(Args);
2120

2221
[Benchmark]
23-
public Task<int> MinimalAsync() => new CommandLineBuilder(BuildCommand()).Build().InvokeAsync(Args);
22+
public Task<int> MinimalAsync() => BuildMinimalConfig(BuildCommand()).InvokeAsync(Args);
2423

2524
private static RootCommand BuildCommand()
2625
{
@@ -41,5 +40,14 @@ private static RootCommand BuildCommand()
4140

4241
return command;
4342
}
43+
44+
private static CommandLineConfiguration BuildMinimalConfig(Command command)
45+
{
46+
CommandLineConfiguration config = new(command);
47+
config.Directives.Clear();
48+
config.EnableDefaultExceptionHandler = false;
49+
config.ProcessTerminationTimeout = null;
50+
return config;
51+
}
4452
}
4553
}

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_TypoCorrection.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ public Perf_Parser_TypoCorrection()
2121
{
2222
var option = new Option<bool>("--0123456789");
2323

24-
_configuration = new CommandLineBuilder(new RootCommand { option })
25-
.UseTypoCorrections()
26-
.Build();
27-
_configuration.Output = System.IO.TextWriter.Null;
24+
_configuration = new CommandLineConfiguration(new RootCommand { option })
25+
{
26+
EnableTypoCorrections = true,
27+
Output = System.IO.TextWriter.Null
28+
};
2829
}
2930

3031
public IEnumerable<BdnParam<ParseResult>> GenerateTestParseResults()
@@ -48,7 +49,7 @@ public IEnumerable<BdnParam<ParseResult>> GenerateTestParseResults()
4849
"--1023546798",
4950
"--1032546798"
5051
}
51-
.Select(opt => new BdnParam<ParseResult>(_configuration.RootCommand.Parse(opt, _configuration), opt));
52+
.Select(opt => new BdnParam<ParseResult>(_configuration.Parse(opt), opt));
5253

5354
[Benchmark]
5455
[ArgumentsSource(nameof(GenerateTestParseResults))]

src/System.CommandLine.Benchmarks/Helpers/Utils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static CommandLineConfiguration CreateConfiguration(this IEnumerable<Opti
5353
rootCommand.Add(symbol);
5454
}
5555

56-
return CommandLineConfiguration.CreateBuilder(rootCommand).UseDefaults().Build();
56+
return new CommandLineConfiguration(rootCommand);
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)