Skip to content

Implement more convenient boolean option #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -363,6 +387,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 @@ -44,6 +44,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 @@ -106,3 +109,14 @@ m4_popdef([_COLLECTOR_FEEDBACK])

assert_equals(_VERSION_PRINTF_COMMAND([], [1.2]), [printf '%s %s\n\n%s\n' "" "1.2" ''])
assert_equals(_VERSION_PRINTF_COMMAND(["hello"], [1.2]), [printf '%s %s\n\n%s\n%s\n' "" "1.2" '' "hello"])

_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])
Loading