-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_example.cpp
150 lines (123 loc) · 5.54 KB
/
short_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
140
141
142
143
144
145
146
147
148
149
150
#include <clapp/argument.h>
#include <clapp/build_info.h>
#include <clapp/main_parser.h>
#include <clapp/option.h>
#include <clapp/parser_container.h>
#include <unistd.h>
#include <iostream>
clapp::value::found_func_t::ret_t print_version_and_exit(
const std::string &option);
class cli_parser_t : public clapp::basic_main_parser_t {
public:
cli_parser_t();
explicit cli_parser_t(const cli_parser_t &) = delete;
explicit cli_parser_t(cli_parser_t &&) noexcept = delete;
cli_parser_t &operator=(const cli_parser_t &) = delete;
cli_parser_t &operator=(cli_parser_t &&) noexcept = delete;
~cli_parser_t() override;
// if help is given, help is printed and exit(EXIT_SUCCESS) is requested
clapp::help_option_t help{*this, std::vector<std::string>{"help", "usage"},
std::vector<char>{'h', '?'},
"Show help options."};
// if version is given, print_version_and_exit() is called.
clapp::bool_option_t version{
*this, std::vector<std::string>{"version", "vers"}, 'v',
"Show version info",
clapp::value::found_func_t{print_version_and_exit}};
// first mandatory argument of string
clapp::string_argument_t string_arg{*this, "string-arg", "String argument"};
// second mandatory argument of int32_t
clapp::int32_argument_t int_arg{
*this, "int-arg", "Int argument",
clapp::min_max_value_t<std::int32_t>{5, 10}};
// third optional variadic arguments of strings
clapp::variadic_string_argument_t variadic_string_arg{
*this, "variadic-string-arg", "Variadic String argument",
purpose_t::optional};
class and_option_container_t : public clapp::option_container_t {
public:
using clapp::option_container_t::option_container_t;
~and_option_container_t() override;
// mandatory string option
clapp::string_param_option_t string_param{
*this, "string", std::vector<char>{'s', '1'}, "String option 1.",
purpose_t::mandatory};
// optional string option (multiple string vectors)
clapp::vector_string_param_option_t string_vector_param{
*this, "string-vector", "String vector param.",
purpose_t::optional};
explicit and_option_container_t(const and_option_container_t &) =
delete;
explicit and_option_container_t(and_option_container_t &&) noexcept =
delete;
and_option_container_t &operator=(const and_option_container_t &) =
delete;
and_option_container_t &operator=(and_option_container_t &&) noexcept =
delete;
};
and_option_container_t options{
*this, clapp::parser::types::logic_operator_type_t::logic_and};
};
clapp::value::found_func_t::ret_t print_version_and_exit(
const std::string & /*option*/) {
std::cout << clapp::build_info::build_info_string << std::endl;
return clapp::value::exit_t::exit(EXIT_SUCCESS);
}
cli_parser_t::~cli_parser_t() = default;
cli_parser_t::and_option_container_t::~and_option_container_t() = default;
cli_parser_t::cli_parser_t()
: clapp::basic_main_parser_t{
clapp::parser::types::logic_operator_type_t::logic_xor} {}
using parser_t = clapp::parser::basic_parser_container_t<cli_parser_t>;
int main(int argc, char *argv[]) {
try {
parser_t parser;
const std::optional<clapp::value::exit_t> exit{
parser.parse_and_validate(argc, argv)};
if (exit) {
return exit.value().get_exit_code();
}
Expects(
parser
->string_arg); // parser ensures mandatory arguments are given
std::cout << "string-arg: " << parser->string_arg.value() << std::endl;
Expects(
parser->int_arg); // parser ensures mandatory arguments are given
std::cout << "int-arg: " << parser->int_arg.value() << std::endl;
if (parser->variadic_string_arg) { // if variadic_string_arg is given
std::cout << "variadic-string-arg (size: "
<< parser->variadic_string_arg.value().size() << "): ";
// iterate over the vector of arguments
for (auto &val : parser->variadic_string_arg.value()) {
std::cout << val << ", ";
}
std::cout << std::endl;
} else {
std::cout << "variadic-string-arg: not given" << std::endl;
}
Expects(parser->options.string_param); // The parser ensures that
// mandatory options are given
std::cout << "string_param: '" << parser->options.string_param.value()
<< "'" << std::endl;
if (parser->options
.string_vector_param) { // if string_vector_param is given
std::cout << "string_vector_param (size: "
<< parser->options.string_vector_param.value().size()
<< "): ";
// iterate over the vector of options
for (auto &val : parser->options.string_vector_param.value()) {
std::cout << val << ", ";
}
std::cout << std::endl;
} else {
std::cout << "string_vector_param: not given" << std::endl;
}
} catch (clapp::exception::clapp_exception_t &e) {
std::cout << "Caught ClaPP-Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
} catch (std::exception &e) {
std::cout << "Caught Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}