Skip to content

Commit

Permalink
Enable use of 'set -u' (#33)
Browse files Browse the repository at this point in the history
* enable use of 'set -u'
* prevent errors from 'set -u' when $1 or $2 not set
* example of 'set -u'

> `set -u` is useful to detect coding errors where variables are unset, and is part of [unofficial strict mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/) for bash.
> The code still works without `set -u` enabled, of course.

Nice trick, thanks.

> Tested under bash 5.0 and 3.2.

I will setup a travis build with bash 5, too.
Thanks.
  • Loading branch information
rdonkin authored and Sylvain303 committed Jun 8, 2019
1 parent db54f05 commit 1156d73
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docopts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ docopt_print_ARGS() {
}

## main code if sourced with arguments
if [[ "$1" == "--auto" ]] ; then
if [[ $2 == '-G' ]] ; then
if [[ $# -ge 1 && "$1" == "--auto" ]] ; then
if [[ $# -ge 2 && $2 == '-G' ]] ; then
shift 2
eval "$(docopt_auto_parse -G "${BASH_SOURCE[1]}" "$@")"
else
Expand Down
3 changes: 3 additions & 0 deletions examples/docopts_auto_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

# Auto parse needs an empty line after the top comment above ^^^

# docopts.sh is compatible with 'set -u'
set -u

# if docopts is in PATH no need to change it.
PATH=..:$PATH

Expand Down

0 comments on commit 1156d73

Please sign in to comment.