CliRun(Delegate, CliSettings) 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 int Run(
	Delegate cliCommandAsDelegate,
	CliSettings settings = null
)

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.

Return Value

Int32
The exit code for the invocation.

Example

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

See Also