Skip to content

Commit

Permalink
Increased flexibility of the version argument.
Browse files Browse the repository at this point in the history
Fixes: #123
  • Loading branch information
matejak committed Jul 30, 2020
1 parent 01d21e1 commit 3736433
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 10 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Buxfixes:
* Fixed incorrect permission of non-script output files (#104).
* Increased MacOS compatibility by removing terminator from the `chmod` invocation (#107).

New features:
* Increased flexibility of the help option (#92).
* Increased flexibility of the version option (#123).


2.8.1 (2019-06-30)
------------------
Expand Down
9 changes: 7 additions & 2 deletions doc/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,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.
Expand Down
39 changes: 31 additions & 8 deletions src/collectors.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)])],
)])


Expand All @@ -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 <program> <version> 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],
)],
)])
Expand Down
10 changes: 10 additions & 0 deletions src/utilities.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
3 changes: 3 additions & 0 deletions tests/unittests/check-arguments.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
6 changes: 6 additions & 0 deletions tests/unittests/check-utils.m4
Original file line number Diff line number Diff line change
Expand Up @@ -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])

0 comments on commit 3736433

Please sign in to comment.