Skip to content

Commit 6502dd6

Browse files
committed
Started new boolean options.
1 parent 810e262 commit 6502dd6

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/collectors.m4

+44
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
@@ -331,6 +355,26 @@ argbash_api([ARG_OPTIONAL_BOOLEAN], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
331355
)])
332356

333357

358+
dnl $1: long name, var suffix (translit of [-] -> _)
359+
dnl $2: short name (opt)
360+
dnl $3: help
361+
argbash_api([ARG_OPTIONAL_SWITCH_ON], _CHECK_PASSED_ARGS_COUNT(1, 3)[m4_do(
362+
[[$0($@)]],
363+
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [off], [bool])],
364+
)])
365+
366+
367+
dnl $1: long name, var suffix (translit of [-] -> _)
368+
dnl $2: short name (opt)
369+
dnl $3: help
370+
dnl $4: the negation prefix (=no-, resulting in i.e. --no-video)
371+
argbash_api([ARG_OPTIONAL_SWITCH_OFF], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
372+
[[$0($@)]],
373+
[STORE_NEGATION_PREFIX([$1], m4_default([[$4]], [[no-]]))],
374+
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [on], [bool])],
375+
)])
376+
377+
334378
m4_define([_ARG_OPTIONAL_ACTION],
335379
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [$4], [action])])
336380

tests/unittests/check-arguments.m4

+14
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ assert_equals([not single], _CATH_IS_SINGLE_VALUED(m4_list_nth([_ARGS_CATH], 4),
4343

4444
_DISCARD_VALUES_FOR_ALL_ARGUMENTS()
4545

46+
assert_equals(m4_list_len([_ARGS_LONG]), 0)
47+
48+
4649
ARG_OPTIONAL_SINGLE([foo], [f], [Help,BOMB], [Default])
4750
ARG_POSITIONAL_SINGLE([defaultless], [xhelp])
4851
ARG_POSITIONAL_MULTI([multi-BOMB], [help-BOMB], 3, [one], [two])
@@ -102,3 +105,14 @@ ARG_OPTIONAL_SINGLE([bar], [f])
102105
assert_equals(m4_list_len([_ERRORS_]), 1)
103106
m4_bmatch(m4_list_nth([_ERRORS_], 1), ['f'.*already used], [], [m4_fatal([Expected error reflecting duplicate short option, got] 'm4_list_nth([_ERRORS_], 1)' instead.)])
104107
m4_popdef([_COLLECTOR_FEEDBACK])
108+
109+
_DISCARD_VALUES_FOR_ALL_ARGUMENTS()
110+
111+
ARG_OPTIONAL_SWITCH_ON([foo], [f])
112+
assert_equals_list_element([_ARGS_LONG], 1, [foo])
113+
assert_equals_list_element([_ARGS_DEFAULT], 1, [off])
114+
115+
ARG_OPTIONAL_SWITCH_OFF([bar], [b], , [BOMB])
116+
assert_equals_list_element([_ARGS_LONG], 2, [bar])
117+
assert_equals_list_element([_ARGS_DEFAULT], 2, [on])
118+
assert_equals(GET_NEGATION_PREFIX([bar]), [BOMB])

tests/unittests/check-collectors.m4

+12
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)