matejak / argbash

Bash argument parsing code generator
Other
1.39k stars 63 forks source link

evaluate_strictness make the script fail if executed with errexit #163

Open nicola-lunghi opened 1 year ago

nicola-lunghi commented 1 year ago

The following statement

evaluate_strictness()
{
    [["$2" =~ ^-(-(config|command|help|version)$|[chv]) ]] && die "You have passed '$2' as a value of argument '$1', which makes it look like that you have omitted the actual value, since '$2' is an option accepted by this script. This is considered a fatal error."
}

Make the script fail if setopt -o errexit is set, as if the comparison is "false" the return value is also false -> and bash kill the process. One solution could be to negate the comparison:

evaluate_strictness()
{
    [[ ! "$2" =~ ^-(-(config|command|help|version)$|[chv]) ]] || die "You have passed '$2' as a value of argument '$1', which makes it look like that you have omitted the actual value, since '$2' is an option accepted by this script. This is considered a fatal error."
}

Or add a return 0 after the [[]], like

evaluate_strictness() { [["$2" =~ ^-(-(config|command|help|version)$|[chv]) ]] && die "You have passed '$2' as a value of argument '$1', which makes it look like that you have omitted the actual value, since '$2' is an option accepted by this script. This is considered a fatal error." return 0 }

nicola-lunghi commented 1 year ago

see my proposed fix https://github.com/matejak/argbash/pull/167