Skip to content

make CommandLineConfiguration mutable, remove CommandLineBuilder #2107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions samples/HostingPlayground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.CommandLine.NamingConventionBinder;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using static HostingPlayground.HostingPlaygroundLogEvents;
Expand All @@ -21,19 +20,17 @@ static Task Main(string[] args) => BuildCommandLine()
services.AddSingleton<IGreeter, Greeter>();
});
})
.UseDefaults()
.Build()
.InvokeAsync(args);

private static CommandLineBuilder BuildCommandLine()
private static CommandLineConfiguration BuildCommandLine()
{
var root = new RootCommand(@"$ dotnet run --name 'Joe'"){
new Option<string>("--name"){
IsRequired = true
}
};
root.Action = CommandHandler.Create<GreeterOptions, IHost>(Run);
return new CommandLineBuilder(root);
return new CommandLineConfiguration(root);
}

private static void Run(GreeterOptions options, IHost host)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ System.CommandLine.Hosting
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder)
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.HostBuilderContext context)
public static System.CommandLine.Command UseCommandHandler<THandler>(this System.CommandLine.Command command)
public static System.CommandLine.CommandLineBuilder UseHost(this System.CommandLine.CommandLineBuilder builder, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
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)
public static System.CommandLine.CommandLineConfiguration UseHost(this System.CommandLine.CommandLineConfiguration builder, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
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)
public static Microsoft.Extensions.Hosting.IHostBuilder UseInvocationLifetime(this Microsoft.Extensions.Hosting.IHostBuilder host, System.CommandLine.Invocation.InvocationContext invocation, System.Action<InvocationLifetimeOptions> configureOptions = null)
public class InvocationLifetime, Microsoft.Extensions.Hosting.IHostLifetime
.ctor(Microsoft.Extensions.Options.IOptions<InvocationLifetimeOptions> options, Microsoft.Extensions.Hosting.IHostEnvironment environment, Microsoft.Extensions.Hosting.IHostApplicationLifetime applicationLifetime, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,24 @@ System.CommandLine
public ParseResult Parse(System.String commandLine, CommandLineConfiguration configuration = null)
public System.Void SetAction(System.Action<System.CommandLine.Invocation.InvocationContext> action)
public System.Void SetAction(System.Func<System.CommandLine.Invocation.InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> action)
public class CommandLineBuilder
public class CommandLineConfiguration
.ctor(Command rootCommand)
public Command Command { get; }
public System.Collections.Generic.List<Directive> Directives { get; }
public CommandLineConfiguration Build()
public CommandLineBuilder CancelOnProcessTermination(System.Nullable<System.TimeSpan> timeout = null)
public CommandLineBuilder EnablePosixBundling(System.Boolean value = True)
public CommandLineBuilder UseDefaults()
public CommandLineBuilder UseEnvironmentVariableDirective()
public CommandLineBuilder UseExceptionHandler(System.Func<System.Exception,System.CommandLine.Invocation.InvocationContext,System.Int32> onException = null, System.Int32 errorExitCode = 1)
public CommandLineBuilder UseHelp(System.Nullable<System.Int32> maxWidth = null)
public CommandLineBuilder UseHelp(System.String name, System.String[] helpAliases)
public CommandLineBuilder UseParseDirective(System.Int32 errorExitCode = 1)
public CommandLineBuilder UseParseErrorReporting(System.Int32 errorExitCode = 1)
public CommandLineBuilder UseSuggestDirective()
public CommandLineBuilder UseTokenReplacer(System.CommandLine.Parsing.TryReplaceToken replaceToken)
public CommandLineBuilder UseTypoCorrections(System.Int32 maxLevenshteinDistance = 3)
public CommandLineBuilder UseVersionOption()
public CommandLineBuilder UseVersionOption(System.String name, System.String[] aliases)
public class CommandLineConfiguration
public static CommandLineBuilder CreateBuilder(Command rootCommand)
.ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableTokenReplacement = True, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null)
public System.Collections.Generic.IReadOnlyList<Directive> Directives { get; }
public System.Boolean EnablePosixBundling { get; }
public System.Boolean EnableTokenReplacement { get; }
public System.Boolean EnableDefaultExceptionHandler { get; set; }
public System.Boolean EnableParseErrorReporting { get; set; }
public System.Boolean EnablePosixBundling { get; set; }
public System.Boolean EnableTypoCorrections { get; set; }
public System.IO.TextWriter Error { get; set; }
public System.IO.TextWriter Output { get; set; }
public System.Nullable<System.TimeSpan> ProcessTerminationTimeout { get; set; }
public System.CommandLine.Parsing.TryReplaceToken ResponseFileTokenReplacer { get; set; }
public Command RootCommand { get; }
public System.Int32 Invoke(System.String commandLine)
public System.Int32 Invoke(System.String[] args)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.String commandLine, System.Threading.CancellationToken cancellationToken = null)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.String[] args, System.Threading.CancellationToken cancellationToken = null)
public ParseResult Parse(System.Collections.Generic.IReadOnlyList<System.String> args)
public ParseResult Parse(System.String commandLine)
public System.Void ThrowIfInvalid()
public class CommandLineConfigurationException : System.Exception, System.Runtime.Serialization.ISerializable
.ctor(System.String message)
Expand Down Expand Up @@ -164,6 +150,12 @@ System.CommandLine
public System.Collections.Generic.IEnumerable<Symbol> Parents { get; }
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
public System.String ToString()
public class VersionOption : Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
.ctor()
.ctor(System.String name, System.String[] aliases)
public CliAction Action { get; set; }
public System.Boolean Equals(System.Object obj)
public System.Int32 GetHashCode()
System.CommandLine.Binding
public interface IValueDescriptor
public System.Boolean HasDefaultValue { get; }
Expand Down Expand Up @@ -229,8 +221,8 @@ System.CommandLine.Help
public System.IO.TextWriter Output { get; }
public System.CommandLine.ParseResult ParseResult { get; }
public class HelpOption : System.CommandLine.Option<System.Boolean>, System.CommandLine.Binding.IValueDescriptor
.ctor(System.String name, System.String[] aliases)
.ctor()
.ctor(System.String name, System.String[] aliases)
public System.CommandLine.CliAction Action { get; set; }
public System.Boolean Equals(System.Object obj)
public System.Int32 GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void SetupOneOptWithNestedCommand()
_rootCommand.Subcommands.Add(nestedCommand);

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

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ public void Setup()
vegetableOption
};

_configuration = new CommandLineBuilder(eatCommand)
.UseSuggestDirective()
.Build();
_configuration.Output = System.IO.TextWriter.Null;
_configuration = new CommandLineConfiguration(eatCommand)
{
Directives = { new SuggestDirective() },
Output = System.IO.TextWriter.Null
};
}

[Params(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void SetupRootCommand()
}

_rootCommand = rootCommand;
_configuration = CommandLineConfiguration.CreateBuilder(rootCommand).UseDefaults().Build();
_configuration = new CommandLineConfiguration(rootCommand);
}

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public void SetupParserFromOptions_Parse()
}

[Benchmark]
public ParseResult ParserFromOptions_Parse() => _testConfiguration.RootCommand.Parse(_testSymbolsAsString, _testConfiguration);
public ParseResult ParserFromOptions_Parse() => _testConfiguration.Parse(_testSymbolsAsString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ public void SetupParserFromOptionsWithArguments_Parse()
}

[Benchmark]
public ParseResult ParserFromOptionsWithArguments_Parse() => _configuration.RootCommand.Parse(_testSymbolsAsString, _configuration);
public ParseResult ParserFromOptionsWithArguments_Parse() => _configuration.Parse(_testSymbolsAsString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public Perf_Parser_ParseResult()
{
var option = new Option<bool>("-opt");

_configuration =
new CommandLineBuilder(new RootCommand { option })
.UseParseDirective()
.Build();
_configuration = new CommandLineConfiguration(new RootCommand { option })
{
Directives = { new ParseDirective() }
};
}

public IEnumerable<string> GenerateTestInputs()
Expand All @@ -39,12 +39,12 @@ public IEnumerable<string> GenerateTestInputs()

public IEnumerable<object> GenerateTestParseResults()
=> GenerateTestInputs()
.Select(input => new BdnParam<ParseResult>(_configuration.RootCommand.Parse(input, _configuration), input));
.Select(input => new BdnParam<ParseResult>(_configuration.Parse(input), input));

[Benchmark]
[ArgumentsSource(nameof(GenerateTestInputs))]
public ParseResult ParseResult_Directives(string input)
=> _configuration.RootCommand.Parse(input, _configuration);
=> _configuration.Parse(input);

[Benchmark]
[ArgumentsSource(nameof(GenerateTestParseResults))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BenchmarkDotNet.Attributes;
using System.CommandLine.Parsing;
using System.Threading.Tasks;

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

[Benchmark]
public int MinimalSync() => new CommandLineBuilder(BuildCommand()).Build().Invoke(Args);
public int MinimalSync() => BuildMinimalConfig(BuildCommand()).Invoke(Args);

[Benchmark]
public Task<int> MinimalAsync() => new CommandLineBuilder(BuildCommand()).Build().InvokeAsync(Args);
public Task<int> MinimalAsync() => BuildMinimalConfig(BuildCommand()).InvokeAsync(Args);

private static RootCommand BuildCommand()
{
Expand All @@ -41,5 +40,14 @@ private static RootCommand BuildCommand()

return command;
}

private static CommandLineConfiguration BuildMinimalConfig(Command command)
{
CommandLineConfiguration config = new(command);
config.Directives.Clear();
config.EnableDefaultExceptionHandler = false;
config.ProcessTerminationTimeout = null;
return config;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public Perf_Parser_TypoCorrection()
{
var option = new Option<bool>("--0123456789");

_configuration = new CommandLineBuilder(new RootCommand { option })
.UseTypoCorrections()
.Build();
_configuration.Output = System.IO.TextWriter.Null;
_configuration = new CommandLineConfiguration(new RootCommand { option })
{
EnableTypoCorrections = true,
Output = System.IO.TextWriter.Null
};
}

public IEnumerable<BdnParam<ParseResult>> GenerateTestParseResults()
Expand All @@ -48,7 +49,7 @@ public IEnumerable<BdnParam<ParseResult>> GenerateTestParseResults()
"--1023546798",
"--1032546798"
}
.Select(opt => new BdnParam<ParseResult>(_configuration.RootCommand.Parse(opt, _configuration), opt));
.Select(opt => new BdnParam<ParseResult>(_configuration.Parse(opt), opt));

[Benchmark]
[ArgumentsSource(nameof(GenerateTestParseResults))]
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Benchmarks/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static CommandLineConfiguration CreateConfiguration(this IEnumerable<Opti
rootCommand.Add(symbol);
}

return CommandLineConfiguration.CreateBuilder(rootCommand).UseDefaults().Build();
return new CommandLineConfiguration(rootCommand);
}
}
}
Loading