parse-options: Allow abbreviated options when unambiguous
When there is an option "--amend", the option parser now recognizes "--am" for that option, provided that there is no other option beginning with "--am". Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Johannes Schindelin committed
Oct 14, 2007 at 17:54 UTC
7f275b91520d31bfbe43ec5a9bbaf8ac6e663ce0
2 files changed
+60
parse-options.c
+37
@@ -113,6 +113,13 @@ static int parse_short_opt(struct optparse_t *p, const struct option *options)
113
static int parse_long_opt(struct optparse_t *p, const char *arg,
114
const struct option *options)
115
{
116
+ const char *arg_end = strchr(arg, '=');
117
+ const struct option *abbrev_option = NULL;
118
+ int abbrev_flags = 0;
119
+
120
+ if (!arg_end)
121
+ arg_end = arg + strlen(arg);
122
+
123
for (; options->type != OPTION_END; options++) {
124
const char *rest;
125
int flags = 0;
@@ -122,10 +129,38 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
129
130
rest = skip_prefix(arg, options->long_name);
131
if (!rest) {
132
+ /* abbreviated? */
133
+ if (!strncmp(options->long_name, arg, arg_end - arg)) {
134
+is_abbreviated:
135
+ if (abbrev_option)
136
+ return error("Ambiguous option: %s "
137
+ "(could be --%s%s or --%s%s)",
138
+ arg,
139
+ (flags & OPT_UNSET) ?
140
+ "no-" : "",
141
+ options->long_name,
142
+ (abbrev_flags & OPT_UNSET) ?
143
+ "no-" : "",
144
+ abbrev_option->long_name);
145
+ if (!(flags & OPT_UNSET) && *arg_end)
146
+ p->opt = arg_end + 1;
147
+ abbrev_option = options;
148
+ abbrev_flags = flags;
149
+ continue;
150
+ }
151
+ /* negated and abbreviated very much? */
152
+ if (!prefixcmp("no-", arg)) {
153
+ flags |= OPT_UNSET;
154
+ goto is_abbreviated;
155
+ }
156
+ /* negated? */
157
if (strncmp(arg, "no-", 3))
158
continue;
159
flags |= OPT_UNSET;
160
rest = skip_prefix(arg + 3, options->long_name);
161
+ /* abbreviated and negated? */
162
+ if (!rest && !prefixcmp(options->long_name, arg + 3))
163
+ goto is_abbreviated;
164
if (!rest)
165
continue;
166
}
@@ -136,6 +171,8 @@ static int parse_long_opt(struct optparse_t *p, const char *arg,
171
}
172
return get_value(p, options, flags);
173
}
174
+ if (abbrev_option)
175
+ return get_value(p, abbrev_option, abbrev_flags);
176
return error("unknown option `%s'", arg);
177
}
178
t/t0040-parse-options.sh
+23
@@ -67,4 +67,27 @@ test_expect_success 'intermingled arguments' '
67
git diff expect output
68
'
69
70
+cat > expect << EOF
71
+boolean: 0
72
+integer: 2
73
+string: (not set)
74
+EOF
75
+
76
+test_expect_success 'unambiguously abbreviated option' '
77
+ test-parse-options --int 2 --boolean --no-bo > output 2> output.err &&
78
+ test ! -s output.err &&
79
+ git diff expect output
80
+'
81
+
82
+test_expect_success 'unambiguously abbreviated option with "="' '
83
+ test-parse-options --int=2 > output 2> output.err &&
84
+ test ! -s output.err &&
85
+ git diff expect output
86
+'
87
+
88
+test_expect_failure 'ambiguously abbreviated option' '
89
+ test-parse-options --strin 123;
90
+ test $? != 129
91
+'
92
+
93
test_done