CliRunAsync(Delegate, CliSettings, CancellationToken) Method

Parses the command line arguments and runs the indicated command as delegate.

Definition

Namespace: DotMake.CommandLine
Assembly: DotMake.CommandLine (in DotMake.CommandLine.dll) Version: 1.8.7
C#
public static Task<int> RunAsync(
	Delegate cliCommandAsDelegate,
	CliSettings settings = null,
	CancellationToken cancellationToken = default
)

Parameters

cliCommandAsDelegate  Delegate
The command as delegate.
C#
([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  (Optional)
The settings for the parser's grammar and behaviors.
cancellationToken  CancellationToken  (Optional)
The token to monitor for cancellation requests.

Return Value

TaskInt32
The exit code for the invocation.

Example

C#
//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);
}
C#
//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;
});

See Also