Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
trace_api_util.cpp
Go to the documentation of this file.
2
3#include <iostream>
4
5#include <boost/program_options.hpp>
6
7using namespace sysio::trace_api;
8namespace bpo = boost::program_options;
9
11
12namespace {
13 auto create_command_map() {
14 auto result = std::map<std::string, command_registration*>();
16 while (cur != nullptr) {
17 if (result.count(cur->name) > 0) {
18 std::cerr << "Illformed Program, duplicate subcommand: " << cur->name << "\n";
19 exit(1);
20 }
21 result[cur->name] = cur;
22 cur = cur->_next;
23 }
24
25 return result;
26 }
27}
28
29int main(int argc, char** argv) {
30 auto command_map = create_command_map();
31
32 bpo::options_description vis_desc("Options");
33 auto vis_opts = vis_desc.add_options();
34 vis_opts("help,h", "show usage help message");
35
36 bpo::options_description hidden_desc;
37 auto hidden_opts = hidden_desc.add_options();
38 hidden_opts("subargs", bpo::value<std::vector<std::string>>(), "args");
39
40 bpo::positional_options_description pos_desc;
41 pos_desc.add("subargs", -1);
42
43 bpo::options_description cmdline_options;
44 cmdline_options.add(vis_desc).add(hidden_desc);
45
46 bpo::variables_map vm;
47 auto parsed_args = bpo::command_line_parser(argc, argv).options(cmdline_options).positional(pos_desc).allow_unregistered().run();
48 bpo::store(parsed_args, vm);
49
50 std::vector<std::string> args = bpo::collect_unrecognized(parsed_args.options, bpo::include_positional);
51
52 auto show_help = [&](std::ostream& os) {
53 os <<
54 "Usage: trace_api_util <options> command ...\n"
55 "\n"
56 "Commands:\n";
57
58 for (const auto& e: command_map) {
59 os << " " << e.second->name << " " << e.second->slug << "\n";
60 }
61
62 os << "\n" << vis_desc << "\n";
63 };
64
65 if (args.size() < 1) {
66 if (vm.count("help") > 0) {
67 show_help(std::cout);
68 return 0;
69 }
70
71 std::cerr << "Error: No command provided\n\n";
72 show_help(std::cerr);
73 return 1;
74 } else if (command_map.count(args.at(0)) == 0) {
75 std::cerr << "Error: unknown command \"" << args.at(0) << "\"\n\n";
76 show_help(std::cerr);
77 return 1;
78 }
79
80 // trim the command name and pass the rest of the args to the defined command
81 return command_map.at(args.at(0))->func(vm, std::vector<std::string>(args.begin() + 1, args.end()));
82}
os_t os
char ** argv
static command_registration * _list