Skip to content

cartercanedy/Commandment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Commandment

Let there be a good command-line interface


Commandment provides fluent builder extension methods for the new System.CommandLine API

Add to your project

$> dotnet add package Commandment --prerelease

Build a fluent and composable CLI

Program.cs

using System.CommandLine;
using Commandment;

// Create build a typed argument ...
var fooOpt = new Option<uint>("--foo", "-f")
  .WithDescription("Foo")
  .NonZero() // ... with convenient numeric validation methods
  .Required();

// Create an option that takes a variant of `MyEnum` ...
var barOpt = new Option<string>("--bar", "-b")
  .WithDescription("Bar")
  .ValidEnumVariant<MyEnum>(ignoreCase: true, showVariantsOnError: true) // ... with configurable parsing and validation
  .Optional()
  .ZeroOrOneArg()
  .WithDefaultValue(MyEnum.Baz.ToString());

// Create an option that takes a string ...
var bazOpt = new Option<string>("--baz", "-B")
  .WithDescription("Baz")
  .ValidFilePath() // ... with file/directory path validation
  .Required(true);

// Put it all together
new RootCommand("My first Commandment-based CLI")
  .AddOption(fooOpt)
  .AddOption(barOpt)
  .AddOption(bazOpt)
  .WithAsyncAction(async (result, cancelToken) => {
    Console.WriteLine($"--foo='{result.GetValue(fooOpt)}'");
    Console.WriteLine($"--bar='{result.GetValue(barOpt)}'");
    Console.WriteLine($"--baz='{result.GetValue(bazOpt)}'");

    await Task.CompletedTask;

    return 0;
  })
  .Parse(["--foo=42", "--bar=Foo", "-BProgram.cs"])
  .Invoke();

enum MyEnum {
  Foo,
  Bar,
  Baz
}

Output:

--foo='42'
--bar='Foo'
--baz='Program.cs'


Future plans:

  • Implement fluent extensions
  • Create tests
  • Create a reflection-based declarative builder API
    • Define attribute API for Command, Subcommand, and Option
    • Implement reflection-based builder

About

Fluent builder extensions for the System.CommandLine API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages