Skip to content

Commit

Permalink
Started new boolean options.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejak committed Mar 31, 2019
1 parent 810e262 commit 6502dd6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/collectors.m4
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
m4_define([_COLLECTOR_FEEDBACK], [m4_fatal($@)])


dnl
dnl $1: The key
m4_define([_FORMAT_MISSING_PREFIX], [m4_do(
[[The prefix for option '$1' has not been found]],
)])

dnl
dnl $1: The argname(i.e. storage key)
dnl $2: The prefix
m4_define([STORE_NEGATION_PREFIX], [m4_do(
[m4_define([_NEG_PREFIX_FOR_$1], [[$2]])],
)])


dnl
dnl $1: The argname(i.e. storage key)
dnl $2: Error-handling callback that is given the error message as the first argument.
m4_define([GET_NEGATION_PREFIX], [m4_do(
[m4_ifndef([_NEG_PREFIX_FOR_$1],
[m4_default([$2], [m4_fatal])([_FORMAT_MISSING_PREFIX([$1])])],
[m4_quote(m4_indir([_NEG_PREFIX_FOR_$1]))])],
)])


dnl
dnl $1: The argument name
dnl $2: The help message
Expand Down Expand Up @@ -331,6 +355,26 @@ argbash_api([ARG_OPTIONAL_BOOLEAN], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
)])


dnl $1: long name, var suffix (translit of [-] -> _)
dnl $2: short name (opt)
dnl $3: help
argbash_api([ARG_OPTIONAL_SWITCH_ON], _CHECK_PASSED_ARGS_COUNT(1, 3)[m4_do(
[[$0($@)]],
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [off], [bool])],
)])


dnl $1: long name, var suffix (translit of [-] -> _)
dnl $2: short name (opt)
dnl $3: help
dnl $4: the negation prefix (=no-, resulting in i.e. --no-video)
argbash_api([ARG_OPTIONAL_SWITCH_OFF], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do(
[[$0($@)]],
[STORE_NEGATION_PREFIX([$1], m4_default([[$4]], [[no-]]))],
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [on], [bool])],
)])


m4_define([_ARG_OPTIONAL_ACTION],
[_ADD_OPTIONAL_ARGUMENT_IF_POSSIBLE([$1], [$2], [$3], [$4], [action])])

Expand Down
14 changes: 14 additions & 0 deletions tests/unittests/check-arguments.m4
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ assert_equals([not single], _CATH_IS_SINGLE_VALUED(m4_list_nth([_ARGS_CATH], 4),

_DISCARD_VALUES_FOR_ALL_ARGUMENTS()

assert_equals(m4_list_len([_ARGS_LONG]), 0)


ARG_OPTIONAL_SINGLE([foo], [f], [Help,BOMB], [Default])
ARG_POSITIONAL_SINGLE([defaultless], [xhelp])
ARG_POSITIONAL_MULTI([multi-BOMB], [help-BOMB], 3, [one], [two])
Expand Down Expand Up @@ -102,3 +105,14 @@ ARG_OPTIONAL_SINGLE([bar], [f])
assert_equals(m4_list_len([_ERRORS_]), 1)
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.)])
m4_popdef([_COLLECTOR_FEEDBACK])

_DISCARD_VALUES_FOR_ALL_ARGUMENTS()

ARG_OPTIONAL_SWITCH_ON([foo], [f])
assert_equals_list_element([_ARGS_LONG], 1, [foo])
assert_equals_list_element([_ARGS_DEFAULT], 1, [off])

ARG_OPTIONAL_SWITCH_OFF([bar], [b], , [BOMB])
assert_equals_list_element([_ARGS_LONG], 2, [bar])
assert_equals_list_element([_ARGS_DEFAULT], 2, [on])
assert_equals(GET_NEGATION_PREFIX([bar]), [BOMB])
12 changes: 12 additions & 0 deletions tests/unittests/check-collectors.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
m4_include([collectors.m4])

m4_include([test-support.m4])


m4_define([SAVE], [m4_define([SAVED], [$1])])

GET_NEGATION_PREFIX([BOMB], [SAVE])
assert_equals(SAVED, _FORMAT_MISSING_PREFIX([BOMB]))

STORE_NEGATION_PREFIX([lol], [BOMB])
assert_equals(GET_NEGATION_PREFIX([lol]), [BOMB])

0 comments on commit 6502dd6

Please sign in to comment.