29 {
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) {
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
81 return command_map.at(args.at(0))->func(vm, std::vector<std::string>(args.begin() + 1, args.end()));
82}