diff --git a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt index f4ca09d3cb..81680080eb 100644 --- a/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt +++ b/src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt @@ -8,8 +8,6 @@ System.CommandLine public System.Type ValueType { get; } public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.Object GetDefaultValue() - public ParseResult Parse(System.String commandLine) - public ParseResult Parse(System.String[] args) public System.String ToString() public class Argument : Argument, IValueDescriptor, System.CommandLine.Binding.IValueDescriptor .ctor() @@ -56,44 +54,48 @@ System.CommandLine public System.Void Add(Symbol symbol) public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) public System.Collections.Generic.IEnumerator GetEnumerator() + public ParseResult Parse(System.Collections.Generic.IReadOnlyList args, CommandLineConfiguration configuration = null) + public ParseResult Parse(System.String commandLine, CommandLineConfiguration configuration = null) public static class CommandExtensions public static System.Int32 Invoke(this Command command, System.String[] args, IConsole console = null) public static System.Int32 Invoke(this Command command, System.String commandLine, IConsole console = null) public static System.Threading.Tasks.Task InvokeAsync(this Command command, System.String[] args, IConsole console = null, System.Threading.CancellationToken cancellationToken = null) public static System.Threading.Tasks.Task InvokeAsync(this Command command, System.String commandLine, IConsole console = null, System.Threading.CancellationToken cancellationToken = null) - public static ParseResult Parse(this Command command, System.String[] args) - public static ParseResult Parse(this Command command, System.String commandLine) public class CommandLineBuilder - .ctor(Command rootCommand = null) + .ctor(Command rootCommand) public Command Command { get; } - public System.CommandLine.Parsing.Parser Build() - public static class CommandLineBuilderExtensions - public static CommandLineBuilder AddMiddleware(this CommandLineBuilder builder, System.CommandLine.Invocation.InvocationMiddleware middleware, System.CommandLine.Invocation.MiddlewareOrder order = Default) - public static CommandLineBuilder AddMiddleware(this CommandLineBuilder builder, System.Action onInvoke, System.CommandLine.Invocation.MiddlewareOrder order = Default) - public static CommandLineBuilder CancelOnProcessTermination(this CommandLineBuilder builder, System.Nullable timeout = null) - public static CommandLineBuilder EnableDirectives(this CommandLineBuilder builder, System.Boolean value = True) - public static CommandLineBuilder EnablePosixBundling(this CommandLineBuilder builder, System.Boolean value = True) - public static CommandLineBuilder RegisterWithDotnetSuggest(this CommandLineBuilder builder) - public static CommandLineBuilder UseDefaults(this CommandLineBuilder builder) - public static CommandLineBuilder UseEnvironmentVariableDirective(this CommandLineBuilder builder) - public static CommandLineBuilder UseExceptionHandler(this CommandLineBuilder builder, System.Action onException = null, System.Nullable errorExitCode = null) - public static CommandLineBuilder UseHelp(this CommandLineBuilder builder, System.Nullable maxWidth = null) - public static CommandLineBuilder UseHelp(this CommandLineBuilder builder, System.String[] helpAliases) - public static CommandLineBuilder UseHelp(this CommandLineBuilder builder, System.Action customize, System.Nullable maxWidth = null) - public static TBuilder UseHelpBuilder(this TBuilder builder, System.Func getHelpBuilder) - public static CommandLineBuilder UseParseDirective(this CommandLineBuilder builder, System.Int32 errorExitCode = 1) - public static CommandLineBuilder UseParseErrorReporting(this CommandLineBuilder builder, System.Int32 errorExitCode = 1) - public static CommandLineBuilder UseSuggestDirective(this CommandLineBuilder builder) - public static CommandLineBuilder UseTokenReplacer(this CommandLineBuilder builder, System.CommandLine.Parsing.TryReplaceToken replaceToken) - public static CommandLineBuilder UseTypoCorrections(this CommandLineBuilder builder, System.Int32 maxLevenshteinDistance = 3) - public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder) - public static CommandLineBuilder UseVersionOption(this CommandLineBuilder builder, System.String[] aliases) + public CommandLineBuilder AddMiddleware(System.CommandLine.Invocation.InvocationMiddleware middleware, System.CommandLine.Invocation.MiddlewareOrder order = Default) + public CommandLineBuilder AddMiddleware(System.Action onInvoke, System.CommandLine.Invocation.MiddlewareOrder order = Default) + public CommandLineConfiguration Build() + public CommandLineBuilder CancelOnProcessTermination(System.Nullable timeout = null) + public CommandLineBuilder EnableDirectives(System.Boolean value = True) + public CommandLineBuilder EnablePosixBundling(System.Boolean value = True) + public CommandLineBuilder RegisterWithDotnetSuggest() + public CommandLineBuilder UseDefaults() + public CommandLineBuilder UseEnvironmentVariableDirective() + public CommandLineBuilder UseExceptionHandler(System.Action onException = null, System.Nullable errorExitCode = null) + public CommandLineBuilder UseHelp(System.Nullable maxWidth = null) + public CommandLineBuilder UseHelp(System.String[] helpAliases) + public CommandLineBuilder UseHelp(System.Action customize, System.Nullable maxWidth = null) + public CommandLineBuilder UseHelpBuilder(System.Func getHelpBuilder) + 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[] aliases) public class CommandLineConfiguration + public static CommandLineBuilder CreateBuilder(Command rootCommand) .ctor(Command command, System.Boolean enablePosixBundling = True, System.Boolean enableDirectives = True, System.Boolean enableTokenReplacement = True, System.Collections.Generic.IReadOnlyList middlewarePipeline = null, System.Func helpBuilderFactory = null, System.CommandLine.Parsing.TryReplaceToken tokenReplacer = null) public System.Boolean EnableDirectives { get; } public System.Boolean EnablePosixBundling { get; } public System.Boolean EnableTokenReplacement { get; } public Command RootCommand { get; } + public System.Int32 Invoke(System.String commandLine, IConsole console = null) + public System.Int32 Invoke(System.String[] args, IConsole console = null) + public System.Threading.Tasks.Task InvokeAsync(System.String commandLine, IConsole console = null, System.Threading.CancellationToken cancellationToken = null) + public System.Threading.Tasks.Task InvokeAsync(System.String[] args, IConsole console = null, System.Threading.CancellationToken cancellationToken = null) public System.Void ThrowIfInvalid() public class CommandLineConfigurationException : System.Exception, System.Runtime.Serialization.ISerializable .ctor(System.String message) @@ -149,8 +151,6 @@ System.CommandLine public System.Collections.Generic.List> Validators { get; } public System.Type ValueType { get; } public System.Collections.Generic.IEnumerable GetCompletions(System.CommandLine.Completions.CompletionContext context) - public ParseResult Parse(System.String commandLine) - public ParseResult Parse(System.String[] args) public class Option : Option, IValueDescriptor, System.CommandLine.Binding.IValueDescriptor .ctor(System.String name, System.String description = null) .ctor(System.String[] aliases, System.String description = null) @@ -170,9 +170,9 @@ System.CommandLine public static Option AcceptExistingOnly(this Option option) public class ParseResult public System.CommandLine.Parsing.CommandResult CommandResult { get; } + public CommandLineConfiguration Configuration { get; } public System.Collections.Generic.IReadOnlyDictionary> Directives { get; } public System.Collections.Generic.IReadOnlyList Errors { get; } - public System.CommandLine.Parsing.Parser Parser { get; } public System.CommandLine.Parsing.CommandResult RootCommandResult { get; } public System.Collections.Generic.IReadOnlyList Tokens { get; } public System.Collections.Generic.IReadOnlyList UnmatchedTokens { get; } @@ -184,6 +184,8 @@ System.CommandLine public System.Collections.Generic.IEnumerable GetCompletions(System.Nullable position = null) public T GetValue(Argument argument) public T GetValue(Option option) + public System.Int32 Invoke(IConsole console = null) + public System.Threading.Tasks.Task InvokeAsync(IConsole console = null, System.Threading.CancellationToken cancellationToken = null) public System.String ToString() public class RootCommand : Command, System.Collections.Generic.IEnumerable, System.Collections.IEnumerable public static System.String ExecutableName { get; } @@ -286,7 +288,6 @@ System.CommandLine.Invocation public System.Int32 ExitCode { get; set; } public System.CommandLine.Help.HelpBuilder HelpBuilder { get; } public System.Action InvocationResult { get; set; } - public System.CommandLine.Parsing.Parser Parser { get; } public System.CommandLine.ParseResult ParseResult { get; set; } public T GetValue(Option option) public T GetValue(Argument argument) @@ -359,22 +360,11 @@ System.CommandLine.Parsing public System.String Message { get; } public SymbolResult SymbolResult { get; } public System.String ToString() - public class Parser - .ctor(System.CommandLine.CommandLineConfiguration configuration) - .ctor(System.CommandLine.Command command) - public System.CommandLine.CommandLineConfiguration Configuration { get; } - public System.CommandLine.ParseResult Parse(System.Collections.Generic.IReadOnlyList arguments, System.String rawInput = null) + public static class Parser + public static System.CommandLine.ParseResult Parse(System.CommandLine.Command command, System.Collections.Generic.IReadOnlyList args, System.CommandLine.CommandLineConfiguration configuration = null) + public static System.CommandLine.ParseResult Parse(System.CommandLine.Command command, System.String commandLine, System.CommandLine.CommandLineConfiguration configuration = null) public static class ParseResultExtensions public static System.String Diagram(this System.CommandLine.ParseResult parseResult) - public static System.Boolean HasOption(this System.CommandLine.ParseResult parseResult, System.CommandLine.Option option) - public static System.Int32 Invoke(this System.CommandLine.ParseResult parseResult, System.CommandLine.IConsole console = null) - public static System.Threading.Tasks.Task InvokeAsync(this System.CommandLine.ParseResult parseResult, System.CommandLine.IConsole console = null, System.Threading.CancellationToken cancellationToken = null) - public static class ParserExtensions - public static System.Int32 Invoke(this Parser parser, System.String commandLine, System.CommandLine.IConsole console = null) - public static System.Int32 Invoke(this Parser parser, System.String[] args, System.CommandLine.IConsole console = null) - public static System.Threading.Tasks.Task InvokeAsync(this Parser parser, System.String commandLine, System.CommandLine.IConsole console = null, System.Threading.CancellationToken cancellationToken = null) - public static System.Threading.Tasks.Task InvokeAsync(this Parser parser, System.String[] args, System.CommandLine.IConsole console = null, System.Threading.CancellationToken cancellationToken = null) - public static System.CommandLine.ParseResult Parse(this Parser parser, System.String commandLine) public abstract class SymbolResult public SymbolResult Parent { get; } public System.Collections.Generic.IReadOnlyList Tokens { get; } diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_CustomScenarios.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_CustomScenarios.cs index 4fa64d7cea..dce90160e9 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_CustomScenarios.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_CustomScenarios.cs @@ -13,23 +13,24 @@ namespace System.CommandLine.Benchmarks.CommandLine public class Perf_Parser_CustomScenarios { private string _testSymbolsAsString; - private Parser _testParser; + private Command _rootCommand; + private CommandLineConfiguration _configuration; [GlobalSetup(Target = nameof(OneOptWithNestedCommand_Parse))] public void SetupOneOptWithNestedCommand() { - var rootCommand = new Command("root_command"); + _rootCommand = new Command("root_command"); var nestedCommand = new Command("nested_command"); var option = new Option("-opt1", () => 123); nestedCommand.Options.Add(option); - rootCommand.Subcommands.Add(nestedCommand); + _rootCommand.Subcommands.Add(nestedCommand); - _testParser = new Parser(rootCommand); _testSymbolsAsString = "root_command nested_command -opt1 321"; + _configuration = CommandLineConfiguration.CreateBuilder(_rootCommand).UseDefaults().Build(); } [Benchmark] public ParseResult OneOptWithNestedCommand_Parse() - => _testParser.Parse(_testSymbolsAsString); + => _rootCommand.Parse(_testSymbolsAsString, _configuration); } } diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Directives_Suggest.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Directives_Suggest.cs index 14760434fa..b24c96fcb1 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Directives_Suggest.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Directives_Suggest.cs @@ -15,7 +15,7 @@ namespace System.CommandLine.Benchmarks.CommandLine public class Perf_Parser_Directives_Suggest { private NullConsole _nullConsole; - private Parser _testParser; + private CommandLineConfiguration _configuration; [GlobalSetup] public void Setup() @@ -34,7 +34,7 @@ public void Setup() vegetableOption }; - _testParser = new CommandLineBuilder(eatCommand) + _configuration = new CommandLineBuilder(eatCommand) .UseSuggestDirective() .Build(); } @@ -47,7 +47,7 @@ public void Setup() [Benchmark] public Task InvokeSuggest() - => _testParser.InvokeAsync(TestCmdArgs, _nullConsole); + => _configuration.InvokeAsync(TestCmdArgs, _nullConsole); } } diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_NestedCommands.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_NestedCommands.cs index e530aa8107..e5c2aecfd8 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_NestedCommands.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_NestedCommands.cs @@ -13,8 +13,8 @@ namespace System.CommandLine.Benchmarks.CommandLine public class Perf_Parser_NestedCommands { private string _testSymbolsAsString; - private Parser _testParser; private Command _rootCommand; + private CommandLineConfiguration _configuration; /// /// 1 - cmd-root @@ -45,7 +45,7 @@ private void GenerateTestNestedCommands(Command parent, int depth, int countPerL } } - [GlobalSetup(Target = nameof(ParserFromNestedCommands_Ctor))] + [GlobalSetup] public void SetupRootCommand() { string rootCommandName = "root"; @@ -62,19 +62,10 @@ public void SetupRootCommand() } _rootCommand = rootCommand; + _configuration = CommandLineConfiguration.CreateBuilder(rootCommand).UseDefaults().Build(); } - [GlobalSetup(Target = nameof(Parser_Parse))] - public void SetupParser() - { - SetupRootCommand(); - _testParser = new Parser(_rootCommand); - } - - [Benchmark] - public Parser ParserFromNestedCommands_Ctor() => new(_rootCommand); - [Benchmark] - public ParseResult Parser_Parse() => _testParser.Parse(_testSymbolsAsString); + public ParseResult Parser_Parse() => Parser.Parse(_rootCommand, _testSymbolsAsString, _configuration); } } diff --git a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs index ca6d9d3a70..67f0d5e3b4 100644 --- a/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs +++ b/src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Options_Bare.cs @@ -17,7 +17,7 @@ public class Perf_Parser_Options_Bare { private IEnumerable