-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexExample.cs
63 lines (56 loc) · 2.07 KB
/
ComplexExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
namespace Argparse.Examples;
public class ComplexExample
{
record ComplexExampleCommandConfig
{
public bool help = false;
}
record ComplexExampleSubCommandConfig
{
public bool help = false;
public int? number;
}
public static void Run(string[] args)
{
var toplevelParser = new Parser<ComplexExampleCommandConfig>(
() => new ComplexExampleCommandConfig()
)
{
Names = new() { "My program" },
Description = "My description",
Run = (c, _) => { Console.WriteLine("I will run after my config is ready"); }
};
toplevelParser.AddFlag(new Flag<ComplexExampleCommandConfig>
{
Names = new() { "-h", "--help" },
Description = "Print help",
Action = (c) => { c.help = true; }
});
var subcommandParser = new Parser<ComplexExampleSubCommandConfig>(
() => new ComplexExampleSubCommandConfig()
)
{
Names = new() { "subcommand" },
Description = "My subcommand description",
Run = (c, _) => { Console.WriteLine("I will run after my config is ready"); }
};
subcommandParser.AddFlag(new Flag<ComplexExampleSubCommandConfig>()
{
Names = new() { "-h", "--help" },
Description = "Print help",
Action = (c) => { c.help = true; }
});
var numberArg = new Argument<ComplexExampleSubCommandConfig, int>
{
ValuePlaceholder = "Number",
Description = "Number description",
Action = (storage, value) => { storage.number = value; },
Converter = ConverterFactory.CreateIntConverter()
};
subcommandParser.AddArgument(numberArg);
toplevelParser.AddSubparser(subcommandParser);
// Now we would run `toplevelParser.ParseAndRun(args);` and
// one of the provided Run methods would run (or an exception is thrown)
}
}