/// <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="CommandParsingException">Thrown when arguments cannot be parsed correctly.</exception> /// <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, new() { ValidateContextIsNotNull(context); using (var bindResult = Bind <TApp>(context)) { if (bindResult.Command.IsShowingInformation) { return(HelpExitCode); } if (bindResult.ValidationResult != ValidationResult.Success) { return(HandleValidationError(context, bindResult)); } var invoker = ExecuteMethodInvoker.Create(bindResult.Target.GetType()); switch (invoker) { case AsyncMethodInvoker asyncInvoker: return(asyncInvoker.ExecuteAsync(context, bindResult).GetAwaiter().GetResult()); case SynchronousMethodInvoker syncInvoker: return(syncInvoker.Execute(context, bindResult)); default: throw new NotImplementedException(); } } }
/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <paramref name="args"/> /// to all attributes on the type, and then invoking a method named "Execute" if it exists. /// See <seealso cref="OptionAttribute" />, <seealso cref="ArgumentAttribute" />, /// <seealso cref="HelpOptionAttribute"/>, and <seealso cref="VersionOptionAttribute"/>. /// </summary> /// <param name="console">The console to use</param> /// <param name="args">The arguments</param> /// <typeparam name="TApp">A type that should be bound to the arguments.</typeparam> /// <exception cref="CommandParsingException">Thrown when arguments cannot be parsed correctly.</exception> /// <exception cref="InvalidOperationException">Thrown when attributes are incorrectly configured.</exception> /// <returns>The process exit code</returns> public static int Execute <TApp>(IConsole console, params string[] args) where TApp : class, new() { var bindResult = Bind <TApp>(console, args); if (bindResult.Command.IsShowingInformation) { return(0); } if (bindResult.ValidationResult != ValidationResult.Success) { return(HandleValidationError(console, bindResult)); } var invoker = ExecuteMethodInvoker.Create(bindResult.Target.GetType()); switch (invoker) { case AsyncMethodInvoker asyncInvoker: return(asyncInvoker.ExecuteAsync(console, bindResult).GetAwaiter().GetResult()); case SynchronousMethodInvoker syncInvoker: return(syncInvoker.Execute(console, bindResult)); default: throw new NotImplementedException(); } }
/// <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 async Task <int> ExecuteAsync <TApp>(CommandLineContext context) where TApp : class, new() { ValidateContextIsNotNull(context); try { using (var bindResult = Bind <TApp>(context)) { if (bindResult.Command.IsShowingInformation) { return(HelpExitCode); } if (bindResult.ValidationResult != ValidationResult.Success) { return(HandleValidationError(context, bindResult)); } var invoker = ExecuteMethodInvoker.Create(bindResult.Target.GetType()); switch (invoker) { case AsyncMethodInvoker asyncInvoker: return(await asyncInvoker.ExecuteAsync(context, bindResult)); case SynchronousMethodInvoker syncInvoker: return(syncInvoker.Execute(context, bindResult)); default: throw new NotImplementedException(); } } } catch (CommandParsingException ex) { context.Console.Error.WriteLine(ex.Message); return(ValidationErrorExitCode); } }
/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <paramref name="args"/> /// to all attributes on the type, and then invoking a method named "Execute" if it exists. /// See <seealso cref="OptionAttribute" />, <seealso cref="ArgumentAttribute" />, /// <seealso cref="HelpOptionAttribute"/>, and <seealso cref="VersionOptionAttribute"/>. /// </summary> /// <param name="console">The console to use</param> /// <param name="args">The arguments</param> /// <typeparam name="TApp">A type that should be bound to the arguments.</typeparam> /// <exception cref="CommandParsingException">Thrown when arguments cannot be parsed correctly.</exception> /// <exception cref="InvalidOperationException">Thrown when attributes are incorrectly configured.</exception> /// <returns>The process exit code</returns> public static async Task <int> ExecuteAsync <TApp>(IConsole console, params string[] args) where TApp : class, new() { var bindResult = Bind <TApp>(console, args); if (IsShowingInfo(bindResult)) { return(0); } var invoker = ExecuteMethodInvoker.Create(bindResult.Target.GetType()); switch (invoker) { case AsyncMethodInvoker asyncInvoker: return(await asyncInvoker.ExecuteAsync(console, bindResult)); case SynchronousMethodInvoker syncInvoker: return(syncInvoker.Execute(console, bindResult)); default: throw new NotImplementedException(); } }