diff --git a/ChangeLog b/ChangeLog index f47a7e7..5ec7413 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,7 @@ Buxfixes: New features: * Increased flexibility of the help option (#92). * Improved `argbash-init` script template (#85). +* Increased flexibility of the version option (#123). 2.8.1 (2019-06-30) diff --git a/doc/guide.rst b/doc/guide.rst index 3c40cc5..f92051f 100644 --- a/doc/guide.rst +++ b/doc/guide.rst @@ -262,12 +262,17 @@ Special arguments * Version argument (a special case of an action argument): :: - ARG_VERSION([code to execute when specified]) + ARG_VERSION([code to execute when specified], + [short option (optional, "v" by default)], [long option (optional, "version" by default)], [option description (optional, "Prints version" by default)]) + + By default, it will generate the ``--version`` and ``-v`` action arguments that will print the version information. + You can use the last three arguments to override the default version argument handling, and if you wish to disable the short argument for the version, just leave it blank, and specify either the long argument, or the description. * Enhanced version argument (a special case of an action argument): :: - ARG_VERSION_AUTO([version number or macro containing it]) + ARG_VERSION_AUTO([version number or macro containing it], [additional version message (optional)], + [short option (optional, "v" by default)], [long option (optional, "version" by default)], [option description (optional, "Prints version" by default)]) The macro will take it's first argument, expands it, and treats it as a version number. This allows you to use a quoted macro containing the version number as the first argument. diff --git a/src/collectors.m4 b/src/collectors.m4 index 2fa5ffc..cd38cbe 100644 --- a/src/collectors.m4 +++ b/src/collectors.m4 @@ -200,14 +200,25 @@ argbash_api([ARG_OPTIONAL_SINGLE], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do( )]) +m4_define([_VERSION_DEFAULT_ARGNAME], [[version]]) +m4_define([_VERSION_DEFAULT_SHORT_ARGNAME], [[v]]) +m4_define([_VERSION_DEFAULT_HELP_MSG], [[Prints version]]) + dnl dnl $1 The function to call to get the version -argbash_api([ARG_VERSION], _CHECK_PASSED_ARGS_COUNT(1)[m4_do( +dnl $2: The version command's short option (optional) +dnl $3: The version command's long option (optional) +dnl $4: The version command's description (optional) +argbash_api([ARG_VERSION], _CHECK_PASSED_ARGS_COUNT(1, 4)[m4_do( [dnl Just record how have we called ourselves ], [[$0($@)]], [m4_bmatch(m4_expand([_W_FLAGS]), [V], , - [_ARG_VERSION([$1])])], + [_ARG_VERSION([$1], + m4_default_quoted([$3], _VERSION_DEFAULT_ARGNAME), + _DEFAULT_IF_NARGS_GREATER_THAN([$#], 2, [$2], _VERSION_DEFAULT_SHORT_ARGNAME), + m4_default_quoted([$4], _VERSION_DEFAULT_HELP_MSG), + )])], )]) @@ -233,29 +244,41 @@ m4_define([_VERSION_PRINTF_ARGS], [m4_do( dnl dnl $1: The possibly blank additional message +dnl $2: The version string m4_define([_VERSION_PRINTF_COMMAND], - [[printf] _VERSION_PRINTF_FORMAT([$1]) _VERSION_PRINTF_ARGS(m4_quote("INFERRED_BASENAME" "PROVIDED_VERSION_STRING"), [$1])]) + [[printf] _VERSION_PRINTF_FORMAT([$1]) _VERSION_PRINTF_ARGS(m4_quote("INFERRED_BASENAME" "[$2]"), [$1])]) dnl dnl Try to guess the program name dnl $1 The version string. dnl $2 The version message (incl. quotes) to printf past the simple display. (optional), UNDOCUMENTED NON-FEATURE +dnl $3: The version command's short option (optional) +dnl $4: The version command's long option (optional) +dnl $5: The version command's description (optional) argbash_api([ARG_VERSION_AUTO], _CHECK_PASSED_ARGS_COUNT(1)[m4_do( [[$0($@)]], - [m4_define([PROVIDED_VERSION_STRING], [m4_expand([$1])])], [m4_bmatch(m4_expand([_W_FLAGS]), [V], , - [_ARG_VERSION(_VERSION_PRINTF_COMMAND([$2]))])], + [_ARG_VERSION( + _VERSION_PRINTF_COMMAND([$2], m4_expand([$1])), + m4_default_quoted([$4], _VERSION_DEFAULT_ARGNAME), + _DEFAULT_IF_NARGS_GREATER_THAN([$#], 3, [$3], _VERSION_DEFAULT_SHORT_ARGNAME), + m4_default_quoted([$5], _VERSION_DEFAULT_HELP_MSG), + )])], )]) +dnl $1 What to do wht the version arg is specified +dnl $2 Version long arg name +dnl $3 Version short arg +dnl $4 Version help message m4_define([_ARG_VERSION], [m4_do( [dnl The function with underscore doesn't record what we have just recorded ], [_ARG_OPTIONAL_ACTION( - [version], - [v], - [Prints version], + [$2], + [$3], + [$4], [$1], )], )]) diff --git a/src/utilities.m4 b/src/utilities.m4 index 7e62eb6..5f31793 100644 --- a/src/utilities.m4 +++ b/src/utilities.m4 @@ -11,6 +11,16 @@ m4_define([m4_include_once], [m4_do( m4_include_once([list.m4]) +dnl +dnl $1: nargs +dnl $2: index +dnl $3: actual value +dnl $4: default +m4_define([_DEFAULT_IF_NARGS_GREATER_THAN], [m4_do( + [dnl too little args enough args +], + [m4_if(m4_eval([$1 <= $2]), 1, [m4_default_quoted([$3], [$4])], [$3])], +)]) m4_define([_ENDL_], [m4_for(_, 1, m4_default([$1], 1), 1, [ diff --git a/tests/unittests/check-arguments.m4 b/tests/unittests/check-arguments.m4 index 96a3e17..3d9dcd4 100644 --- a/tests/unittests/check-arguments.m4 +++ b/tests/unittests/check-arguments.m4 @@ -103,3 +103,6 @@ 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]) + +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"]) diff --git a/tests/unittests/check-utils.m4 b/tests/unittests/check-utils.m4 index 5faf058..efd26fc 100644 --- a/tests/unittests/check-utils.m4 +++ b/tests/unittests/check-utils.m4 @@ -138,3 +138,9 @@ assert_equals(_SUBSTITUTE_LF_FOR_NEWLINE_WITH_SPACE_INDENT_AND_ESCAPE_DOUBLEQUOT assert_equals(UNDERLINE([Abc], [+], [=]), [=== Abc +++]) + +assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 3, [], foo), []) +assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 4, [], foo), [foo]) +assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 5, [], foo), [foo]) +assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 5, [], [BOMB]), [BOMB]) +assert_equals(_DEFAULT_IF_NARGS_GREATER_THAN(4, 5, [BOMB], foo), [BOMB])