Giter VIP home page Giter VIP logo

nopt's Introduction

NOpt

Small, flexible and crossplatform command line parser.

Target: .NET Standard 1.2

Show me the code

class Options {
    [Option('f', "flag")]
    public bool Flag  { get; set; }

    [Option('p', "path")]
    public string Path  { get; set; }

    [Value(0)]
    public string[] OtherOpts { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var opt = NOpt.NOpt.Parse<Options>(args)
        // Use opt!
    }
}

Usage

  1. install with Install-Package NOpt

  2. define a class that holds an options

  3. call NOpt.Parse method to get an options

Flags

Create a boolean property to hold a flag

[Option(shortName: 'f', longName: "flag")]
public bool Flag { get; set; }

or more shorter

[Option('f', "flag")]
public bool Flag  { get; set; }

Now if you call progam -f or program --flag this flag will be set to true.

You can use only short name or only long name or both to name your flag

Options with value

Create property to hold option with value like

[Option('p', "path")]
public string Path  { get; set; }

Now you can call

program -p path\to\file or

program --path path\to\file or

program --path=path\to\file

Option propery can be an array also:

[Option('p', "pets")]
public string[] Pets  { get; set; }

So

program -p cats dogs or

program --pets cats dogs

will set Pets field to {"cats", "gods"}

You can't use program --pets=cats dogs syntax here

Nameless option.

[Value(0)]
public string Dir  { get; set; }

Now you can call

program mydir or

program --path path\to\file mydir

Value attribute has only one parameter - index of nameless option. Indexes are zero based.

Arrays, vararg feature

[Value(1)]
public string[] OtherParams { get; set; }

Now you can call program mydir a b c and otherParams will be set to {"a", "b", "c"}

mydir option was ignored because we don't have property with attribute [Value(0)] here

Enumerations

enum Color {RED, GREEN, NO_COLOR}
[Option('c', "color")]
public Color color  { get; set; }

Now you can call

program -c no-color or

program --color no-color or

program --color=no-color or

program --color=no_color or

program --color=NO_COLOR or

program --color=red

What to pass multiply values? Just add [Flags] attribute to your enumeration:

[Flags] enum Color {RED, GREEN, NO_COLOR}

and use program -c red,green to set flags

No spaces allowed between "red,green"

Default value

NOpt don't touch property default value when there is no corresponding options.

So just use:

public Color color { get; set; } = Color.RED;

fills naturally, right?

Verbs

Check this git-like options:

public class Options
{
    [Option("help")]
    public bool help { get; set; }

    [Verb("add")]
    public Add add { get; set; }

    [Verb("commit")]
    public Commit commit { get; set; }

    [Verb("push")]
    public Push push { get; set; }
}

class Add
{
    [Value(0)]
    public string[] files { get; set; }
}

class Commit
{
    [Option("amend")]
    public bool amend { get; set; }

    [Option('m', "message")]
    public string message { get; set; }

    [Value(0)]
    public string[] files { get; set; }
}

class Push
{
    [Option("repo")]
    public string repo { get; set; }

    [Option('f', "force")]
    public bool force { get; set; }

    [Option('v', "verbose")]
    public bool verbose { get; set; }
}

With this options you can:

program add file1 file2

program commit -m 'initial commit'

program push -repo origin

Verbs are mutually exclusive so program add push will fail.

Remember that NOpt don't touch properties default values so to check what verb was used:

if(opt.help != null) {
    // show help
}
else if(opt.add != null) {
    // use opt.add
}
else if(opt.commit != null) {
    // use opt.commit
}
...
else {
    // no verbs? show a hint
}

nopt's People

Contributors

dimaaan avatar

Stargazers

 avatar  avatar

Watchers

 avatar

nopt's Issues

Use properties instead of fields

To use NOpt in conjunction with System.ComponentModel.DataAnnotations.Validator we need to apply it attributes to the properties instead of fields

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.