Skip to content

Commit bb4c1a5

Browse files
committed
Started new boolean options.
1 parent 7f752ed commit bb4c1a5

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/collectors.m4

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
m4_define([_COLLECTOR_FEEDBACK], [m4_fatal($@)])
22

33

4+
dnl
5+
dnl $1: The key
6+
m4_define([_FORMAT_MISSING_PREFIX], [m4_do(
7+
[[The prefix for option '$1' has not been found]],
8+
)])
9+
10+
dnl
11+
dnl $1: The argname(i.e. storage key)
12+
dnl $2: The prefix
13+
m4_define([STORE_NEGATION_PREFIX], [m4_do(
14+
[m4_define([_NEG_PREFIX_FOR_$1], [[$2]])],
15+
)])
16+
17+
18+
dnl
19+
dnl $1: The argname(i.e. storage key)
20+
dnl $2: Error-handling callback that is given the error message as the first argument.
21+
m4_define([GET_NEGATION_PREFIX], [m4_do(
22+
[m4_ifndef([_NEG_PREFIX_FOR_$1],
23+
[m4_default([$2], [m4_fatal])([_FORMAT_MISSING_PREFIX([$1])])],
24+
[m4_quote(m4_indir([_NEG_PREFIX_FOR_$1]))])],
25+
)])
26+
27+
428
dnl
529
dnl $1: The argument name
630
dnl $2: The help message
@@ -363,6 +387,26 @@ argbash_api([ARG_OPTIONAL_BOOLEAN], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
363387
)])
364388

365389

390+
dnl $1: long name, var suffix (translit of [-] -> _)
391+
dnl $2: short name (opt)
392+
dnl $3: help
393+
argbash_api([ARG_OPTIONAL_SWITCH_ON], _CHECK_PASSED_ARGS_COUNT(1, 3)[m4_do(
394+
[[$0($@)]],
395+
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [off], [bool])],
396+
)])
397+
398+
399+
dnl $1: long name, var suffix (translit of [-] -> _)
400+
dnl $2: short name (opt)
401+
dnl $3: help
402+
dnl $4: the negation prefix (=no-, resulting in i.e. --no-video)
403+
argbash_api([ARG_OPTIONAL_SWITCH_OFF], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
404+
[[$0($@)]],
405+
[STORE_NEGATION_PREFIX([$1], m4_default([[$4]], [[no-]]))],
406+
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [on], [bool])],
407+
)])
408+
409+
366410
m4_define([_ARG_OPTIONAL_ACTION],
367411
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [$4], [action])])
368412

tests/unittests/check-arguments.m4

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ assert_equals([not single], _CATH_IS_SINGLE_VALUED(m4_list_nth([_ARGS_CATH], 4),
4444

4545
_DISCARD_VALUES_FOR_ALL_ARGUMENTS()
4646

47+
assert_equals(m4_list_len([_ARGS_LONG]), 0)
48+
49+
4750
ARG_OPTIONAL_SINGLE([foo], [f], [Help,BOMB], [Default])
4851
ARG_POSITIONAL_SINGLE([defaultless], [xhelp])
4952
ARG_POSITIONAL_MULTI([multi-BOMB], [help-BOMB], 3, [one], [two])
@@ -106,3 +109,14 @@ m4_popdef([_COLLECTOR_FEEDBACK])
106109

107110
assert_equals(_VERSION_PRINTF_COMMAND([], [1.2]), [printf '%s %s\n\n%s\n' "" "1.2" ''])
108111
assert_equals(_VERSION_PRINTF_COMMAND(["hello"], [1.2]), [printf '%s %s\n\n%s\n%s\n' "" "1.2" '' "hello"])
112+
113+
_DISCARD_VALUES_FOR_ALL_ARGUMENTS()
114+
115+
ARG_OPTIONAL_SWITCH_ON([foo], [f])
116+
assert_equals_list_element([_ARGS_LONG], 1, [foo])
117+
assert_equals_list_element([_ARGS_DEFAULT], 1, [off])
118+
119+
ARG_OPTIONAL_SWITCH_OFF([bar], [b], , [BOMB])
120+
assert_equals_list_element([_ARGS_LONG], 2, [bar])
121+
assert_equals_list_element([_ARGS_DEFAULT], 2, [on])
122+
assert_equals(GET_NEGATION_PREFIX([bar]), [BOMB])
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
m4_include([collectors.m4])
2+
3+
m4_include([test-support.m4])
4+
5+
6+
m4_define([SAVE], [m4_define([SAVED], [$1])])
7+
8+
GET_NEGATION_PREFIX([BOMB], [SAVE])
9+
assert_equals(SAVED, _FORMAT_MISSING_PREFIX([BOMB]))
10+
11+
STORE_NEGATION_PREFIX([lol], [BOMB])
12+
assert_equals(GET_NEGATION_PREFIX([lol]), [BOMB])

0 commit comments

Comments
 (0)