Table of Contents

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

args string[]

The string array typically passed to a program. This is usually the special variable args available in Program.cs (new style with top-level statements) or the string array passed to the program's Main method (old style). If not specified or null, args will be retrieved automatically from the current process via DotMake.CommandLine.CliParser.GetArgs().

settings CliSettings

The settings for the parser's grammar and behaviors.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<int>

The exit code for the invocation.

Type Parameters

TDefinition

The 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

commandLine string

The 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.

settings CliSettings

The settings for the parser's grammar and behaviors.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<int>

The exit code for the invocation.

Type Parameters

TDefinition

The 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

cliCommandAsDelegate Delegate

The 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); }

MethodReference
settings CliSettings

The settings for the parser's grammar and behaviors.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<int>

The exit code for the invocation.

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;
});