Skip to content

Instantly share code, notes, and snippets.

@fkromer
Last active April 26, 2024 20:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fkromer/91b774e01270e675ba1a921703eea378 to your computer and use it in GitHub Desktop.
Save fkromer/91b774e01270e675ba1a921703eea378 to your computer and use it in GitHub Desktop.
framework cocona commanddotnet commandline command-line-api CommandLineUtils CliFx
maintenance github contributors 4 4 78 75 92 14
releases 12 ? 17 3 45 17
.NET foundation project no no no yes no no
user acceptance github stars 366 223 2500 1800 1500 875
github used by 22 88 11900 ? 1900 51
.NET compatibility .NET Standard 2.0+ .NET Standard 2.0+ .NET Framework 4.0+/.NET Standard .NET Standard 2.0+ .NET Framework 4.5.1/.NET Standard 1.3 .NET Standard 2.0+/.NET Core 2.0+/.NET Framework 4.6.1+
F# support ? ? yes yes ? ?
multi-level command hierarchies yes
UNIX CLI style support yes yes yes
Configuration yes
Console Lifetime yes
Interrupt handling yes yes
Logging yes
Debugging utilities yes yes
Dependency Injection yes yes yes
Shell-completion yes yes
Extension API yes ?
testing utilities yes (extensive features) yes
@drewburlingame
Copy link

For CommandDotNet, Yes for all except F# support and shell completion. It also has many other features not in the list.

@calacayir
Copy link

calacayir commented Dec 27, 2023

System.CommandLine is a very good parser but you need a lot of boilerplate code to get going and the API is hard to discover. This becomes complicated to newcomers and also you would have a lot of ugly code in your Program.cs to maintain. What if you had an easy class-based layer combined with a good parser?

DotMake Command-Line comes to the rescue!

DotMake.CommandLine is a library which provides declarative syntax for System.CommandLine via attributes for easy, fast, strongly-typed (no reflection) usage. The library includes a source generator which automagically converts your classes to CLI commands and properties to CLI options or CLI arguments.

Usage

Create a simple class like this:

using System;
using DotMake.CommandLine;

[CliCommand(Description = "A root cli command")]
public class RootCliCommand
{
    [CliOption(Description = "Description for Option1")]
    public string Option1 { get; set; } = "DefaultForOption1";
 
    [CliArgument(Description = "Description for Argument1")]
    public string Argument1 { get; set; }
 
    public void Run()
    {
        Console.WriteLine($@"Handler for '{GetType().FullName}' is run:");
        Console.WriteLine($@"Value for {nameof(Option1)} property is '{Option1}'");
        Console.WriteLine($@"Value for {nameof(Argument1)} property is '{Argument1}'");
        Console.WriteLine();
    }
}

In Program.cs, add this single line:

Cli.Run<RootCliCommand>(args);

And that's it! You now have a fully working command-line app. You just specify the name of your class which represents your root command to Cli.Run<> method and everything is wired.

If you want to go async, just use this:

await Cli.RunAsync<RootCliCommand>(args);

You get this result:

image

Easy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment