Skip to content

Commit a839fae

Browse files
committed
Fail on INDICATE_SUPPLIED of non-optional arguments
1 parent 02ef457 commit a839fae

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

src/collectors.m4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ 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_set_add([_ALL_OPTIONAL_ARGUMENTS], [$1])],
166167
)])
167168

168169

@@ -525,9 +526,10 @@ m4_define([_ARG_POSITIONAL_DOUBLEDASH], [m4_do(
525526
)])
526527

527528

528-
argbash_api([ARGBASH_INDICATE_SUPPLIED], [m4_do(
529+
argbash_api([ARGBASH_INDICATE_SUPPLIED], _CHECK_PASSED_ARGS_COUNT(1)[m4_do(
529530
[[$0($@)]],
530531
[m4_set_add_all([HAVE_SUPPLIED], $@)],
532+
[CHECK_SUPPLIED_ARE_OPTIONAL([HAVE_SUPPLIED], [_ALL_OPTIONAL_ARGUMENTS], [FATAL_NON_OPTIONAL_SUPPLIED])],
531533
)])
532534

533535

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: pos-arg" $(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

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: pos-arg])
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)