Skip to content

Commit b49251f

Browse files
committed
Fail on INDICATE_SUPPLIED of non-optional args and incr/repeat/action
1 parent 221a476 commit b49251f

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

src/collectors.m4

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ m4_define([__ADD_OPTIONAL_ARGUMENT], [m4_do(
163163
[_FILL_IN_VALUES_FOR_AN_OPTIONAL_ARGUMENT([$1], [$3], _arg_varname, [$5], [$2], [$4])],
164164
[m4_popdef([_arg_varname])],
165165
[m4_define([_DISTINCT_OPTIONAL_ARGS_COUNT], m4_incr(_DISTINCT_OPTIONAL_ARGS_COUNT))],
166+
[m4_case([$5],
167+
[arg], [m4_set_add([_ALL_OPTIONAL_ARGUMENTS], [$1])],
168+
[bool], [m4_set_add([_ALL_OPTIONAL_ARGUMENTS], [$1])],
169+
[dnl]
170+
)],
166171
)])
167172

168173

@@ -525,9 +530,10 @@ m4_define([_ARG_POSITIONAL_DOUBLEDASH], [m4_do(
525530
)])
526531

527532

528-
argbash_api([ARGBASH_INDICATE_SUPPLIED], [m4_do(
533+
argbash_api([ARGBASH_INDICATE_SUPPLIED], _CHECK_PASSED_ARGS_COUNT(1)[m4_do(
529534
[[$0($@)]],
530535
[m4_set_add_all([HAVE_SUPPLIED], $@)],
536+
[CHECK_SUPPLIED_ARE_OPTIONAL([HAVE_SUPPLIED], [_ALL_OPTIONAL_ARGUMENTS], [FATAL_NON_OPTIONAL_SUPPLIED])],
531537
)])
532538

533539

src/utilities.m4

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,20 @@ m4_define([UNDERLINE], [m4_do(
340340
[m4_if(m4_len([$1]), 0, ,
341341
[m4_for(idx, 1, m4_len([$1]), 1, [$2])])],
342342
)])
343+
344+
345+
dnl
346+
dnl $1: Set of arguments marked as supplied
347+
dnl $2: Set of all optional arguments
348+
dnl $3: Macro that gets called with the supplied arguments that are not optional, if any.
349+
m4_define([CHECK_SUPPLIED_ARE_OPTIONAL], [m4_do(
350+
[m4_set_add_all([_temp_supplied_not_optional]m4_set_difference([$1], [$2]))],
351+
[m4_if(m4_set_size([_temp_supplied_not_optional]), 0,
352+
[],
353+
[$3([_temp_supplied_not_optional])])],
354+
[m4_set_delete([_temp_set_supplied])],
355+
)])
356+
357+
358+
m4_define([FATAL_NON_OPTIONAL_SUPPLIED],
359+
[m4_fatal([ARGBASH_INDICATE_SUPPLIED: The following arguments are not optional:] m4_set_dump([$1], [, ]))])

tests/regressiontests/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ TESTS_GEN += \
126126
gen-test-bool-default \
127127
gen-test-pos-opt \
128128
gen-test-pos-opt2 \
129+
gen-test-supplied-non-optional \
129130
gen-test-more \
130131
gen-test-illegal-pos \
131132
gen-test-illegal-opt \
@@ -561,6 +562,9 @@ gen-test-pos-opt: $(TESTDIR)/gen-test-pos-opt.m4 $(ARGBASH_BIN)
561562
gen-test-pos-opt2: $(TESTDIR)/gen-test-pos-opt2.m4 $(ARGBASH_BIN)
562563
ERROR="same_arg" $(REVERSE) $(ARGBASH_EXEC) $< > /dev/null
563564

565+
gen-test-supplied-non-optional: $(TESTDIR)/gen-test-supplied-non-optional.m4 $(ARGBASH_BIN)
566+
ERROR="ARGBASH_INDICATE_SUPPLIED: The following arguments are not optional: phantom, pos-arg-inf, pos-arg-multi, pos-arg-single, opt-arg-action, opt-arg-incr, opt-arg-repeat" $(REVERSE) $(ARGBASH_EXEC) $< > /dev/null
567+
564568
gen-test-more: $(TESTDIR)/gen-test-more.m4 $(ARGBASH_BIN)
565569
ERROR="is unknown" $(REVERSE) $(ARGBASH_EXEC) $< > /dev/null
566570

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# ARG_OPTIONAL_SINGLE([opt-arg-single])
4+
# ARG_OPTIONAL_BOOLEAN([opt-arg-bool])
5+
# ARG_OPTIONAL_REPEATED([opt-arg-repeat])
6+
# ARG_OPTIONAL_INCREMENTAL([opt-arg-incr])
7+
# ARG_OPTIONAL_ACTION([opt-arg-action])
8+
# ARG_POSITIONAL_SINGLE([pos-arg-single])
9+
# ARG_POSITIONAL_MULTI([pos-arg-multi], [], 3)
10+
# ARG_POSITIONAL_INF([pos-arg-inf])
11+
# ARGBASH_INDICATE_SUPPLIED([opt-arg-single], [opt-arg-bool], [opt-arg-repeat], [opt-arg-incr], [opt-arg-action], [pos-arg-single], [pos-arg-multi], [pos-arg-inf], [phantom])
12+
# ARGBASH_GO
13+

tests/regressiontests/make/tests/tests-base.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ ADD_GENTEST_BASH([infinity-illegal], [number of expected positional arguments be
329329
ADD_GENTEST_BASH([bool-default], ['on' or 'off' are allowed as boolean defaults])
330330
ADD_GENTEST_BASH([pos-opt], [same-arg])
331331
ADD_GENTEST_BASH([pos-opt2], [same_arg])
332+
ADD_GENTEST_BASH([supplied-non-optional], [ARGBASH_INDICATE_SUPPLIED: The following arguments are not optional: phantom, pos-arg-inf, pos-arg-multi, pos-arg-single, opt-arg-action, opt-arg-incr, opt-arg-repeat])
332333
ADD_GENTEST_BASH([more], [is unknown])
333334
ADD_GENTEST_BASH([illegal-pos], [contains forbidden characters])
334335
ADD_GENTEST_BASH([illegal-opt], [one character])

tests/unittests/check-utils.m4

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,14 @@ assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 4, [], foo), [foo])
144144
assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 5, [], foo), [foo])
145145
assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 5, [], [BOMB]), [BOMB])
146146
assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 5, [BOMB], foo), [BOMB])
147+
148+
m4_define([ON_NOT_OPTIONAL], [called:][m4_set_dump([$1], [,])])
149+
m4_set_add_all([X], [a], [b], [c])
150+
m4_set_add_all([Y], [b], [c], [d])
151+
assert_equals(CHECK_SUPPLIED_ARE_OPTIONAL([X], [X], [ON_NOT_OPTIONAL]), [])
152+
assert_equals(CHECK_SUPPLIED_ARE_OPTIONAL([X], [Y], [ON_NOT_OPTIONAL]), [called:a])
153+
assert_equals(CHECK_SUPPLIED_ARE_OPTIONAL([Y], [X], [ON_NOT_OPTIONAL]), [called:d])
154+
m4_set_add_all([Y], [x], [y], [z])
155+
assert_equals(CHECK_SUPPLIED_ARE_OPTIONAL([Y], [X], [ON_NOT_OPTIONAL]), [called:z,y,x,d])
156+
m4_set_delete([X])
157+
m4_set_delete([Y])

0 commit comments

Comments
 (0)