Skip to content

Commit 2fc8836

Browse files
pks-tgitster
authored andcommitted
parse-options: introduce OPTION_UNSIGNED
We have two generic ways to parse integers in the "parse-options" subsystem: - `OPTION_INTEGER` parses a signed integer. - `OPTION_MAGNITUDE` parses an unsigned integer, but it also interprets suffixes like "k" or "g". Notably missing is a middle ground that parses unsigned integers without interpreting suffixes. Introduce a new `OPTION_UNSIGNED` option type to plug this gap. This option type will be used in subsequent commits. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b3df3a5 commit 2fc8836

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

parse-options.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,60 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
222222
optname(opt, flags));
223223
}
224224
}
225+
case OPTION_UNSIGNED:
226+
{
227+
uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);
228+
uintmax_t value;
229+
230+
if (unset) {
231+
value = 0;
232+
} else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
233+
value = opt->defval;
234+
} else if (get_arg(p, opt, flags, &arg)) {
235+
return -1;
236+
} else if (!*arg) {
237+
return error(_("%s expects a numerical value"),
238+
optname(opt, flags));
239+
} else if (*arg == '-') {
240+
return error(_("%s does not accept negative values"),
241+
optname(opt, flags));
242+
} else {
243+
errno = 0;
244+
value = strtoumax(arg, (char **)&s, 10);
245+
if (*s)
246+
return error(_("%s expects a numerical value"),
247+
optname(opt, flags));
248+
if (errno == ERANGE)
249+
return error(_("value %s for %s not in range [%"PRIuMAX",%"PRIuMAX"]"),
250+
arg, optname(opt, flags), (uintmax_t)0, (uintmax_t)upper_bound);
251+
if (errno)
252+
return error_errno(_("value %s for %s cannot be parsed"),
253+
arg, optname(opt, flags));
254+
255+
}
256+
257+
if (value > upper_bound)
258+
return error(_("value %s for %s not in range [%"PRIuMAX",%"PRIuMAX"]"),
259+
arg, optname(opt, flags), (uintmax_t)0, (uintmax_t)upper_bound);
260+
261+
switch (opt->precision) {
262+
case 1:
263+
*(uint8_t *)opt->value = value;
264+
return 0;
265+
case 2:
266+
*(uint16_t *)opt->value = value;
267+
return 0;
268+
case 4:
269+
*(uint32_t *)opt->value = value;
270+
return 0;
271+
case 8:
272+
*(uint64_t *)opt->value = value;
273+
return 0;
274+
default:
275+
BUG("invalid precision for option %s",
276+
optname(opt, flags));
277+
}
278+
}
225279
case OPTION_MAGNITUDE:
226280
{
227281
uintmax_t upper_bound = UINTMAX_MAX >> (bitsizeof(uintmax_t) - CHAR_BIT * opt->precision);

parse-options.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum parse_opt_type {
2525
/* options with arguments (usually) */
2626
OPTION_STRING,
2727
OPTION_INTEGER,
28+
OPTION_UNSIGNED,
2829
OPTION_MAGNITUDE,
2930
OPTION_CALLBACK,
3031
OPTION_LOWLEVEL_CALLBACK,
@@ -224,6 +225,16 @@ struct option {
224225
.help = (h), \
225226
.flags = (f), \
226227
}
228+
#define OPT_UNSIGNED_F(s, l, v, h, f) { \
229+
.type = OPTION_UNSIGNED, \
230+
.short_name = (s), \
231+
.long_name = (l), \
232+
.value = (v), \
233+
.precision = sizeof(*v), \
234+
.argh = N_("n"), \
235+
.help = (h), \
236+
.flags = (f), \
237+
}
227238

228239
#define OPT_END() { \
229240
.type = OPTION_END, \
@@ -276,6 +287,7 @@ struct option {
276287
#define OPT_CMDMODE(s, l, v, h, i) OPT_CMDMODE_F(s, l, v, h, i, 0)
277288

278289
#define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
290+
#define OPT_UNSIGNED(s, l, v, h) OPT_UNSIGNED_F(s, l, v, h, 0)
279291
#define OPT_MAGNITUDE(s, l, v, h) { \
280292
.type = OPTION_MAGNITUDE, \
281293
.short_name = (s), \

t/helper/test-parse-options.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int cmd__parse_options(int argc, const char **argv)
120120
};
121121
struct string_list expect = STRING_LIST_INIT_NODUP;
122122
struct string_list list = STRING_LIST_INIT_NODUP;
123-
uint16_t m16 = 0;
123+
uint16_t m16 = 0, u16 = 0;
124124
int16_t i16 = 0;
125125

126126
struct option options[] = {
@@ -142,6 +142,7 @@ int cmd__parse_options(int argc, const char **argv)
142142
OPT_GROUP(""),
143143
OPT_INTEGER('i', "integer", &integer, "get a integer"),
144144
OPT_INTEGER(0, "i16", &i16, "get a 16 bit integer"),
145+
OPT_UNSIGNED(0, "u16", &u16, "get a 16 bit unsigned integer"),
145146
OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
146147
OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
147148
OPT_MAGNITUDE(0, "m16", &m16, "get a 16 bit magnitude"),
@@ -215,6 +216,7 @@ int cmd__parse_options(int argc, const char **argv)
215216
show(&expect, &ret, "boolean: %d", boolean);
216217
show(&expect, &ret, "integer: %d", integer);
217218
show(&expect, &ret, "i16: %"PRIdMAX, (intmax_t) i16);
219+
show(&expect, &ret, "u16: %"PRIuMAX, (uintmax_t) u16);
218220
show(&expect, &ret, "magnitude: %lu", magnitude);
219221
show(&expect, &ret, "m16: %"PRIuMAX, (uintmax_t) m16);
220222
show(&expect, &ret, "timestamp: %"PRItime, timestamp);

t/t0040-parse-options.sh

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ usage: test-tool parse-options <options>
2323
-i, --[no-]integer <n>
2424
get a integer
2525
--[no-]i16 <n> get a 16 bit integer
26+
--[no-]u16 <n> get a 16 bit unsigned integer
2627
-j <n> get a integer, too
2728
-m, --magnitude <n> get a magnitude
2829
--m16 <n> get a 16 bit magnitude
@@ -139,6 +140,7 @@ cat >expect <<\EOF
139140
boolean: 2
140141
integer: 1729
141142
i16: 0
143+
u16: 0
142144
magnitude: 16384
143145
m16: 0
144146
timestamp: 0
@@ -161,6 +163,7 @@ cat >expect <<\EOF
161163
boolean: 2
162164
integer: 1729
163165
i16: 9000
166+
u16: 5432
164167
magnitude: 16384
165168
m16: 32768
166169
timestamp: 0
@@ -173,7 +176,7 @@ file: prefix/fi.le
173176
EOF
174177

175178
test_expect_success 'long options' '
176-
test-tool parse-options --boolean --integer 1729 --i16 9000 --magnitude 16k \
179+
test-tool parse-options --boolean --integer 1729 --i16 9000 --u16 5432 --magnitude 16k \
177180
--m16 32k --boolean --string2=321 --verbose --verbose --no-dry-run \
178181
--abbrev=10 --file fi.le --obsolete \
179182
>output 2>output.err &&
@@ -186,6 +189,7 @@ test_expect_success 'abbreviate to something longer than SHA1 length' '
186189
boolean: 0
187190
integer: 0
188191
i16: 0
192+
u16: 0
189193
magnitude: 0
190194
m16: 0
191195
timestamp: 0
@@ -262,6 +266,7 @@ cat >expect <<\EOF
262266
boolean: 1
263267
integer: 13
264268
i16: 0
269+
u16: 0
265270
magnitude: 0
266271
m16: 0
267272
timestamp: 0
@@ -287,6 +292,7 @@ cat >expect <<\EOF
287292
boolean: 0
288293
integer: 2
289294
i16: 0
295+
u16: 0
290296
magnitude: 0
291297
m16: 0
292298
timestamp: 0
@@ -356,6 +362,7 @@ Callback: "four", 0
356362
boolean: 5
357363
integer: 4
358364
i16: 0
365+
u16: 0
359366
magnitude: 0
360367
m16: 0
361368
timestamp: 0
@@ -383,6 +390,7 @@ cat >expect <<\EOF
383390
boolean: 1
384391
integer: 23
385392
i16: 0
393+
u16: 0
386394
magnitude: 0
387395
m16: 0
388396
timestamp: 0
@@ -464,6 +472,7 @@ cat >expect <<\EOF
464472
boolean: 0
465473
integer: 0
466474
i16: 0
475+
u16: 0
467476
magnitude: 0
468477
m16: 0
469478
timestamp: 0
@@ -826,4 +835,17 @@ test_expect_success 'm16 limits range' '
826835
test_grep "value 65536 for option .m16. not in range \[0,65535\]" err
827836
'
828837

838+
test_expect_success 'u16 limits range' '
839+
test-tool parse-options --u16 65535 >out &&
840+
test_grep "u16: 65535" out &&
841+
test_must_fail test-tool parse-options --u16 65536 2>err &&
842+
test_grep "value 65536 for option .u16. not in range \[0,65535\]" err
843+
'
844+
845+
test_expect_success 'u16 does not accept negative value' '
846+
test_must_fail test-tool parse-options --u16 -1 >out 2>err &&
847+
test_grep "option .u16. does not accept negative values" err &&
848+
test_must_be_empty out
849+
'
850+
829851
test_done

0 commit comments

Comments
 (0)