forked from kmurray/libargparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
argparse_example.cpp
140 lines (115 loc) · 4.08 KB
/
argparse_example.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
#include "argparse.hpp"
using argparse::ArgValue;
using argparse::ConvertedValue;
struct Args {
ArgValue<bool> do_foo;
ArgValue<bool> enable_bar;
ArgValue<std::string> filename;
ArgValue<size_t> verbosity;
ArgValue<bool> show_version;
ArgValue<float> utilization;
ArgValue<std::vector<float>> zulus;
ArgValue<std::vector<float>> alphas;
};
struct OnOff {
ConvertedValue<bool> from_str(std::string str) {
ConvertedValue<bool> converted_value;
if (str == "on") converted_value.set_value(true);
else if (str == "off") converted_value.set_value(false);
else converted_value.set_error("Invalid argument value");
return converted_value;
}
ConvertedValue<std::string> to_str(bool val) {
ConvertedValue<std::string> converted_value;
if (val) converted_value.set_value("on");
else converted_value.set_value("off");
return converted_value;
}
std::vector<std::string> default_choices() {
return {"on", "off"};
}
};
struct ZeroOneRange {
ConvertedValue<float> from_str(std::string str) {
float value;
std::stringstream ss(str);
ss >> value;
ConvertedValue<float> converted_value;
if (value < 0. || value > 1.) {
std::stringstream msg;
msg << "Value '" << value << "' out of expected range [0.0, 1.0]";
converted_value.set_error(msg.str());
} else {
converted_value.set_value(value);
}
return converted_value;
}
std::string to_str(float value) {
return std::to_string(value);
}
std::vector<std::string> default_choices() {
return {};
}
};
int main(int argc, const char** argv) {
Args args;
auto parser = argparse::ArgumentParser(argv[0], "My application description");
parser.version("Version: 0.0.1");
parser.add_argument(args.filename, "filename")
.help("File to process");
parser.add_argument(args.do_foo, "--foo")
.help("Causes foo")
.default_value("false")
.action(argparse::Action::STORE_TRUE);
parser.add_argument<bool,OnOff>(args.enable_bar, "--bar")
.help("Control whether bar is enabled")
.default_value("off");
parser.add_argument(args.verbosity, "--verbosity", "-v")
.help("Sets the verbosity")
.default_value("1")
.choices({"0", "1", "2"});
parser.add_argument(args.show_version, "--version", "-V")
.help("Show version information")
.action(argparse::Action::VERSION);
parser.add_argument<float,ZeroOneRange>(args.utilization, "--util")
.help("Sets target utilization")
.default_value("1.0");
parser.add_argument(args.zulus, "--zulu")
.help("One or more float values")
.nargs('+')
.default_value({"1.0", "0.2"});
parser.add_argument(args.alphas, "--alphas")
.help("Zero or more float values")
.nargs('*')
.default_value({});
parser.parse_args(argc, argv);
//Show the arguments
std::cout << "args.filename: " << args.filename << "\n";
std::cout << "args.do_foo: " << args.do_foo << "\n";
std::cout << "args.verbosity: " << args.verbosity << "\n";
std::cout << "args.utilization: " << args.utilization << "\n";
std::cout << "args.zulu: " << argparse::join(args.zulus.value(), ", ") << "\n";
std::cout << "args.alphas: " << argparse::join(args.alphas.value(), ", ") << "\n";
std::cout << "\n";
//Do work
if (args.do_foo) {
if (args.verbosity > 0) {
std::cout << "Doing foo with " << args.filename << "\n";
}
if (args.verbosity > 1) {
std::cout << "Doing foo step 1" << "\n";
std::cout << "Doing foo step 2" << "\n";
std::cout << "Doing foo step 3" << "\n";
}
}
if (args.enable_bar) {
if (args.verbosity > 0) {
std::cout << "Bar is enabled" << "\n";
}
} else {
if (args.verbosity > 0) {
std::cout << "Bar is disabled" << "\n";
}
}
return 0;
}