|
1 | 1 | # FlowCommandLine |
2 | | -A fast and simple command line parser that works in two modes: command-based (e.g. `git commit ...`) or parameter-only. It support modern dotnet core runtimes (net8+), compilation in NativeAot. |
| 2 | +A fast and simple command line parser that works in two modes: command-based (e.g. `git commit ...`) or parameters-only. Parsing can be happened to any model class or record with parameterless constructor. |
| 3 | +It support modern dotnet core runtimes (net8+), compilation in NativeAot. It supported auto documentation for commands and parameters. |
| 4 | +Model parameters mapped types is supported: int, long, double, float, string, DateOnly, DateTime, TimeSpan. |
| 5 | +By default, the output will be to the system console, but can be redefined to any of your case - instead `CommandLine.Console ()` you can use `new CommandLine (new MyConsoleCommandLineProvider())` where `MyConsoleCommandLineProvider` it is you class which is implement `ICommandLineProvider` interface. |
| 6 | + |
| 7 | +## Command-based mode |
| 8 | + |
| 9 | +Example command line: |
| 10 | +`myconapp runapp --param1=stringvalue --param2=120` |
| 11 | + |
| 12 | +```csharp |
| 13 | +public class Test { |
| 14 | + public string Param1 { get; set; } = ""; |
| 15 | + public int Param2 { get; set; } |
| 16 | +} |
| 17 | + |
| 18 | +CommandLine.Console () |
| 19 | + // setup console application description version and so on |
| 20 | + .Application ( "My Console App", "1.0.0", "full description.", "Copyright My Super Corporation", "myconapp" ) |
| 21 | + .AddCommand ( // add console command |
| 22 | + "runapp", // command name |
| 23 | + ( Test parameters ) => { // command delegate handler for class Test |
| 24 | + ... |
| 25 | + }, |
| 26 | + "Command description", // command description :) |
| 27 | + new List<FlowCommandParameter> { // adjust command parameters |
| 28 | + new FlowCommandParameter { |
| 29 | + FullName = "param1", // for parameter in format --param1 |
| 30 | + ShortName = "p1, // for parameter in format -p1 |
| 31 | + PropertyName = "Param1" // it |
| 32 | + Description = "parameter description", |
| 33 | + Required = true, // parameter is required |
| 34 | + }, |
| 35 | + new FlowCommandParameter { |
| 36 | + FullName = "param2", // full name is required property, other properties ShortName or PropertyName can be inferred from FullName |
| 37 | + Description = "parameter2 description", |
| 38 | + Required = false, |
| 39 | + } |
| 40 | + } |
| 41 | + ) |
| 42 | + .RunCommand (); |
| 43 | +``` |
| 44 | + |
| 45 | +## Parameters-only mode |
| 46 | + |
| 47 | +Example command line: |
| 48 | +`myconapp --param1=stringvalue --param2=120` |
| 49 | + |
| 50 | +```csharp |
| 51 | +var options = CommandLine.Console () |
| 52 | + .Application ( "My Console App", "1.0.0", "full description.", "Copyright My Super Corporation", "myconapp" ) |
| 53 | + .AddOption ( "p1", "param1", "parameter 1 description", required: true ) |
| 54 | + .AddOption ( "p2", "param2", "parameter 2 description", required: false ) |
| 55 | + .RunOptions<Test> (); // run options parse and |
| 56 | +``` |
| 57 | + |
0 commit comments