#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters /// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <see cref="CommandLineContext.Arguments"/> /// to all attributes on the type, and then invoking a method named "OnExecute" or "OnExecuteAsync" if it exists. /// </summary> /// <seealso cref="OptionAttribute" /> /// <seealso cref="ArgumentAttribute" /> /// <seealso cref="HelpOptionAttribute"/> /// <seealso cref="VersionOptionAttribute"/> /// <param name="context">The execution context.</param> /// <param name="cancellationToken"></param> /// <typeparam name="TApp">A type that should be bound to the arguments.</typeparam> /// <exception cref="InvalidOperationException">Thrown when attributes are incorrectly configured.</exception> /// <returns>The process exit code</returns> public static async Task <int> ExecuteAsync <TApp>(CommandLineContext context, CancellationToken cancellationToken = default) #pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters where TApp : class { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Arguments == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.Arguments)); } if (context.WorkingDirectory == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.WorkingDirectory)); } if (context.Console == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.Console)); } try { using var app = new CommandLineApplication <TApp>(); app.SetContext(context); app.Conventions.UseDefaultConventions(); return(await app.ExecuteAsync(context.Arguments, cancellationToken)); } catch (OperationCanceledException) { return(s_exitCodeOperationCanceled); } catch (CommandParsingException ex) { context.Console.Error.WriteLine(ex.Message); if (ex is UnrecognizedCommandParsingException uex && uex.NearestMatches.Any()) { context.Console.Error.WriteLine(); context.Console.Error.WriteLine("Did you mean this?"); context.Console.Error.WriteLine(" " + uex.NearestMatches.First()); } return(ValidationErrorExitCode); } }
/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <see cref="CommandLineContext.Arguments"/> /// to all attributes on the type, and then invoking a method named "OnExecute" or "OnExecuteAsync" if it exists. /// See <seealso cref="OptionAttribute" />, <seealso cref="ArgumentAttribute" />, /// <seealso cref="HelpOptionAttribute"/>, and <seealso cref="VersionOptionAttribute"/>. /// </summary> /// <param name="context">The execution context.</param> /// <typeparam name="TApp">A type that should be bound to the arguments.</typeparam> /// <exception cref="InvalidOperationException">Thrown when attributes are incorrectly configured.</exception> /// <returns>The process exit code</returns> public static int Execute <TApp>(CommandLineContext context) where TApp : class { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Arguments == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.Arguments)); } if (context.WorkingDirectory == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.WorkingDirectory)); } if (context.Console == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.Console)); } try { using (var app = new CommandLineApplication <TApp>()) { app.SetContext(context); app.Conventions.UseDefaultConventions(); return(app.Execute(context.Arguments)); } } catch (CommandParsingException ex) { context.Console.Error.WriteLine(ex.Message); if (ex is UnrecognizedCommandParsingException uex && !string.IsNullOrEmpty(uex.NearestMatch)) { context.Console.Error.WriteLine(); context.Console.Error.WriteLine("Did you mean this?"); context.Console.Error.WriteLine(" " + uex.NearestMatch); } return(ValidationErrorExitCode); } }
public BindResult Bind(CommandLineContext context) { EnsureInitialized(); App.SetContext(context); var processor = new CommandLineProcessor(App, context.Arguments); var command = processor.Process(); var validationResult = command.GetValidationResult(); command.Invoke(); _bindResult.Command = command; _bindResult.ValidationResult = validationResult; _bindResult.ParentTarget = _target; return(_bindResult); }
/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <see cref="CommandLineContext.Arguments"/> /// to all attributes on the type, and then invoking a method named "OnExecute" or "OnExecuteAsync" if it exists. /// See <seealso cref="OptionAttribute" />, <seealso cref="ArgumentAttribute" />, /// <seealso cref="HelpOptionAttribute"/>, and <seealso cref="VersionOptionAttribute"/>. /// </summary> /// <param name="context">The execution context.</param> /// <typeparam name="TApp">A type that should be bound to the arguments.</typeparam> /// <exception cref="InvalidOperationException">Thrown when attributes are incorrectly configured.</exception> /// <returns>The process exit code</returns> public static int Execute <TApp>(CommandLineContext context) where TApp : class { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (context.Arguments == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.Arguments)); } if (context.WorkingDirectory == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.WorkingDirectory)); } if (context.Console == null) { throw new ArgumentNullException(nameof(context) + "." + nameof(context.Console)); } try { using (var app = new CommandLineApplication <TApp>()) { app.SetContext(context); app.Conventions.UseDefaultConventions(); return(app.Execute(context.Arguments)); } } catch (Exception ex) when(ex is CommandParsingException || ex is FormatException) { context.Console.Error.WriteLine(ex.Message); return(ValidationErrorExitCode); } }