Skip to content
This repository has been archived by the owner on Aug 16, 2020. It is now read-only.

sanisoclem/Konsole

Repository files navigation

Konsole - CLI Builder

CodeFactor codecov AppVeyorTests Dependencies Nuget Downloads

A WebHostBuilder like experience for creating console applications.

Build status

Build server Platform SDK Version Build status
AppVeyor Windows ? Build status
Travis Linux / OS X ? ?

Nuget Packages

Name Version Framework
P3.Konsole Nuget netstandard2.0

Requirements

This library targets netstandard2.0 so it should work with net461 and netcoreapp2.x. However, it is only tested with netcoreapp2.1.

Usage

See the examples folder

var app = new KonsoleApplicationBuilder()
    .Configure(builder => builder
        .AddEnvironmentVariables("EXAMPLE_")
        .AddJsonFile("config.json", true)
        .AddUserSecrets("EXAMPLE_"))
    .ConfigureServices((col, config) => col
        .AddOptions()

        .Configure<Setting>(config.GetSection("settings"))

        .AddExampleServices())
    .ConfigureCommands((builder, config) => builder
        .DefineCommand<ParameterizedCommand, Parameters>("param")
        .DefineCommand<PrintCommand>("print")
        .DefineCommand<SettingCommand>("setting")
        .DefineCompositeCommand("nested", builder2 => builder2
                .DefineCommand<NestedCommand>("n1")
                .DefineCommand<ServiceCommand>("svc")
                .DefineCommand<NestedCommand2>("n2")
                .DefineCommand<NestedCommand3>("n3")
                .DefineCommand<NestedCommand4>("n4")
                .DefineCatchAllCommand<NestedCatchAllCommand>()))
    .Build();

try {
    var cancellation = new CancellationTokenSource();


    Console.CancelKeyPress += (sender, e) => {
        e.Cancel = true;
        cancellation.Cancel();
    };

    // -- execute command in child scope
    using (var scope = app.ServiceProvider.CreateScope()) {
        await app.ExecuteCommandAsync(args, scope.ServiceProvider, cancellation.Token);
    }
}
catch (InvalidCommandException) {
    // PRINT USAGE
    Console.Write("Invalid command");
    Environment.Exit(-1);
}
catch (Exception ex) {
    Console.WriteLine(ex.ToString());
    Environment.Exit(-2);
}

Installation

This library is available via NuGet. To install using dotnet cli:

dotnet package add P3.Konsole

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages