Skip to content

xo-energy/XO.Console.Cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XO.Console.Cli

GitHub Actions Status codecov

XO.Console.Cli is a command line parser and application framework.

Features

  • Simple programs are simple
  • Class or delegate command implementations
  • Default commands, nested sub-commands ("branches"), global options, greedy arguments, and more
  • Automatic help text
  • Explicit argument value separator (--) forces parsing remainder of the command line as argument values (not commands or options)
  • Built-in --cli-explain flag helps debug parsing
  • Console abstraction makes commands testable
  • Reflection free and trimmable via compile-time source generation of command and parameter type information
  • Dependency free

Packages

Documentation for each package is available in its individual README file, which is linked in the "Package" column below.

Package Description
XO.Console.Cli NuGet Version Core library (parser and application framework)
XO.Console.Cli.Extensions NuGet Version Integration with hosting, logging, and dependency injection
XO.Console.Cli.Instrumentation NuGet Version Integration with OpenTelemetry

Synopsis

using XO.Console.Cli;

return await new CommandAppBuilder()
    .ExecuteAsync(args);

[Command("greet", Description = "Prints a greeting")]
class GreetingCommand : Command<GreetingCommand.Parameters>
{
    public class Parameters : CommandParameters
    {
        [CommandArgument(0, "name", Description = "Name of the person to greet")]
        public required string Name { get; set; }

        [CommandOption("--greeting", Description = "A custom greeting (defaults to 'Hello')")]
        public string Greeting { get; set; } = "Hello";
    }

    public override int Execute(ICommandContext context, Parameters parameters, CancellationToken cancellationToken)
    {
        context.Console.Output.WriteLine($"{parameters.Greeting}, {parameters.Name}!");
        return 0;
    }
}

Acknowledgements

The design of this library's public API was partially inspired by Spectre.Console.Cli. I liked that project's development model, but I wished its implementation were different, and here we are.