Cli.Run Method
Definition
- Namespace
- DotMake.CommandLine
- Assembly
- DotMake.CommandLine.dll
Run<TDefinition>(string[], CliSettings)
Parses a command line string array and runs the handler for the indicated command.
public static int Run<TDefinition>(string[] args = null, CliSettings settings = null)
Parameters
argsstring[]The string array typically passed to a program. This is usually the special variable
argsavailable inProgram.cs(new style with top-level statements) or the string array passed to the program'sMainmethod (old style). If not specified or null,argswill be retrieved automatically from the current process via DotMake.CommandLine.CliParser.GetArgs().settingsCliSettingsThe settings for the parser's grammar and behaviors.
Returns
- int
The exit code for the invocation.
Type Parameters
TDefinitionThe definition class for the command. A command builder for this class should be automatically generated by the source generator.
Examples
//In Program.cs, add this single line:
Cli.Run<RootCliCommand>(args);
//In Program.cs, add this single line for returning exit code:
return Cli.Run<RootCliCommand>(args);
Run<TDefinition>(string, CliSettings)
Parses a command line string value and runs the handler for the indicated command.
public static int Run<TDefinition>(string commandLine, CliSettings settings = null)
Parameters
commandLinestringThe command line string that will be split into tokens as if it had been passed on the command line. Useful for testing command line input by just specifying it as a single string.
settingsCliSettingsThe settings for the parser's grammar and behaviors.
Returns
- int
The exit code for the invocation.
Type Parameters
TDefinitionThe definition class for the command. A command builder for this class should be automatically generated by the source generator.
Examples
//In Program.cs, add this single line:
Cli.Run<RootCliCommand>("NewValueForArgument1 --option-1 NewValueForOption1");
//In Program.cs, add this single line for returning exit code:
return Cli.Run<RootCliCommand>("NewValueForArgument1 --option-1 NewValueForOption1");
Run(Delegate, CliSettings)
Parses the command line arguments and runs the indicated command as delegate.
public static int Run(Delegate cliCommandAsDelegate, CliSettings settings = null)
Parameters
cliCommandAsDelegateDelegateThe command as delegate.
([CliArgument] string argument1, bool option1) => { }
([CliArgument] string argument1, bool option1) => { return 0; }
async ([CliArgument] string argument1, bool option1) => { await Task.Delay(1000); }
MethodReferencesettingsCliSettingsThe settings for the parser's grammar and behaviors.
Returns
- int
The exit code for the invocation.
Examples
//Delegate-based model
//In Program.cs, add this simple code:
Cli.Run(([CliArgument] string argument1, bool option1) =>
{
Console.WriteLine($@"Value for {nameof(argument1)} parameter is '{argument1}'");
Console.WriteLine($@"Value for {nameof(option1)} parameter is '{option1}'");
});
//Or:
Cli.Run(Method);
void Method([CliArgument] string argument2, bool option2)
{
Console.WriteLine($@"Value for {nameof(argument2)} parameter is '{argument2}'");
Console.WriteLine($@"Value for {nameof(option2)} parameter is '{option2}'");
}
//In Program.cs, add this simple code for returning exit code:
return Cli.Run(([CliArgument] string argument1, bool option1) =>
{
Console.WriteLine($@"Value for {nameof(argument1)} parameter is '{argument1}'");
Console.WriteLine($@"Value for {nameof(option1)} parameter is '{option1}'");
return 0;
});