Repository navigation
Expand file tree
/
Copy pathexample.cpp
More file actions
53 lines (44 loc) · 1.66 KB
/
Copy pathexample.cpp
File metadata and controls
53 lines (44 loc) · 1.66 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
#include "arg_parser.hpp"
#include <exception>
#include <iostream>
#include <string>
constexpr const char *usage = "Usage: \ng++ [options] file...\n";
#ifdef _WIN32
int wmain(int argc, wchar_t *argv[])
#else
int main(int argc, char *argv[])
#endif
{
using arg_parser::ArgType;
arg_parser::ArgParser parser("g++", usage);
// or set the program name by set_xxx function
// parser.set_program_name("cpp");
// parser.set_usage(usage);
// step 1: set the required arguments
// [ArgType::Option] and [ArgType::Flag] support
// default Flag
// For Flag, default value will be false, no need to set manually
parser.add_argument("verbose", "v")
.set_description("if true, print with verbose output");
parser.add_argument("output", "o", ArgType::Option)
.set_description("Place the output into <file>.")
.set_required(true);
parser.add_argument("help", "h").set_description("print help manual");
try {
// step 2: parse arguments directly
// WARN: Don't use the vector version unless you know what you're doing
parser.parse(argc, argv);
// step 3: traverse the positional args
// fetch option value with type assign
for (size_t i = 0; i < parser.size(); i++) {
std::cout << i << " pos: " << parser.at(i) << '\n';
}
std::cout << "verbose: " << parser.get<bool>("v") << '\n';
std::cout << "output: " << parser.get<std::string>("o") << '\n';
} catch (const std::exception &err) {
std::cerr << err.what() << '\n';
// if you have setted usage, just leave it empty
std::cout << parser.help();
return 1;
}
}