Skip to content

Commit 4043011

Browse files
committed
Started new boolean options.
1 parent aefefef commit 4043011

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

src/collectors.m4

Lines changed: 61 additions & 1 deletion
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
@@ -324,13 +348,49 @@ dnl $3: help
324348
dnl $4: default (=off)
325349
argbash_api([ARG_OPTIONAL_BOOLEAN], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
326350
[[$0($@)]],
327-
[m4_ifnblank([$4], [m4_case([$4], [on], , [off], ,
351+
[m4_ifnblank([$4], [m4_case([$4],
352+
[on], [_ARG_OPTIONAL_SWITCH_ON([$1], [$2], [$3])],
353+
[off], [_ARG_OPTIONAL_SWITCH_OFF([$1], [$2], [$3])],
328354
[_COLLECTOR_FEEDBACK([Problem with argument '$1': Only 'on' or 'off' are allowed as boolean defaults, you have specified '$4'.])])])],
329355
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3],
330356
m4_default_quoted([$4], [off]), [bool])],
331357
)])
332358

333359

360+
dnl $1: long name, var suffix (translit of [-] -> _)
361+
dnl $2: short name (opt)
362+
dnl $3: help
363+
argbash_api([ARG_OPTIONAL_SWITCH_ON], _CHECK_PASSED_ARGS_COUNT(1, 3)[m4_do(
364+
[[$0($@)]],
365+
[_ARG_OPTIONAL_SWITCH_ON($@)],
366+
)])
367+
368+
369+
m4_define([_ARG_OPTIONAL_SWITCH_ON], _CHECK_PASSED_ARGS_COUNT(1, 3)[m4_do(
370+
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [off], [bool])],
371+
)])
372+
373+
374+
m4_define([_DEFAULT_NEGATION_PREFIX], [[no-]])
375+
376+
m4_define([ARG_SET_DEFAULT_NEGATION_PREFIX], [m4_define([_DEFAULT_NEGATION_PREFIX], [[$1]])])
377+
378+
dnl $1: long name, var suffix (translit of [-] -> _)
379+
dnl $2: short name (opt)
380+
dnl $3: help
381+
dnl $4: the negation prefix (=no-, resulting in i.e. --no-video)
382+
argbash_api([ARG_OPTIONAL_SWITCH_OFF], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
383+
[[$0($@)]],
384+
[_ARG_OPTIONAL_SWITCH_OFF($@)],
385+
)])
386+
387+
388+
m4_define([_ARG_OPTIONAL_SWITCH_OFF], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
389+
[STORE_NEGATION_PREFIX([$1], m4_default_quoted([$4], _DEFAULT_NEGATION_PREFIX))],
390+
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [on], [bool])],
391+
)])
392+
393+
334394
m4_define([_ARG_OPTIONAL_ACTION],
335395
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [$4], [action])])
336396

start_tmux.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ tmux send-keys 'vim stuff.m4' C-m
44
tmux new-window -c resources
55
tmux send-keys -l 'make unittests'
66
tmux new-window -c tests/unittests
7-
tmux send-keys -l 'vim '
7+
tmux send-keys -l 'vim -p test-support.m4 '
88
tmux select-window -t argbash:0
99
tmux -2 attach-session -t argbash

tests/unittests/check-arguments.m4

Lines changed: 23 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])
@@ -103,3 +106,23 @@ ARG_OPTIONAL_SINGLE([bar], [f])
103106
assert_equals(m4_list_len([_ERRORS_]), 1)
104107
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.)])
105108
m4_popdef([_COLLECTOR_FEEDBACK])
109+
110+
_DISCARD_VALUES_FOR_ALL_ARGUMENTS()
111+
112+
ARG_OPTIONAL_SWITCH_ON([foo], [f])
113+
assert_equals_list_element([_ARGS_LONG], 1, [foo])
114+
assert_equals_list_element([_ARGS_DEFAULT], 1, [off])
115+
116+
ARG_OPTIONAL_SWITCH_OFF([bar], [b], , [BOMB])
117+
assert_equals_list_element([_ARGS_LONG], 2, [bar])
118+
assert_equals_list_element([_ARGS_DEFAULT], 2, [on])
119+
assert_equals(GET_NEGATION_PREFIX([bar]), [BOMB])
120+
121+
ARG_OPTIONAL_SWITCH_OFF([BOMB])
122+
assert_equals_list_element([_ARGS_LONG], 3, [BOMB])
123+
assert_equals_list_element([_ARGS_DEFAULT], 2, [on])
124+
assert_equals(GET_NEGATION_PREFIX([BOMB]), [no-])
125+
126+
ARG_SET_DEFAULT_NEGATION_PREFIX([BOMB])
127+
ARG_OPTIONAL_SWITCH_OFF([xx])
128+
assert_equals(GET_NEGATION_PREFIX([xx]), [BOMB])

tests/unittests/check-collectors.m4

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)