Cli.RunAsync Method
Definition
- Namespace
- DotMake.CommandLine
- Assembly
- DotMake.CommandLine.dll
RunAsync<TDefinition>(string[], CliSettings, CancellationToken)
Parses a command line string array and runs the handler asynchronously for the indicated command.
public static Task<int> RunAsync<TDefinition>(string[] args = null, CliSettings settings = null, CancellationToken cancellationToken = default)
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.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
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, to go async, add this single line:
await Cli.RunAsync<RootCliCommand>(args);
//In Program.cs, to go async, add this single line for returning exit code:
return await Cli.RunAsync<RootCliCommand>(args);
RunAsync<TDefinition>(string, CliSettings, CancellationToken)
Parses a command line string value and runs the handler asynchronously for the indicated command.
public static Task<int> RunAsync<TDefinition>(string commandLine, CliSettings settings = null, CancellationToken cancellationToken = default)
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.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
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, to go async, add this single line:
await Cli.RunAsync<RootCliCommand>("NewValueForArgument1 --option-1 NewValueForOption1");
//In Program.cs, to go async, add this single line for returning exit code:
return await Cli.RunAsync<RootCliCommand>("NewValueForArgument1 --option-1 NewValueForOption1");
RunAsync(Delegate, CliSettings, CancellationToken)
Parses the command line arguments and runs the indicated command as delegate.
public static Task<int> RunAsync(Delegate cliCommandAsDelegate, CliSettings settings = null, CancellationToken cancellationToken = default)
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.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Examples
//In Program.cs, to go async, add this simple code:
Cli.Run(async ([CliArgument] string argument1, bool option1) =>
{
Console.WriteLine($@"Value for {nameof(argument1)} parameter is '{argument1}'");
Console.WriteLine($@"Value for {nameof(option1)} parameter is '{option1}'");
await Task.Delay(1000);
});
//Or:
Cli.Run(Method);
async Task Method([CliArgument] string argument2, bool option2)
{
Console.WriteLine($@"Value for {nameof(argument2)} parameter is '{argument2}'");
Console.WriteLine($@"Value for {nameof(option2)} parameter is '{option2}'");
await Task.Delay(1000);
}
//In Program.cs, to go async, add this simple code for returning exit code:
return Cli.Run(async ([CliArgument] string argument1, bool option1) =>
{
Console.WriteLine($@"Value for {nameof(argument1)} parameter is '{argument1}'");
Console.WriteLine($@"Value for {nameof(option1)} parameter is '{option1}'");
await Task.Delay(1000);
return 0;
});