Git Product home page Git Product logo

commandline's Introduction

Octopus.CommandLine

This repository contains Octopus.CommandLine, the command line parsing library used by many of Octopus apps.

Please see Contributing.

Usage

Setup references

Add a reference to Octopus.CommandLine, Newtonsoft.Json and Autofac:

dotnet add package Octopus.CommandLine
dotnet add package Autofac

Note:

  • Autofac can be swapped for your container of choice

Build your container

private static IContainer BuildContainer()
{
    var builder = new ContainerBuilder();

    //configure logging - you're already likely doing this
    Log.Logger = new LoggerConfiguration()
        .WriteTo.ColoredConsole(outputTemplate: "{Message}{NewLine}{Exception}")
        .CreateLogger();
    builder.RegisterInstance(Log.Logger).As<ILogger>().SingleInstance();

    //Sometimes you'll want to provide your own implementation of these, but for most usages
    //the default ones work fine
    builder.RegisterType<DefaultCommandOutputJsonSerializer>()
        .As<ICommandOutputJsonSerializer>();
    builder.RegisterType<CommandOutputProvider>()
        .WithParameter("applicationName", "My sample application")
        .WithParameter("applicationVersion", typeof(Startup).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion) 
        .As<ICommandOutputProvider>()
        .SingleInstance();
        
    //Register all the built-in shell completion installers
    builder.RegisterAssemblyTypes(typeof(ICommand).Assembly)
        .Where(t => t.IsAssignableTo<IShellCompletionInstaller>())
        .AsImplementedInterfaces()
        .AsSelf();
        
    //register the command locator and other in-built commands (such as help, and the auto complete commands)
    builder.RegisterType<CommandLocator>().As<ICommandLocator>();
    builder.RegisterAssemblyTypes(typeof(ICommand).Assembly).As<ICommand>().AsSelf();

    //Register any implementations in your app
    var thisAssembly = typeof(Program).GetTypeInfo().Assembly;
    builder.RegisterAssemblyTypes(thisAssembly).As<ICommand>().AsSelf();

    return builder.Build();
}

Find and execute the command in your Main method

static async Task<int> Main(string[] args)
{
    var container = BuildContainer();
    var commandLocator = container.Resolve<ICommandLocator>();
    try
    {
        var command = commandLocator.GetCommand(args);
        await command.Execute(args.Skip(1).ToArray());
        return 0;
    }
    catch (CommandException ex)
    {
        //this is a "known error" - ie, one we dont want a stack trace for
        Log.Error(ex.Message);
        return 1;
    }
    catch (Exception ex)
    {
        //this is an unknown error - log with stack trace
        Log.Error(ex, ex.Message);
        return 2;
    }
}

Create your first command

[Command("mycommand", Description = "Does the thing")]
public class MyCommand : CommandBase
{
    private readonly ICommandOutputProvider _commandOutputProvider;
    private bool dryRun;

    public MyCommand(ICommandOutputProvider commandOutputProvider) : base(commandOutputProvider)
    {
        _commandOutputProvider = commandOutputProvider;
        var options = Options.For("My Command");
        options.Add<bool>("dryRun",
            "Dry run will output the proposed changes to console, instead of writing to disk.",
            v => dryRun = true);
    }

    public override Task Execute(string[] commandLineArguments)
    {
        var remainingArguments = Options.Parse(commandLineArguments);
        if (remainingArguments.Count > 0)
            throw new CommandException("Unrecognized command arguments: " + string.Join(", ", remainingArguments));

        _commandOutputProvider.Information("This is my command");
        _commandOutputProvider.Information(dryRun
            ? "This is a dry-run; skipping doing the thing"
            : "Doing the thing");
        return Task.CompletedTask;
    }

Test it out:

PS> ./myapp.exe help

commandline's People

Contributors

dependabot[bot] avatar droyad avatar matt-richardson avatar n-lson avatar octobob avatar

Forkers

heniu75

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.