/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <paramref name="args"/> /// 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="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() { args = args ?? new string[0]; var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args); return(Execute <TApp>(context)); }
/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <paramref name="args"/> /// 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="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="InvalidOperationException">Thrown when attributes are incorrectly configured.</exception> /// <returns>The process exit code</returns> public static Task <int> ExecuteAsync <TApp>(IConsole console, params string[] args) where TApp : class { args ??= Util.EmptyArray <string>(); var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args); return(ExecuteAsync <TApp>(context)); }
/// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <paramref name="args"/> /// 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="args">The arguments</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 Task <int> ExecuteAsync <TApp>(string[] args, CancellationToken cancellationToken = default) where TApp : class { args ??= Util.EmptyArray <string>(); var context = new DefaultCommandLineContext(PhysicalConsole.Singleton, Directory.GetCurrentDirectory(), args); return(ExecuteAsync <TApp>(context, cancellationToken)); }
public static T ParseArgs <T>(params string[] args) where T : class, new() { var applicationBuilder = new ReflectionAppBuilder <T>(); var context = new DefaultCommandLineContext(NullConsole.Singleton, Directory.GetCurrentDirectory(), args); return((T)applicationBuilder.Bind(context).ParentTarget); }
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters /// <summary> /// Creates an instance of <typeparamref name="TApp"/>, matching <paramref name="args"/> /// 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="args">The arguments</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 Task <int> ExecuteAsync <TApp>(string[] args, CancellationToken cancellationToken = default) #pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters where TApp : class { args ??= Util.EmptyArray <string>(); var context = new DefaultCommandLineContext(PhysicalConsole.Singleton, Directory.GetCurrentDirectory(), args); return(ExecuteAsync <TApp>(context, cancellationToken)); }
public void BindsToSubcommandProperty() { var builder = new ReflectionAppBuilder <MasterApp>(); var context = new DefaultCommandLineContext(new TestConsole(_output), Directory.GetCurrentDirectory(), new[] { "add" }); var bound = builder.Bind(context); var add = Assert.IsType <AddCmd>(bound.Target); Assert.IsType <MasterApp>(add.Parent); }
public void ValidatesDirectories(string dirPath) { Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, dirPath)); var context = new DefaultCommandLineContext( new TestConsole(_output), AppContext.BaseDirectory, new[] { dirPath }); Assert.Equal(0, CommandLineApplication.Execute <App>(context)); }
public void ValidatesFilesRelativeToAppContext() { var exists = Path.Combine(AppContext.BaseDirectory, "exists.txt"); if (!File.Exists(exists)) { File.WriteAllText(exists, ""); } var appInBaseDir = new CommandLineApplication( new TestConsole(_output), AppContext.BaseDirectory, false); var notFoundDir = Path.Combine(AppContext.BaseDirectory, "notfound"); var appNotInBaseDir = new CommandLineApplication( new TestConsole(_output), notFoundDir, false); appInBaseDir.Argument("Files", "Files") .Accepts(v => v.ExistingFileOrDirectory()); appNotInBaseDir.Argument("Files", "Files") .Accepts(v => v.ExistingFileOrDirectory()); var success = appInBaseDir .Parse("exists.txt") .SelectedCommand .GetValidationResult(); var fails = appNotInBaseDir .Parse("exists.txt") .SelectedCommand .GetValidationResult(); Assert.Equal(ValidationResult.Success, success); Assert.NotEqual(ValidationResult.Success, fails); Assert.Equal("The file path 'exists.txt' does not exist.", fails.ErrorMessage); var console = new TestConsole(_output); var context = new DefaultCommandLineContext(console, appNotInBaseDir.WorkingDirectory, new[] { "exists.txt" }); Assert.NotEqual(0, CommandLineApplication.Execute <App>(context)); context = new DefaultCommandLineContext(console, appInBaseDir.WorkingDirectory, new[] { "exists.txt" }); Assert.Equal(0, CommandLineApplication.Execute <App>(context)); }
public void ValidatesOnlyFiles() { var filePath = "exists.txt"; var fullPath = Path.Combine(AppContext.BaseDirectory, filePath); if (!File.Exists(fullPath)) { File.WriteAllText(fullPath, ""); } var context = new DefaultCommandLineContext( new TestConsole(_output), AppContext.BaseDirectory, new[] { filePath }); Assert.Equal(0, CommandLineApplication.Execute <OnlyFile>(context)); Assert.NotEqual(0, CommandLineApplication.Execute <OnlyDir>(context)); }