mgrosperrin - MGR.CommandLineParser.Command.Lambda 0.14.0-preview11

MGR.CommandLineParser is a multi-command line parser. It uses System.ComponentModel.DataAnnotations to declare the commands.

PM> Install-Package MGR.CommandLineParser.Command.Lambda -Version 0.14.0-preview11 -Source https://www.myget.org/F/mgrosperrin/api/v3/index.json

Copy to clipboard

> nuget.exe install MGR.CommandLineParser.Command.Lambda -Version 0.14.0-preview11 -Source https://www.myget.org/F/mgrosperrin/api/v3/index.json

Copy to clipboard

> dotnet add package MGR.CommandLineParser.Command.Lambda --version 0.14.0-preview11 --source https://www.myget.org/F/mgrosperrin/api/v3/index.json

Copy to clipboard
<PackageReference Include="MGR.CommandLineParser.Command.Lambda" Version="0.14.0-preview11" />
Copy to clipboard
source https://www.myget.org/F/mgrosperrin/api/v3/index.json

nuget MGR.CommandLineParser.Command.Lambda  ~> 0.14.0-preview11
Copy to clipboard

> choco install MGR.CommandLineParser.Command.Lambda --version 0.14.0-preview11 --source https://www.myget.org/F/mgrosperrin/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "mgrosperrin" -SourceLocation "https://www.myget.org/F/mgrosperrin/api/v2"
Install-Module -Name "MGR.CommandLineParser.Command.Lambda" -RequiredVersion "0.14.0-preview11" -Repository "mgrosperrin" -AllowPreRelease
Copy to clipboard

MGR.Commandlineparser

Build status

CI CodeQL

MyGet (preview bits):

MGR.CommandLineParser MyGet package version Number of MyGet downloads
MGR.CommandLineParser.Command.Lambda MyGet package version Number of MyGet downloads
MGR.CommandLineParser.Hosting MyGet package version Number of MyGet downloads

Nuget:

MGR.CommandLineParser NuGet package version Number of NuGet downloads
MGR.CommandLineParser.Command.Lambda NuGet package version Number of NuGet downloads
MGR.CommandLineParser.Hosting NuGet package version Number of NuGet downloads

Number of open issues Number of open PR

MGR.CommandLineParser is a multi-command line parser. It uses System.ComponentModel.DataAnnotations to declare and validate the commands.

How to use it ?

You can find more docs here

1. Install MGR.CommandLineParser

MGR.CommandLineParser is available through NuGet:

PM> Install-Package MGR.CommandLineParser

2. Declare your own commands

After adding MGR.CommandLineParser to your project, you have to define your own commands:

  • by implementing the interface MGR.CommandLineParser.Command.ICommand;
  • by extending the abstract class MGR.CommandLineParser.Command.CommandBase.

To personnalize your commands, you add some properties to your class, and implement Execute (if you directly implement ICommand), or override ExecuteCommand (if you override CommandBase).

For example:

via MGR.CommandLineParser.Command.ICommand:

public class HelloWorldCommand : ICommand
{
    [Display(ShortName = "n", Description = "The name to display")]
    [Required]
    public string Name {get; set;}

    public IList<string> Arguments {get; set;}

    public Task<int> ExecuteAsync()
    {
        Console.WriteLine("Hello world {0} !", Name);
        if(Arguments.Count > 0)
        {
            Console.WriteLine("Arguments: {0}", string.Join(",", Arguments));
        }
        return Task.FromResult(0);
    }
}

Via MGR.CommandLineParser.Command.CommandBase:

public class HelloWorldCommand : CommandBase
{
    [Display(ShortName = "n", Description = "The name to display")]
    [Required]
    public string Name {get; set;}

    protected override Task<int> ExecuteCommandAsync()
    {
        Console.WriteLine("Hello world {0} !", Name);
        if(Arguments.Count > 0)
        {
            Console.WriteLine("Arguments: {0}", string.Join(",", Arguments));
        }
        return Task.FromResult(0);
    }
}

3. Parse the command line

The simplest way to parse the command line is to call the Parse method on a IParser instance:

var parserBuilder = new ParserBuilder(new ParserOptions())
                .AddCommands(builder => builder.AddCommands<HelloWorldCommand>());
IParser parser = parserBuilder.BuildParser();
CommandResult<ICommand> commandResult = await parser.Parse(args);
if(commandResult.IsValid)
{
    return await commandResult.ExecuteAsync();
}
return commandResult.ReturnCode;

Or if you have define only one command for your program:

var parserBuilder = new ParserBuilder(new ParserOptions())
                .AddCommands(builder => builder.AddCommands<HelloWorldCommand>());
IParser parser = parserBuilder.BuildParser();
CommandResult<HelloWorldCommand> commandResult = await parser.Parse<HelloWorldCommand>(args);
if(commandResult.IsValid)
{
    return await commandResult.ExecuteAsync();
}
return commandResult.ReturnCode;

In the first case, the first item in the args parameter must be the name of the command (the name of the type, minus the suffix Command if present). In the other case, the name of the command should be omitted.

Depending on the value of args, the result will be (when not providing the type of the command to the Parse method):

Value of args Result
null return code is CommandResultCode.NoArgs (-100)
empty enumeration of string return code is CommandResultCode.NoCommandName (-200) and the global help is printed to the console
doesn't begin by HelloWorld or Help (the default help command) return code is CommandResultCode.NoCommandFound (-300) and the global help is printed to the console
HelloWorld return code is CommandResultCode.CommandParameterNotValid (-400) and the help for the HelloWorldCommand is printed to the console
HelloWorld --name Matthias or HelloWorld -n Matthias return code is CommandResultCode.Ok (0) and Hello world Matthias ! is printed to the console
  • .NETStandard 2.0
    • MGR.CommandLineParser (>= 0.14.0-preview11)
  • .NETStandard 2.0: 2.0.0.0

Owners

mgrosperrin

Authors

Matthias GROSPERRIN

Project URL

https://github.com/mgrosperrin/commandlineparser

License

MIT

Info

62 total downloads
1 downloads for version 0.14.0-preview11
Download (13.13 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
0.14.0-preview9 13.12 KB Tue, 14 Oct 2025 09:46:07 GMT 1
0.14.0-preview8 13.13 KB Tue, 07 Oct 2025 14:25:13 GMT 2
0.14.0-preview7 13.13 KB Tue, 30 Sep 2025 19:53:49 GMT 2
0.14.0-preview6 13.13 KB Fri, 19 Sep 2025 07:39:46 GMT 2
0.14.0-preview5 13.13 KB Tue, 09 Sep 2025 07:43:36 GMT 2
0.14.0-preview4 13.13 KB Tue, 26 Aug 2025 10:29:01 GMT 1
0.14.0-preview3 13.13 KB Mon, 18 Aug 2025 13:05:31 GMT 3
0.14.0-preview2 13.12 KB Sun, 17 Aug 2025 14:04:07 GMT 3
0.14.0-preview12 13.13 KB Sun, 09 Nov 2025 14:01:29 GMT 4
0.14.0-preview11 13.13 KB Sun, 09 Nov 2025 13:55:35 GMT 1
0.14.0-preview10 13.13 KB Wed, 22 Oct 2025 07:42:57 GMT 1
0.14.0-preview0 11.24 KB Fri, 08 Mar 2024 20:44:31 GMT 1
0.13.1-preview0 11.25 KB Fri, 08 Mar 2024 15:12:39 GMT 6
0.13.0-preview0 19.26 KB Thu, 29 Feb 2024 11:25:40 GMT 1
0.13.0-alpha.10 18.39 KB Sun, 10 Sep 2023 14:05:54 GMT 1
0.13.0-alpha.9 18.39 KB Thu, 07 Sep 2023 21:11:24 GMT 1
0.13.0-alpha.8 18.4 KB Thu, 07 Sep 2023 19:44:34 GMT 3
0.13.0-alpha.7 18.04 KB Mon, 21 Feb 2022 11:25:58 GMT 0
0.13.0-alpha.6 18.04 KB Mon, 10 Jan 2022 16:44:56 GMT 1
0.13.0-alpha.5 18.04 KB Fri, 26 Nov 2021 20:13:01 GMT 2
0.13.0-alpha.4 18.04 KB Mon, 01 Nov 2021 07:31:28 GMT 1
0.13.0-alpha.3 18.04 KB Mon, 13 Sep 2021 08:22:33 GMT 1
0.13.0-alpha.2 18.03 KB Thu, 09 Sep 2021 04:48:25 GMT 2
0.12.1-preview4 11.24 KB Thu, 29 Feb 2024 15:31:58 GMT 1
0.12.1-preview3 19.26 KB Tue, 27 Feb 2024 15:38:34 GMT 1
0.12.0-alpha.19 17.99 KB Thu, 18 Mar 2021 09:41:58 GMT 2
0.12.0-alpha.18 18.03 KB Thu, 31 Dec 2020 14:45:52 GMT 1
0.12.0-alpha.17 18.55 KB Mon, 30 Nov 2020 20:53:21 GMT 1
0.12.0-alpha.16 18.56 KB Fri, 13 Nov 2020 06:45:32 GMT 1
0.12.0-alpha.15 18.55 KB Thu, 20 Aug 2020 12:57:33 GMT 1
0.12.0-alpha.14 14.7 KB Sun, 12 Apr 2020 17:32:46 GMT 3
0.12.0-alpha.13 14.23 KB Sun, 05 May 2019 21:19:33 GMT 3
0.11.0-alpha.10 14.23 KB Wed, 20 Mar 2019 09:29:24 GMT 2
0.11.0-alpha.9 14.22 KB Tue, 19 Mar 2019 17:21:19 GMT 2
0.11.0-alpha.8 14.18 KB Fri, 15 Mar 2019 06:25:44 GMT 2