Skip to content

Commit

Permalink
Improved conditionals for arguments count checking (fixes #30).
Browse files Browse the repository at this point in the history
Restored compatibility of scripts with Bash strict modes.
  • Loading branch information
matejak committed Nov 30, 2017
1 parent 3671312 commit 98e89ff
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 10 deletions.
9 changes: 9 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
2.5.1 (TBA)
------------------

Bugfixes:

* Fixed argbash-init corner cases (wrong output filename, ).
* Allowed argbash to wrap scripts in files with no extension.
* Fixed compatibility with the `-e` (i.e. "strict") mode (fixed #30).

2.5.0 (2017-09-03)
------------------

Expand Down
4 changes: 2 additions & 2 deletions bin/argbash
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ parse_commandline ()
handle_passed_args_count ()
{
_required_args_string="'input'"
test ${#_positionals[@]} -lt 1 && _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${#_positionals[@]}." 1
test ${#_positionals[@]} -gt 1 && _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${#_positionals[@]} (the last one was: '${_positionals[*]: -1}')." 1
test ${#_positionals[@]} -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${#_positionals[@]}." 1
test ${#_positionals[@]} -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${#_positionals[@]} (the last one was: '${_positionals[*]: -1}')." 1
}

assign_positional_args ()
Expand Down
2 changes: 1 addition & 1 deletion bin/argbash-1to2
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ parse_commandline ()
handle_passed_args_count ()
{
_required_args_string="'input'"
test ${#_positionals[@]} -lt 1 && _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${#_positionals[@]}." 1
test ${#_positionals[@]} -ge 1 || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require at least 1 (namely: $_required_args_string), but got only ${#_positionals[@]}." 1
}

assign_positional_args ()
Expand Down
2 changes: 1 addition & 1 deletion bin/argbash-init
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ parse_commandline ()

handle_passed_args_count ()
{
test ${#_positionals[@]} -gt 1 && _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect between 0 and 1, but got ${#_positionals[@]} (the last one was: '${_positionals[*]: -1}')." 1
test ${#_positionals[@]} -le 1 || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect between 0 and 1, but got ${#_positionals[@]} (the last one was: '${_positionals[*]: -1}')." 1
}

assign_positional_args ()
Expand Down
8 changes: 4 additions & 4 deletions src/function_generators.m4
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ m4_define([_MAKE_RESTRICT_VALUES_FUNCTION], [m4_do(
m4_define([_CHECK_FOR_TOO_LITTLE_ARGS], [m4_do(
[_INDENT_(1)_required_args_string="m4_list_join([_POSITIONALS_REQUIRED], [, ], , , [ and ])"
],
[_INDENT_(1)[test ${#_positionals[@]} -lt ]],
[_INDENT_(1)[test ${#_positionals[@]} -ge ]],
[_MINIMAL_POSITIONAL_VALUES_COUNT],
[[ && _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require ]],
[[ || _PRINT_HELP=yes die "FATAL ERROR: Not enough positional arguments - we require ]],
[_SPECIFICATION_OF_ACCEPTED_VALUES_COUNT],
[ (namely: $_required_args_string)],
[[, but got only ${#_positionals[@]}." 1
Expand All @@ -76,8 +76,8 @@ m4_define([_CHECK_FOR_TOO_LITTLE_ARGS], [m4_do(


m4_define([_CHECK_FOR_TOO_MANY_ARGS], [m4_do(
[_INDENT_(1)[test ${#_positionals[@]} -gt ]_HIGHEST_POSITIONAL_VALUES_COUNT],
[[ && _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect ]],
[_INDENT_(1)[test ${#_positionals[@]} -le ]_HIGHEST_POSITIONAL_VALUES_COUNT],
[[ || _PRINT_HELP=yes die "FATAL ERROR: There were spurious positional arguments --- we expect ]],
[_SPECIFICATION_OF_ACCEPTED_VALUES_COUNT],
[_IF_SOME_POSITIONAL_VALUES_ARE_EXPECTED([ (namely: $_required_args_string)])],
[dnl The last element of _positionals (even) for bash < 4.3 according to http://unix.stackexchange.com/a/198790
Expand Down
2 changes: 2 additions & 0 deletions tests/regressiontests/test-infinity-mixed.m4
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

# ARG_POSITIONAL_INF([pos-arg], [@pos-arg@], 1, first, second, third)
# ARG_HELP()
# ARGBASH_GO
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/test-ls_like.m4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -e

# ARG_POSITIONAL_SINGLE([arg], [], [foo])
# ARG_OPTIONAL_SINGLE([width], w, [])
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/test-simple.m4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -e

# ARG_POSITIONAL_SINGLE([pos-arg], [help line PEND-\n-PBEGIN])
# ARG_OPTIONAL_SINGLE([prefix],[o],[help line END-\n-BEGIN line 2END-\\n-2BEGIN],[x])
Expand Down

0 comments on commit 98e89ff

Please sign in to comment.