-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (51 loc) · 1.94 KB
/
main.cpp
File metadata and controls
62 lines (51 loc) · 1.94 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
54
55
56
57
58
59
60
61
62
#include <clapp/argument.h>
#include <clapp/build_info.h>
#include <clapp/exception.h>
#include <clapp/filesystem.h>
#include <clapp/main_parser.h>
#include <clapp/option.h>
#include <clapp/parser.h>
#include <clapp/parser_container.h>
#include <clapp/sub_parser.h>
#include <clapp/type_traits.h>
#include <clapp/value.h>
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() = default;
~cli_parser_t() override;
clapp::help_option_t help{*this, "help", 'h', "Show help options."};
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}};
clapp::string_argument_t string_arg{*this, "string-arg", "String argument"};
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::~cli_parser_t() = default;
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);
}
using parser_t = clapp::parser::basic_parser_container_t<cli_parser_t>;
int main(int argc, char *argv[]) {
try {
parser_t p;
const std::optional<clapp::value::exit_t> exit{
p.parse_and_validate(argc, argv)};
if (exit) {
return exit.value().get_exit_code();
}
Ensures(p->string_arg);
std::cout << "string-arg: " << p->string_arg.value() << std::endl;
} catch (std::exception &e) {
std::cout << "Caught Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}