Coder Social home page Coder Social logo

clap's Introduction

CLAP: A Kick-Ass .NET Command-Line Parser

Full Documentation and Samples

http://adrianaisemberg.github.com/CLAP

Basic example: One verb, Some parameters

class Program
{
    public static void Main(string[] args)
    {
        Parser.Run<MyApp>(args);
    }
}

class MyApp
{
    [Verb]
    public static void Print(string text, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine(text);
        }
    }
}
>myexe print -text:"Hello World" -count:10

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Complex sample: Various verbs, global handlers, help handler, validation, default values, switches, arrays

class MyApp
{
    [Global(Aliases = "d", Description = "Launch a debugger")]
    public static void Debug()
    {
        // this is a global parameter handler.
        // it works for any verb.

        Debugger.Launch();
    }

    [Empty, Help]
    public static void Help(string help)
    {
        // this is an empty handler that prints
        // the automatic help string to the console.

        Console.WriteLine(help);
    }

    [Verb]
    public static void Print(string text, int count)
    {
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine(text);
        }
    }

    [Verb]
    public static void Foo(
        [Parameter(Aliases = "t", Description = "A string parameter with an additional alias")]
        string text,

        [Parameter(Default = 5, Description = "An int parameter with a default")]
        int number,

        [MoreThan(10)]
        [LessThan(100)]
        [Parameter(Default = 42.3, Description = "A double parameter with validation and a default value")]
        double percent,

        [Parameter(Description = "A bool parameter, which can be used as a switch")]
        bool verbose,

        [Parameter(Description = "An enum parameter")]
        OptionEnum option,

        [Parameter(Description = "An array of strings")]
        string[] array)
    {
        Console.WriteLine("text = {0}", text);
        Console.WriteLine("number = {0}", number);
        Console.WriteLine("percent = {0}", percent);
        Console.WriteLine("verbose = {0}", verbose);
        Console.WriteLine("option = {0}", option);
        Console.WriteLine("array = [{0}]", string.Join(",", array));
    }
}
>myexe foo -t:hello -number:88 -percent:12.446 -verbose -option:Option2 -array:a,b,c,d

text = hello
number = 88
percent = 12.446
verbose = True
option = Option2
array = [a,b,c,d]

No arguments, help is printed:

>myexe

Print:
 -text:  [String]
 -count:  [Int32]

Foo:
 -text/t: A string parameter with an additional alias [String]
 -number: An int parameter with a default [Int32, Default = 5]
 -percent: A double parameter with validation and a default value [Double, Default = 42.3, More than 10, Less than 100]
 -verbose: A bool parameter, which can be used as a switch [Boolean]
 -option: An enum parameter [OptionEnum]
 -array: An array of strings [String[]]

Global parameters:
 -Debug/d: Launch a debugger [Boolean]

clap's People

Contributors

adrianaisemberg avatar appakz avatar brutaldev avatar invizory avatar mikalai-kardash avatar mikejansen avatar wernight avatar

Watchers

 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.