Coder Social home page Coder Social logo

nconsole's Introduction

NConsole for .NET

Build status NuGet Version

NConsole is a .NET library to parse command line arguments and execute commands. If the command name or some non-optional parameters are missing, the library automatically switches to an interactive mode where the user is prompted to enter the missing parameters. Using descriptive attributes on the command class and its parameters, a nicely formatted help page can be generated.

To use the library in your application, install the NuGet package NConsole.

Features:

  • Interactive mode when parameters are missing
  • Customizable parameter reading
  • Automatically generated help page
  • Support for optional parameters
  • Dependency injection to inject services into command instances

Usage

The usage is simple: Create an instance of the CommandLineProcessor class, register the commands and execute a command by calling the Process() method with the command line arguments.

namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var processor = new CommandLineProcessor(new ConsoleHost());
            processor.RegisterCommand<SumCommand>("sum");
            processor.Process(args);
        }
    }

    [Description("Calculates the sum of two values.")]
    public class SumCommand : IConsoleCommand
    {
        [Description("The first value.")]
        [Argument(Name = "FirstValue")]
        public int FirstValue { get; set; }

        [Description("The second value.")]
        [Argument(Name = "SecondValue", IsRequired = false)]
        public int SecondValue { get; set; } = 10;

        public async Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            host.WriteMessage((FirstValue + SecondValue).ToString());
            return null;
        }
    }
}

The command sum can now be executed using the following command line call:

MyApplication.exe sum /firstvalue:5 /secondvalue:6
Output: 11

The SecondValue has is not required (IsRequired set to ``false`). The default value is set via a C# property initializer. This parameter is not required:

MyApplication.exe sum /firstvalue:5
Output: 15

Execute the following command to show a list of all available commands:

MyApplication.exe help

nconsole's People

Contributors

ello250 avatar joaope avatar julianwb avatar nvseanianhale avatar quality-leftovers avatar ricosuter avatar slynet avatar

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.