Giter Site home page Giter Site logo

mgrosperrin / commandlineparser Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 5.0 2.69 MB

MGR.CommandLineParser is a multi-command line parser. It uses System.ComponentModel.DataAnnotations to declare and validate the commands.

Home Page: http://mgrosperrin.github.io/commandlineparser/

License: MIT License

C# 99.84% PowerShell 0.16%
commandlineparser

commandlineparser's Introduction

MGR.Commandlineparser

Build status

dev Build status
master Build status

MyGet (preview bits):

MGR.CommandLineParser MyGet package version Number of MyGet downloads
MGR.CommandLineParser.Command.Lambda MyGet package version Number of MyGet downloads
MGR.CommandLineParser.Hosting MyGet package version Number of MyGet downloads

Nuget:

MGR.CommandLineParser NuGet package version Number of NuGet downloads
MGR.CommandLineParser.Command.Lambda NuGet package version Number of NuGet downloads
MGR.CommandLineParser.Hosting NuGet package version Number of NuGet downloads

Number of open issues Number of open PR

MGR.CommandLineParser is a multi-command line parser. It uses System.ComponentModel.DataAnnotations to declare and validate the commands.

How to use it ?

You can find more docs here

1. Install MGR.CommandLineParser

MGR.CommandLineParser is available through NuGet:

PM> Install-Package MGR.CommandLineParser

2. Declare your own commands

After adding MGR.CommandLineParser to your project, you have to define your own commands:

  • by implementing the interface MGR.CommandLineParser.Command.ICommand;
  • by extending the abstract class MGR.CommandLineParser.Command.CommandBase.

To personnalize your commands, you add some properties to your class, and implement Execute (if you directly implement ICommand), or override ExecuteCommand (if you override CommandBase).

For example:

via MGR.CommandLineParser.Command.ICommand:

public class HelloWorldCommand : ICommand
{
    [Display(ShortName = "n", Description = "The name to display")]
    [Required]
    public string Name {get; set;}

    public IList<string> Arguments {get; set;}

    public Task<int> ExecuteAsync()
    {
        Console.WriteLine("Hello world {0} !", Name);
        if(Arguments.Count > 0)
        {
            Console.WriteLine("Arguments: {0}", string.Join(",", Arguments));
        }
        return Task.FromResult(0);
    }
}

Via MGR.CommandLineParser.Command.CommandBase:

public class HelloWorldCommand : CommandBase
{
    [Display(ShortName = "n", Description = "The name to display")]
    [Required]
    public string Name {get; set;}

    protected override Task<int> ExecuteCommandAsync()
    {
        Console.WriteLine("Hello world {0} !", Name);
        if(Arguments.Count > 0)
        {
            Console.WriteLine("Arguments: {0}", string.Join(",", Arguments));
        }
        return Task.FromResult(0);
    }
}

3. Parse the command line

The simplest way to parse the command line is to call the Parse method on a IParser instance:

var parserBuilder = new ParserBuilder(new ParserOptions())
                .AddCommands(builder => builder.AddCommands<HelloWorldCommand>());
IParser parser = parserBuilder.BuildParser();
CommandResult<ICommand> commandResult = await parser.Parse(args);
if(commandResult.IsValid)
{
    return await commandResult.ExecuteAsync();
}
return commandResult.ReturnCode;

Or if you have define only one command for your program:

var parserBuilder = new ParserBuilder(new ParserOptions())
                .AddCommands(builder => builder.AddCommands<HelloWorldCommand>());
IParser parser = parserBuilder.BuildParser();
CommandResult<HelloWorldCommand> commandResult = await parser.Parse<HelloWorldCommand>(args);
if(commandResult.IsValid)
{
    return await commandResult.ExecuteAsync();
}
return commandResult.ReturnCode;

In the first case, the first item in the args parameter must be the name of the command (the name of the type, minus the suffix Command if present). In the other case, the name of the command should be omitted.

Depending on the value of args, the result will be (when not providing the type of the command to the Parse method):

Value of args Result
null return code is CommandResultCode.NoArgs (-100)
empty enumeration of string return code is CommandResultCode.NoCommandName (-200) and the global help is printed to the console
doesn't begin by HelloWorld or Help (the default help command) return code is CommandResultCode.NoCommandFound (-300) and the global help is printed to the console
HelloWorld return code is CommandResultCode.CommandParameterNotValid (-400) and the help for the HelloWorldCommand is printed to the console
HelloWorld --name Matthias or HelloWorld -n Matthias return code is CommandResultCode.Ok (0) and Hello world Matthias ! is printed to the console

commandlineparser's People

Contributors

gitter-badger avatar kapitanov avatar mgrosperrin avatar samirzebbouche avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

commandlineparser's Issues

Update documentation for 0.6.0

Some changes are made for 0.6.0 so documentation need to be updated.

This issue is to track all stuff that have changed.

Improve build automation

The build script should run the tests, create a package and upload it to nuget.org or myget.org galleries.
The AppVeyor build should be updated to build and publish the dev branch to myget.org, the master branch to nuget.org and just build the others branches.

Change registrying of dependencies

Currently registrying the dependencies take a Func<IDependencyResolverScope, T>.
It should be changed to Func<Func<IDependencyResolverScope, T>>. The first Func will be called when a new scope is created, and the second one will be be called everytime the dependency should be resolved.

Fix the Enum converter

The EnumConverter is not selected when an option is an enum.
Checks the filter of the converters

Installation instruction fails

sabayon ~ # pash
Pash - Copyright (C) Pash Contributors. License: GPL/BSD. See https://github.com/Pash-Project/Pash/

PASH /home/user> Install-Package MGR.CommandLineParser
Command 'Install-Package' not found.
  +CategoryInfo: ObjectNotFound, Reason: ParentContainsErrorRecordException
  +FullyQualifiedErrorId: CommandNotFoundException
PASH /home/user>

Improve console logging in tests

Currently the console logging in the integration tests displays in the console. In some circumstance the tests fails because the console is not correctly configured

Add possibility to group commands

Add the capability to group commands.
When grouped, the commands can only be invoked by specifying the group and then the command name.

Clean the public API

Move all the public APIs for extensibility to a "Extensibility" namespace.
The goal is to have to simpliest public API in the MGR.CommandLineParser and MGR.CommandLineParser.Command namespaces.

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.