This is playing fast and loose with the typical interpretation of command line "arguments", but I start most of my bash scripts with the following, as an easy way to add --help
support:
if [[ "$@" =~ --help ]]; then
echo 'So, lemme tell you how to work this here script...'
exit
fi
The main drawback is that this will also be triggered by arguments like request--help.log
, --no--help
, etc. (not just --help
, which might be a requirement for your solution).
To apply this method in your case, you would write something like:
[[ "$@" =~ arg4 ]] && echo "Ahoy, arg4 sighted!"
Bonus! If your script requires at least one command line argument, you can similarly trigger a help message when no arguments are supplied:
if [[ "${@---help}" =~ --help ]]; then
echo 'Ok first yer gonna need to find a file...'
exit 1
fi
which uses the empty-variable-substitution syntax ${VAR-default}
to hallucinate a --help
argument if absolutely no arguments were given.