|
1 |
| -Fluent Command Line Parser |
2 |
| -========================== |
3 |
| -A simple, strongly typed .NET C# command line parser library using a fluent easy to use interface. |
4 |
| - |
5 |
| -### Download |
6 |
| - |
7 |
| -You can download the latest release from [CodeBetter's TeamCity server](http://teamcity.codebetter.com/project.html?projectId=project314) |
8 |
| - |
9 |
| -You can also install using [NuGet](http://nuget.org/packages/FluentCommandLineParser/) |
10 |
| - |
11 |
| -``` |
12 |
| -PM> Install-Package FluentCommandLineParser |
13 |
| -``` |
14 |
| -Commands such as `updaterecord.exe /r 10 /v="Mr. Smith" /silent` can be captured using |
15 |
| - |
16 |
| -``` |
17 |
| -static void Main(string[] args) |
18 |
| -{ |
19 |
| - IFluentCommandLineParser parser = new FluentCommandLineParser(); |
20 |
| -
|
21 |
| - parser.Setup<int>("r") |
22 |
| - .Callback(record => RecordID = record) |
23 |
| - .Required(); |
24 |
| -
|
25 |
| - parser.Setup<string>("v") |
26 |
| - .Callback(value => NewValue = value) |
27 |
| - .Required(); |
28 |
| -
|
29 |
| - parser.Setup<bool>("s", "silent") |
30 |
| - .Callback(silent => InSilentMode = silent) |
31 |
| - .SetDefault(false); |
32 |
| -
|
33 |
| - parser.Parse(args); |
34 |
| -} |
35 |
| -``` |
36 |
| -### Parser Option Methods |
37 |
| - |
38 |
| -`.Setup<int>("r")` Setup an option using a short name, |
39 |
| - |
40 |
| -`.Setup<int>("r", "record")` or short and long name. |
41 |
| - |
42 |
| -`.Required()` Indicate the option is required and an error should be raised if it is not provided. |
43 |
| - |
44 |
| -`.Callback(val => Value = val)` Provide a delegate to call after the option has been parsed |
45 |
| - |
46 |
| -`.SetDefault(int.MaxValue)` Define a default value if the option was not specified in the args |
47 |
| - |
48 |
| -`.WithDescription("Execute operation in silent mode without feedback")` Specify a help description for the option |
49 |
| - |
50 |
| -### Setup Help |
51 |
| - |
52 |
| -Setup to print the available options to the console when any of the help args are found. |
53 |
| - |
54 |
| -``` |
55 |
| -static void Main(string[] args) |
56 |
| -{ |
57 |
| - var parser = new FluentCommandLineParser(); |
58 |
| -
|
59 |
| - parser.SetupHelp("h", "help", "?") |
60 |
| - .Callback(Console.WriteLine); |
61 |
| -
|
62 |
| - var result = parser.Parse(args); |
63 |
| - |
64 |
| - if(result.HelpCalled) return; |
65 |
| -} |
66 |
| -``` |
67 |
| -### Supported syntax |
68 |
| - |
69 |
| -`[-|--|/][switch_name][=|:| ][value]` |
70 |
| - |
71 |
| -Also supports boolean names |
72 |
| - |
73 |
| -``` |
74 |
| -updaterecord.exe -s // enabled |
75 |
| -updaterecord.exe -s- // disabled |
76 |
| -updaterecord.exe -s+ // enabled |
77 |
| -``` |
78 |
| - |
79 |
| -### Development |
80 |
| - |
81 |
| -Fclp is in the early stages of development. Please feel free to provide any feedback on feature support or the Api itself. |
82 |
| - |
| 1 | +Fluent Command Line Parser |
| 2 | +========================== |
| 3 | +A simple, strongly typed .NET C# command line parser library using a fluent easy to use interface. |
| 4 | +### Download |
| 5 | + |
| 6 | +See what's new in [v1.1](https://github.com/fclp/fluent-command-line-parser/wiki/Roadmap). |
| 7 | + |
| 8 | +You can download the latest release from [CodeBetter's TeamCity server](http://teamcity.codebetter.com/project.html?projectId=project314) |
| 9 | + |
| 10 | +You can also install using [NuGet](http://nuget.org/packages/FluentCommandLineParser/) |
| 11 | +``` |
| 12 | +PM> Install-Package FluentCommandLineParser |
| 13 | +``` |
| 14 | +### Usage |
| 15 | +Commands such as `updaterecord.exe -r 10 -v="Mr. Smith" --silent` can be captured using |
| 16 | +``` |
| 17 | +static void Main(string[] args) |
| 18 | +{ |
| 19 | + var p = new FluentCommandLineParser(); |
| 20 | +
|
| 21 | + p.Setup<int>("r") |
| 22 | + .Callback(record => RecordID = record) |
| 23 | + .Required(); |
| 24 | +
|
| 25 | + p.Setup<string>("v") |
| 26 | + .Callback(value => NewValue = value) |
| 27 | + .Required(); |
| 28 | +
|
| 29 | + p.Setup<bool>("s", "silent") |
| 30 | + .Callback(silent => InSilentMode = silent) |
| 31 | + .SetDefault(false); |
| 32 | +
|
| 33 | + p.Parse(args); |
| 34 | +} |
| 35 | +``` |
| 36 | +### Parser Option Methods |
| 37 | + |
| 38 | +`.Setup<int>("r")` Setup an option using a short name, |
| 39 | + |
| 40 | +`.Setup<int>("r", "record")` or short and long name. |
| 41 | + |
| 42 | +`.Required()` Indicate the option is required and an error should be raised if it is not provided. |
| 43 | + |
| 44 | +`.Callback(val => Value = val)` Provide a delegate to call after the option has been parsed |
| 45 | + |
| 46 | +`.SetDefault(int.MaxValue)` Define a default value if the option was not specified in the args |
| 47 | + |
| 48 | +`.WithDescription("Execute operation in silent mode without feedback")` Specify a help description for the option |
| 49 | + |
| 50 | +### Parsing To Collections |
| 51 | + |
| 52 | +Many arguments can be collected as part of a list. Types supported are `string`, `int`, `double` and `bool` |
| 53 | + |
| 54 | +For example arguments such as |
| 55 | + |
| 56 | +`--filenames C:\file1.txt C:\file2.txt C:\file3.txt` |
| 57 | + |
| 58 | +can be automatically parsed to a `List<string>` using |
| 59 | +``` |
| 60 | +static void Main(string[] args) |
| 61 | +{ |
| 62 | + var p = new FluentCommandLineParser(); |
| 63 | +
|
| 64 | + var filenames = new List<string>(); |
| 65 | +
|
| 66 | + p.Setup<List<string>>("f", "filenames") |
| 67 | + .Callback(items => filenames = items); |
| 68 | +
|
| 69 | + p.Parse(args); |
| 70 | +
|
| 71 | + Console.WriteLine("Input file names:"); |
| 72 | +
|
| 73 | + foreach (var filename in filenames) |
| 74 | + { |
| 75 | + Console.WriteLine(filename); |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | +output: |
| 80 | +``` |
| 81 | +Input file names |
| 82 | +C:\file1.txt |
| 83 | +C:\file2.txt |
| 84 | +C:\file3.txt |
| 85 | +``` |
| 86 | +### Supported Syntax |
| 87 | +`[-|--|/][switch_name][=|:| ][value]` |
| 88 | + |
| 89 | +Supports boolean names |
| 90 | +``` |
| 91 | +example.exe -s // enable |
| 92 | +example.exe -s- // disabled |
| 93 | +example.exe -s+ // enable |
| 94 | +``` |
| 95 | +Supports combined (grouped) options |
| 96 | +``` |
| 97 | +example.exe -xyz // enable option x, y and z |
| 98 | +example.exe -xyz- // disable option x, y and z |
| 99 | +example.exe -xyz+ // enable option x, y and z |
| 100 | +``` |
| 101 | +### Development |
| 102 | +Fclp is in the early stages of development. Please feel free to provide any feedback on feature support or the Api itself. |
| 103 | + |
83 | 104 | If you would like to contribute, you may do so to the [develop branch](https://github.com/fclp/fluent-command-line-parser/tree/develop).
|
0 commit comments