change how argparse handles boolean flags

When reporting whether a boolean flag was seen report the actual flags
rather than a summary count. For example, if you have option spec `h/help`
and we parse `-h --help -h` don't do the equivalent of `set _flag_h 3`
do `set _flag_h -h --help -h`.

Partial fix for #4226
This commit is contained in:
Kurtis Rader
2017-07-20 17:54:06 -07:00
parent 23c296f8d5
commit 72968bec42
3 changed files with 17 additions and 9 deletions

View File

@@ -437,6 +437,7 @@ static void populate_option_strings(
// Add a count for how many times we saw each boolean flag but only if we saw the flag at least
// once.
static void update_bool_flag_counts(argparse_cmd_opts_t &opts) {
return;
for (auto it : opts.options) {
auto opt_spec = it.second;
// The '#' short flag is special. It doesn't take any values but isn't a boolean arg.
@@ -539,6 +540,13 @@ static int argparse_parse_flags(argparse_cmd_opts_t &opts, const wchar_t *short_
option_spec_t *opt_spec = found->second;
opt_spec->num_seen++;
if (opt_spec->num_allowed == 0) {
// It's a boolean flag. Save the flag we saw since it might be useful to know if the
// short or long flag was given.
if (long_idx == -1) {
opt_spec->vals.push_back(wcstring(1, L'-') + opt_spec->short_flag);
} else {
opt_spec->vals.push_back(L"--" + opt_spec->long_flag);
}
assert(!w.woptarg);
long_idx = -1;
continue;