|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @author Michal Sedlak <[email protected]> |
| 4 | + * @brief Argument parser component |
| 5 | + */ |
| 6 | + |
| 7 | +#include <common/argParser.hpp> |
| 8 | + |
| 9 | +#include <algorithm> |
| 10 | +#include <map> |
| 11 | + |
| 12 | +#include <getopt.h> |
| 13 | + |
| 14 | +namespace fdsdump { |
| 15 | + |
| 16 | +bool Args::has(char short_opt) const { |
| 17 | + return std::any_of(m_named_args.begin(), m_named_args.end(), [=](const NamedArg &arg) { return arg.short_opt == short_opt; }); |
| 18 | +} |
| 19 | + |
| 20 | +bool Args::has(const std::string &long_opt) const { |
| 21 | + return std::any_of(m_named_args.begin(), m_named_args.end(), [&](const NamedArg &arg) { return arg.long_opt == long_opt; }); |
| 22 | +} |
| 23 | + |
| 24 | +bool Args::has(int pos) const { |
| 25 | + return pos < int(m_pos_args.size()); |
| 26 | +} |
| 27 | + |
| 28 | +bool Args::count(char short_opt) const { |
| 29 | + return std::count_if(m_named_args.begin(), m_named_args.end(), [=](const NamedArg &arg) { return arg.short_opt == short_opt; }); |
| 30 | +} |
| 31 | + |
| 32 | +bool Args::count(const std::string &long_opt) const { |
| 33 | + return std::count_if(m_named_args.begin(), m_named_args.end(), [&](const NamedArg &arg) { return arg.long_opt == long_opt; }); |
| 34 | +} |
| 35 | + |
| 36 | +bool Args::pos_count() const { |
| 37 | + return m_pos_args.size(); |
| 38 | +} |
| 39 | + |
| 40 | +std::string Args::get(char short_opt) const { |
| 41 | + auto it = std::find_if(m_named_args.rbegin(), m_named_args.rend(), [&](const NamedArg &arg) { return arg.short_opt == short_opt; }); |
| 42 | + if (it == m_named_args.rend()) { |
| 43 | + return ""; |
| 44 | + } |
| 45 | + return it->value; |
| 46 | +} |
| 47 | + |
| 48 | +std::string Args::get(const std::string &long_opt) const { |
| 49 | + auto it = std::find_if(m_named_args.rbegin(), m_named_args.rend(), [&](const NamedArg &arg) { return arg.long_opt == long_opt; }); |
| 50 | + if (it == m_named_args.rend()) { |
| 51 | + return ""; |
| 52 | + } |
| 53 | + return it->value; |
| 54 | +} |
| 55 | + |
| 56 | +std::string Args::get(int pos) const { |
| 57 | + if (!has(pos)) { |
| 58 | + return ""; |
| 59 | + } |
| 60 | + return m_pos_args[pos]; |
| 61 | +} |
| 62 | + |
| 63 | +std::vector<std::string> Args::get_all(char short_opt) const { |
| 64 | + std::vector<std::string> values; |
| 65 | + for (const auto &arg : m_named_args) { |
| 66 | + if (arg.short_opt == short_opt) { |
| 67 | + values.push_back(arg.value); |
| 68 | + } |
| 69 | + } |
| 70 | + return values; |
| 71 | +} |
| 72 | + |
| 73 | +std::vector<std::string> Args::get_all(const std::string &long_opt) const { |
| 74 | + std::vector<std::string> values; |
| 75 | + for (const auto &arg : m_named_args) { |
| 76 | + if (arg.long_opt == long_opt) { |
| 77 | + values.push_back(arg.value); |
| 78 | + } |
| 79 | + } |
| 80 | + return values; |
| 81 | +} |
| 82 | + |
| 83 | +void ArgParser::add(char short_opt, const std::string &long_opt, bool requires_value) { |
| 84 | + m_defs.push_back({short_opt, long_opt, requires_value}); |
| 85 | +} |
| 86 | + |
| 87 | +void ArgParser::add(char short_opt, bool requires_value) { |
| 88 | + m_defs.push_back({short_opt, "", requires_value}); |
| 89 | +} |
| 90 | + |
| 91 | +void ArgParser::add(const std::string &long_opt, bool requires_value) { |
| 92 | + m_defs.push_back({0, long_opt, requires_value}); |
| 93 | +} |
| 94 | + |
| 95 | +Args ArgParser::parse(int argc, char **argv) const { |
| 96 | + std::string short_opts = ":"; |
| 97 | + std::vector<option> long_opts; |
| 98 | + int next_nonchar_val = 256; |
| 99 | + |
| 100 | + std::map<int, const ArgDef *> val_to_arg_map; |
| 101 | + |
| 102 | + for (const auto &def : m_defs) { |
| 103 | + if (def.short_opt != 0) { |
| 104 | + short_opts.push_back(def.short_opt); |
| 105 | + if (def.requires_value) { |
| 106 | + short_opts.append(":"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + option getopt_option = {}; |
| 111 | + |
| 112 | + if (!def.long_opt.empty()) { |
| 113 | + getopt_option.name = def.long_opt.c_str(); |
| 114 | + } |
| 115 | + |
| 116 | + if (def.short_opt == 0) { |
| 117 | + getopt_option.val = next_nonchar_val; |
| 118 | + next_nonchar_val++; |
| 119 | + } else { |
| 120 | + getopt_option.val = def.short_opt; |
| 121 | + } |
| 122 | + |
| 123 | + if (def.requires_value) { |
| 124 | + getopt_option.has_arg = required_argument; |
| 125 | + } else { |
| 126 | + getopt_option.has_arg = no_argument; |
| 127 | + } |
| 128 | + |
| 129 | + long_opts.push_back(getopt_option); |
| 130 | + val_to_arg_map.emplace(getopt_option.val, &def); |
| 131 | + } |
| 132 | + |
| 133 | + long_opts.push_back({0, 0, 0, 0}); |
| 134 | + |
| 135 | + // Parse args |
| 136 | + Args parsed; |
| 137 | + opterr = 0; |
| 138 | + int opt; |
| 139 | + |
| 140 | + while ((opt = getopt_long(argc, argv, short_opts.c_str(), long_opts.data(), nullptr)) != -1) { |
| 141 | + if (opt == '?') { |
| 142 | + throw ArgParser::UnknownArgument {argv[optind - 1]}; |
| 143 | + } |
| 144 | + if (opt == ':') { |
| 145 | + throw ArgParser::MissingArgument {argv[optind - 1]}; |
| 146 | + } |
| 147 | + |
| 148 | + const ArgDef *def = val_to_arg_map[opt]; |
| 149 | + |
| 150 | + Args::NamedArg arg; |
| 151 | + arg.short_opt = def->short_opt; |
| 152 | + arg.long_opt = def->long_opt; |
| 153 | + if (optarg) { |
| 154 | + arg.value = optarg; |
| 155 | + } |
| 156 | + parsed.m_named_args.push_back(arg); |
| 157 | + } |
| 158 | + |
| 159 | + // Collect remaining positional arguments |
| 160 | + for (int i = optind; i < argc; i++) { |
| 161 | + parsed.m_pos_args.push_back(argv[i]); |
| 162 | + } |
| 163 | + |
| 164 | + return parsed; |
| 165 | +} |
| 166 | + |
| 167 | +} // fdsdump |
0 commit comments