matejak / argbash

Bash argument parsing code generator
Other
1.4k stars 62 forks source link

Make die work with nounset #98

Closed kamalmarhubi closed 5 years ago

kamalmarhubi commented 5 years ago

I like to keep set -euo pipefail at the top of my scripts. This causes a couple of errors with argbash's die function reading unset variables:

  1. it sets a default for the exit code like in a way that unconditionally reads the second argument

    local _ret=$2 # <---- can fail if only one arg passed in test -n "$_ret" || _ret=1

  2. it reads unconditionally from _PRINT_HELP, which may not be set

This commit changes both of these to use default values in parameter expansion. This construct is part of POSIX, and so should work in both bash and posix mode.

Additionally fix a shellcheck quote warning.

Tested: Verified the generated die function works with nounset.

matejak commented 5 years ago

Looks great, thank you for your contribution!

kamalmarhubi commented 5 years ago

Quick question as I'm completely unfamiliar with m4: is @S|@ standard for $? It took me quite a while to understand that this was an escape for $ and not some other construct.

matejak commented 5 years ago

Yes, argbash is using an extension of m4 called m4sugar. It is documented in the autoconf manual: https://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Quadrigraphs.html