-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleExample.cs
More file actions
54 lines (45 loc) · 1.75 KB
/
SimpleExample.cs
File metadata and controls
54 lines (45 loc) · 1.75 KB
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
namespace Argparse.Examples;
public class SimpleExample
{
record ExampleConfiguration
{
public string? algorithm;
public bool help = false;
public string? inputFile;
}
public static void Run(string[] args)
{
var config = new ExampleConfiguration();
var parser = new Parser<ExampleConfiguration>(config)
{
Names = new() { "program" },
Description = "Program description"
};
var helpFlag = new Flag<ExampleConfiguration>()
{
Names = new() { "-h", "--help" },
Description = "Show help",
Action = (storage) => { storage.help = true; }
};
parser.AddFlag(helpFlag);
var algorithmOption = new Option<ExampleConfiguration, string>
{
Names = new() { "-a", "--algorithm" },
Description = "Set algorithm to use",
Action = (storage, value) => { storage.algorithm = value; },
Converter = ConverterFactory.CreateStringConverter(),
};
var inputFileArg = new Argument<ExampleConfiguration, string>
{
ValuePlaceholder = "input file",
Description = "File to process",
Multiplicity = new ArgumentMultiplicity.SpecificCount(1, true),
Action = (storage, value) => { storage.inputFile = value; },
Converter = ConverterFactory.CreateStringConverter()
};
parser.AddArgument(inputFileArg);
parser.AddOption(algorithmOption);
// Now we would call `parser.Parse(args);` and we our config instance would be
// populated with the values from the command line (or an exception is thrown)
}
}