Argument Tools
helpArgument
Simple help argument handler.
Usage
helpArgument [ --only ] handlerFunction [ arguments ... ]
Simple help argument handler.
Easy --help handler for any function useful when it's the only option.
Useful for utilities which single argument types, single arguments, and no arguments (except for --help)
Oddly one of the few functions we can not offer the --help flag for.
Without arguments, displays help.
Example: Example: Example: Example:
Location:
bin/build/tools/argument.sh
Arguments
--only- Flag. Optional. Must be first parameter. If calling function ONLY takes the--helpparameter then throw an argument error if the argument is anything but--help.handlerFunction- Function. Required. Must be first or second parameter. If calling function ONLY takes the--helpparameter then throw an argument error if the argument is anything but--help.arguments ...- Arguments. Optional. Arguments passed to calling function to check for--helpargument.
Examples
# NOT DEFINED handler
helpArgument "_${FUNCNAME[0]}" "$@" || return 0
[ "${1-}" != "--help" ] || helpArgument "_${FUNCNAME[0]}" "$@" || return 0
[ $# -eq 0 ] || helpArgument --only "_${FUNCNAME[0]}" "$@" || return "$(convertValue $? 1 0)"
# Argument 1 absolutely exists
[ "${1-}" != "--help" ] || helpArgument "_${FUNCNAME[0]}" "$@" || return 0
# DEFINED handler
local handler="_${FUNCNAME[0]}"
helpArgument "$handler" "$@" || return 0
[ "${1-}" != "--help" ] || helpArgument "$handler" "$@" || return 0
[ $# -eq 0 ] || helpArgument --only "$handler" "$@" || return "$(convertValue $? 1 0)"
# Blank Arguments for help
[ $# -gt 0 ] || helpArgument "_${FUNCNAME[0]}" --help || return 0
[ $# -gt 0 ] || helpArgument "$handler" --help || return 0
Return codes
0- Help was not found or displayed1- Help was found and displayed2- Argument error
Requires
- throwArgument - Run `handler` with an argument error (source)
- bashDocumentation - Universal function documentation (source)
Common argument handling
This is a work in progress and not yet ready for release.
To handle a possible --help ending the arguments early:
# Argument: --help - Optional. Flag. This help.
# Argument: --json - Optional. Flag. Output in JSON.
# Argument: fileName - Required. FileDirectory. File to generate.
myMagic() {
local json fileName
stateFile=$(_arguments "${BASH_SOURCE[0]}" "${NOT-FUNCNAME[0]}" "$@") || return "$(_argumentReturn $?)"
# shellcheck source=/dev/null
source "$stateFile"
# fileName set to a valid file, json is set to true or false
# ...
}
{_arguments}
{_arguments}
{_argumentReturn}