-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_cli.cpp
170 lines (136 loc) · 5.47 KB
/
example_cli.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <c2p/cli.hpp>
#include <c2p/json.hpp>
#include <iostream>
#include <optional>
#include <vector>
const c2p::Logger logger{
// clang-format off
[](const std::string& logStr) { std::cerr << "Error: " << logStr << std::endl; },
[](const std::string& logStr) { std::cout << "Warning: " << logStr << std::endl; },
[](const std::string& logStr) { std::cout << "Info: " << logStr << std::endl; },
// clang-format on
};
int main(int argc, char* argv[]) {
// clang-format off
c2p::cli::CommandGroup cg = {
.command = "root_cmd",
.description = "This is a CLI parser exapmle.",
.flagArgs = {
{ .name = "version", .shortName = 'v', .description = "Show version information." },
{ .name = "help", .shortName = 'h', .description = "Show help information." },
},
.subCommands = {
{
.command = "sub_cmd",
.description = "This is a sub command.",
.flagArgs = {
{ .name = "version", .shortName = 'v', .description = "Show version information." },
{ .name = "help", .shortName = 'h', .description = "Show help information." },
{ .name = "list", .shortName = 'l', .description = "List all items." },
},
.valueArgs = {
{ .name = "nums", .shortName = 'n', .typeTag = c2p::TypeTag::NUMBER, .multiple = true, .description = "Specify a series of numbers." },
{ .name = "input", .shortName = 'i', .typeTag = c2p::TypeTag::STRING, .required = true, .description = "Specify input file path." },
{ .name = "output", .typeTag = c2p::TypeTag::STRING, .description = "Specify output file path." },
},
.minPositionalArgNum = 2,
.maxPositionalArgNum = 6,
.positionalArgDescription = "Positional arguments are required as inputs.",
},
{
.command = "sub_cmd2",
.description = "This is another sub command.",
},
}
};
// clang-format on
const auto parser = c2p::cli::Parser::constructFrom(cg, logger);
// clang-format off
std::cout << "--------------------------------------------------------------------------------" << std::endl;
std::cout << (*(*parser).getHelp({}, true, logger)) << "\n";
std::cout << "--------------------------------------------------------------------------------" << std::endl;
std::cout << (*(*parser).getHelp({ "sub_cmd" }, true, logger)) << "\n";
std::cout << "--------------------------------------------------------------------------------" << std::endl;
std::cout << (*(*parser).getHelp({ "sub_cmd2" }, true, logger)) << "\n";
std::cout << "--------------------------------------------------------------------------------" << std::endl;
// clang-format on
const char* const args[] = {
"root_cmd", "sub_cmd", "-l", "position1", "-n",
"1e3", "-hv", "-n", "123", "--input",
"~/input.ini", "--output", "./output.exe", "position2", "position3",
};
const auto tree =
parser->parse(sizeof(args) / sizeof(args[0]), args, logger);
std::cout << c2p::json::dump(tree, true, 4) << std::endl;
// clang-format off
// Output should be:
/*
--------------------------------------------------------------------------------
Usage:
root_cmd [-v] [-h]
This is a CLI parser exapmle.
Sub Commands:
sub_cmd
This is a sub command.
sub_cmd2
This is another sub command.
Flag Arguments:
-v, --version
Show version information.
-h, --help
Show help information.
--------------------------------------------------------------------------------
Usage:
root_cmd sub_cmd -i <STRING> [-n <NUMBER>] [--output <STRING>] [-v] [-h] [-l] <positionalArg0> <positionalArg1> [positionalArg2...5]
This is a sub command.
Flag Arguments:
-v, --version
Show version information.
-h, --help
Show help information.
-l, --list
List all items.
Required Value Arguments:
-i, --input <STRING>
Specify input file path.
Optional Value Arguments:
-n, --nums <NUMBER> [multiple as array]
Specify a series of numbers.
--output <STRING>
Specify output file path.
Positional Arguments:
Need 2 ~ 6 positional argument(s).
Positional arguments are required as inputs.
--------------------------------------------------------------------------------
Usage:
root_cmd sub_cmd2
This is another sub command.
--------------------------------------------------------------------------------
{
"command": "root_cmd",
"subCommand": {
"command": "sub_cmd",
"flagArgs": [
"list",
"help",
"version"
],
"positionalArgs": [
"position1",
"position2",
"position3"
],
"valueArgs": {
"input": "~/input.ini",
"nums": [
1000,
123
],
"output": "./output.exe"
}
}
}
*/
// clang-format on
return 0;
}