mgrosperrin - MGR.CommandLineParser 0.14.0-preview3

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

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

Copy to clipboard

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

Copy to clipboard

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

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

nuget MGR.CommandLineParser  ~> 0.14.0-preview3
Copy to clipboard

> choco install MGR.CommandLineParser --version 0.14.0-preview3 --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" -RequiredVersion "0.14.0-preview3" -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
    • Microsoft.Extensions.DependencyInjection (>= 8.0.0)
    • Microsoft.Extensions.DependencyModel (>= 8.0.0)
    • Microsoft.Extensions.Logging (>= 8.0.0)
    • Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
    • System.ComponentModel.Annotations (>= 5.0.0)
  • .NETStandard 2.0: 2.0.0.0

Owners

mgrosperrin

Authors

Matthias GROSPERRIN

Project URL

https://github.com/mgrosperrin/commandlineparser

License

MIT

Info

3047 total downloads
46 downloads for version 0.14.0-preview3
Download (49.78 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
0.14.0-preview9 49.79 KB Tue, 14 Oct 2025 09:46:07 GMT 26
0.14.0-preview8 49.78 KB Tue, 07 Oct 2025 14:25:13 GMT 26
0.14.0-preview7 49.78 KB Tue, 30 Sep 2025 19:53:48 GMT 32
0.14.0-preview6 49.78 KB Fri, 19 Sep 2025 07:39:45 GMT 42
0.14.0-preview5 49.79 KB Tue, 09 Sep 2025 07:43:35 GMT 47
0.14.0-preview4 49.78 KB Tue, 26 Aug 2025 10:29:00 GMT 38
0.14.0-preview3 49.78 KB Mon, 18 Aug 2025 13:05:31 GMT 46
0.14.0-preview2 49.78 KB Sun, 17 Aug 2025 14:04:06 GMT 42
0.14.0-preview12 49.8 KB Sun, 09 Nov 2025 14:01:29 GMT 11
0.14.0-preview11 49.79 KB Sun, 09 Nov 2025 13:55:34 GMT 14
0.14.0-preview10 49.79 KB Wed, 22 Oct 2025 07:42:57 GMT 20
0.14.0-preview0 55.76 KB Fri, 08 Mar 2024 20:44:31 GMT 46
0.13.1-preview0 55.37 KB Fri, 08 Mar 2024 15:12:39 GMT 43
0.13.0-preview0 76.84 KB Thu, 29 Feb 2024 11:25:39 GMT 47
0.13.0-alpha.10 76.32 KB Sun, 10 Sep 2023 14:05:51 GMT 53
0.13.0-alpha.9 76.05 KB Thu, 07 Sep 2023 21:11:21 GMT 43
0.13.0-alpha.8 76.09 KB Thu, 07 Sep 2023 19:44:32 GMT 32
0.13.0-alpha.7 75.49 KB Mon, 21 Feb 2022 11:25:56 GMT 37
0.13.0-alpha.6 75.49 KB Mon, 10 Jan 2022 16:44:54 GMT 37
0.13.0-alpha.5 75.48 KB Fri, 26 Nov 2021 20:12:59 GMT 42
0.13.0-alpha.4 75.49 KB Mon, 01 Nov 2021 07:31:25 GMT 43
0.13.0-alpha.3 75.49 KB Mon, 13 Sep 2021 08:22:31 GMT 33
0.13.0-alpha.2 75.49 KB Thu, 09 Sep 2021 04:48:23 GMT 43
0.12.1-preview4 55.34 KB Thu, 29 Feb 2024 15:31:57 GMT 48
0.12.1-preview3 76.85 KB Tue, 27 Feb 2024 15:38:33 GMT 52
0.12.0-alpha.19 75.42 KB Thu, 18 Mar 2021 09:41:55 GMT 43
0.12.0-alpha.18 75.76 KB Thu, 31 Dec 2020 14:45:50 GMT 46
0.12.0-alpha.17 71.33 KB Mon, 30 Nov 2020 20:53:19 GMT 44
0.12.0-alpha.16 71.42 KB Fri, 13 Nov 2020 06:45:30 GMT 36
0.12.0-alpha.15 71.42 KB Thu, 20 Aug 2020 12:57:31 GMT 48
0.12.0-alpha.14 67.6 KB Sun, 12 Apr 2020 17:32:44 GMT 45
0.12.0-alpha.13 64.96 KB Sun, 05 May 2019 21:19:31 GMT 42
0.11.0-alpha.10 65.52 KB Wed, 20 Mar 2019 09:29:22 GMT 46
0.11.0-alpha.9 65.51 KB Tue, 19 Mar 2019 17:21:17 GMT 43
0.11.0-alpha.8 63.28 KB Fri, 15 Mar 2019 06:25:42 GMT 41
0.10.0-alpha9 58.76 KB Mon, 07 May 2018 13:41:27 GMT 47
0.10.0-alpha7 58.38 KB Sat, 05 May 2018 18:04:52 GMT 42
0.10.0-alpha5 58.38 KB Wed, 02 May 2018 15:24:49 GMT 43
0.10.0-alpha3 58.37 KB Tue, 01 May 2018 18:29:54 GMT 43
0.10.0-alpha19 62.16 KB Mon, 30 Jul 2018 07:33:11 GMT 55
0.10.0-alpha18 62.16 KB Sat, 26 May 2018 08:31:32 GMT 40
0.10.0-alpha17 60.18 KB Tue, 22 May 2018 08:44:00 GMT 42
0.10.0-alpha16 60.15 KB Wed, 16 May 2018 13:19:18 GMT 48
0.10.0-alpha15 60.15 KB Tue, 15 May 2018 11:35:14 GMT 48
0.10.0-alpha14 59.79 KB Sun, 13 May 2018 12:57:00 GMT 46
0.10.0-alpha13 60.5 KB Mon, 07 May 2018 17:04:54 GMT 48
0.10.0-alpha1 97.51 KB Mon, 21 Aug 2017 10:10:09 GMT 45
0.9.0-alpha86 97.51 KB Thu, 17 Aug 2017 09:04:46 GMT 40
0.9.0-alpha84 97.48 KB Sat, 29 Jul 2017 20:35:14 GMT 31
0.9.0-alpha79 96.71 KB Sun, 28 May 2017 07:47:47 GMT 42
0.9.0-alpha77 95.24 KB Sat, 27 May 2017 14:55:06 GMT 51
0.9.0-alpha75 95.27 KB Fri, 26 May 2017 16:37:22 GMT 45
0.9.0-alpha67 95.21 KB Fri, 26 May 2017 14:36:15 GMT 44
0.9.0-alpha65 95.3 KB Fri, 26 May 2017 14:06:23 GMT 43
0.9.0-alpha64 95.25 KB Thu, 25 May 2017 18:18:57 GMT 47
0.9.0-alpha61 93.81 KB Thu, 25 May 2017 15:17:37 GMT 49
0.9.0-alpha11 97.46 KB Mon, 21 Aug 2017 09:54:08 GMT 39
0.9.0-alpha10 97.49 KB Sat, 19 Aug 2017 14:10:00 GMT 45
0.7.0-alpha56 93.57 KB Sat, 06 May 2017 11:23:20 GMT 42
0.7.0-alpha53 93.62 KB Sun, 30 Apr 2017 14:48:27 GMT 37
0.7.0-alpha51 93.04 KB Sat, 29 Apr 2017 19:39:53 GMT 41
0.7.0-alpha44 91.91 KB Mon, 10 Apr 2017 07:54:26 GMT 42
0.7.0-alpha42 90.75 KB Sun, 09 Apr 2017 16:46:02 GMT 54
0.7.0-alpha39 91.01 KB Sun, 09 Apr 2017 10:50:49 GMT 35
0.7.0-alpha35 92.13 KB Sun, 25 Dec 2016 13:46:28 GMT 43
0.7.0-alpha24 92.04 KB Sat, 15 Oct 2016 17:45:26 GMT 34
0.7.0-alpha21 92 KB Mon, 26 Sep 2016 08:09:56 GMT 37
0.7.0-alpha17 92.01 KB Sun, 04 Sep 2016 17:08:57 GMT 49
0.7.0-alpha15 91.39 KB Fri, 02 Sep 2016 20:02:35 GMT 47
0.7.0-alpha13 91.35 KB Fri, 02 Sep 2016 19:32:08 GMT 32
0.7.0-alpha10 91.13 KB Sun, 21 Aug 2016 16:09:07 GMT 43
0.7.0-alpha 91.15 KB Sun, 21 Aug 2016 15:59:28 GMT 34
0.7.0-alpaha1 45.33 KB Sat, 30 Jan 2016 13:05:36 GMT 41
0.6.1 90.79 KB Wed, 06 Jul 2016 05:31:47 GMT 36