Skip to content

Commit 9ba0941

Browse files
rscharfegitster
authored andcommitted
parseopt: check for duplicate long names and numerical options
We already check for duplicate short names. Check for and report duplicate long names and numerical options as well. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a5f2ff6 commit 9ba0941

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

parse-options.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "gettext.h"
66
#include "strbuf.h"
77
#include "string-list.h"
8+
#include "strmap.h"
89
#include "utf8.h"
910

1011
static int disallow_abbreviated_options;
@@ -634,6 +635,8 @@ static void check_typos(const char *arg, const struct option *options)
634635
static void parse_options_check(const struct option *opts)
635636
{
636637
char short_opts[128];
638+
struct strset long_names = STRSET_INIT;
639+
bool saw_number_option = false;
637640
void *subcommand_value = NULL;
638641

639642
memset(short_opts, '\0', sizeof(short_opts));
@@ -648,6 +651,16 @@ static void parse_options_check(const struct option *opts)
648651
else if (short_opts[opts->short_name]++)
649652
optbug(opts, "short name already used");
650653
}
654+
if (opts->long_name) {
655+
if (strset_contains(&long_names, opts->long_name))
656+
optbug(opts, "long name already used");
657+
strset_add(&long_names, opts->long_name);
658+
}
659+
if (opts->type == OPTION_NUMBER) {
660+
if (saw_number_option)
661+
optbug(opts, "duplicate numerical option");
662+
saw_number_option = true;
663+
}
651664
if (opts->flags & PARSE_OPT_NODASH &&
652665
((opts->flags & PARSE_OPT_OPTARG) ||
653666
!(opts->flags & PARSE_OPT_NOARG) ||
@@ -705,6 +718,7 @@ static void parse_options_check(const struct option *opts)
705718
optbug(opts, "multi-word argh should use dash to separate words");
706719
}
707720
BUG_if_bug("invalid 'struct option'");
721+
strset_clear(&long_names);
708722
}
709723

710724
static int has_subcommands(const struct option *options)

0 commit comments

Comments
 (0)