]> git.hungrycats.org Git - bees/commitdiff
options: clean up the parser, prepare for new options with no short form
authorZygo Blaxell <bees@furryterror.org>
Tue, 17 Dec 2024 03:22:31 +0000 (22:22 -0500)
committerZygo Blaxell <bees@furryterror.org>
Tue, 17 Dec 2024 04:32:18 +0000 (23:32 -0500)
We're not adding any more short options, but the debugging code doesn't
work with optvals above 255.  Also clean up constness and variable
lifetimes.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees.cc

index 1268d0f55758c2c2e166df6f3aae307db14c8c71..635ec3f2742b745ed9c8cd26884fb822a4a4c8b8 100644 (file)
@@ -735,10 +735,10 @@ bees_main(int argc, char *argv[])
        // Build getopt_long's short option list from the long_options table.
        // While we're at it, make sure we didn't duplicate any options.
        string getopt_list;
-       set<decltype(option::val)> option_vals;
+       map<decltype(option::val), string> option_vals;
        for (const struct option *op = long_options; op->val; ++op) {
-               THROW_CHECK1(runtime_error, op->val, !option_vals.count(op->val));
-               option_vals.insert(op->val);
+               const auto ins_rv = option_vals.insert(make_pair(op->val, op->name));
+               THROW_CHECK1(runtime_error, op->val, ins_rv.second);
                if ((op->val & 0xff) != op->val) {
                        continue;
                }
@@ -749,16 +749,17 @@ bees_main(int argc, char *argv[])
        }
 
        // Parse options
-       int c;
        while (true) {
                int option_index = 0;
 
-               c = getopt_long(argc, argv, getopt_list.c_str(), long_options, &option_index);
+               const auto c = getopt_long(argc, argv, getopt_list.c_str(), long_options, &option_index);
                if (-1 == c) {
                        break;
                }
 
-               BEESLOGDEBUG("Parsing option '" << static_cast<char>(c) << "'");
+               // getopt_long should have weeded out any invalid options,
+               // so we can go ahead and throw here
+               BEESLOGDEBUG("Parsing option '" << option_vals.at(c) << "'");
 
                switch (c) {