-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimeExample.cs
124 lines (105 loc) · 4.5 KB
/
TimeExample.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections.Generic;
namespace Argparse.Examples;
public class TimeExample
{
record TimeCommandConfiguration
{
public bool help = false;
public bool portability = false;
public bool version = false;
public bool append = false;
public bool verbose = false;
public string? format;
public List<string> arguments = new();
public string? command;
public string? outputFile;
}
public static void Run(string[] args)
{
var config = new TimeCommandConfiguration();
var parser = new Parser<TimeCommandConfiguration>(config)
{
Names = new() { "time" },
Description = "Program to print information about time"
};
var helpFlag = new Flag<TimeCommandConfiguration>()
{
Names = new() { "-h", "--help" },
Description = "Print a usage message on standard output and exit successfully.",
Action = (storage) => { storage.help = true; }
};
parser.AddFlag(helpFlag);
var versionFlag = new Flag<TimeCommandConfiguration>()
{
Names = new() { "-V", "--version" },
Description = "Print version information on standard output, then exit successfully.",
Action = (storage) => { storage.version = true; }
};
parser.AddFlag(versionFlag);
var appendFlag = new Flag<TimeCommandConfiguration>()
{
Names = new() { "-a", "--append" },
Description = "(Used together with -o.) Do not overwrite but append.",
Action = (storage) => { storage.append = true; }
};
parser.AddFlag(appendFlag);
var verboseFlag = new Flag<TimeCommandConfiguration>()
{
Names = new() { "-v", "--verbose" },
Description = "Give very verbose output about all the program knows about.",
Action = (storage) => { storage.verbose = true; }
};
parser.AddFlag(verboseFlag);
var portabilityFlag = new Flag<TimeCommandConfiguration>()
{
Names = new() { "-p", "--portability" },
Description = "Use the portable output format.",
Action = (storage) => { storage.portability = true; }
};
parser.AddFlag(portabilityFlag);
var formatOption = new Option<TimeCommandConfiguration, string>
{
Names = new() { "-f", "--format" },
Description = "Specify output format, possibly overriding the format specified in the environment variable TIME.",
ValuePlaceHolder = "FORMAT",
Action = (storage, value) =>
{
storage.format = value;
},
Converter = ConverterFactory.CreateStringConverter()
};
parser.AddOption(formatOption);
var outputFileOption = new Option<TimeCommandConfiguration, string>
{
Names = new() { "-o", "--output" },
Description = "Do not send the results to stderr, but overwrite the specified file.",
ValuePlaceHolder = "FILE",
Converter = ConverterFactory.CreateStringConverter(),
Action = (storage, value) =>
{
storage.outputFile = value;
}
};
parser.AddOption(outputFileOption);
// if we needed special parsing for different kinds of commands, we could use subparsers
parser.AddArgument(new Argument<TimeCommandConfiguration, string>
{
Multiplicity = new ArgumentMultiplicity.SpecificCount(1, true),
Action = (storage, value) => { storage.command = value; },
ValuePlaceholder = "command",
Converter = ConverterFactory.CreateStringConverter(),
Description = "command to run"
});
parser.AddArgument(new Argument<TimeCommandConfiguration, string>
{
Multiplicity = new ArgumentMultiplicity.AllThatFollow(),
Action = (storage, value) => { storage.arguments.Add(value); },
ValuePlaceholder = "arg-name",
Converter = ConverterFactory.CreateStringConverter(),
Description = "arg desc"
});
// 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)
parser.PrintHelp();
}
}