CLAP

by adrianaisemberg

adrianaisemberg / CLAP

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

214 Stars 41 Forks Last release: Not found MIT License 191 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

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(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 &lt; 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]

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.