Common Argument Handling
This is a work in progress and not yet ready for release.
Sample usage
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]}" "${FUNCNAME[0]}" "$@") || return "$(_argumentReturn $?)"
# shellcheck source=/dev/null
source "$stateFile"
# fileName set to a valid file, json is set to true or false
# ...
}
_arguments
Generic argument parsing using Bash comments.
Usage
_arguments this source [ --none ] [ arguments ... ]
Generic argument parsing using Bash comments.
Argument formatting (in comments) is as follows:
... token means one or more arguments may be passed.
argumentType is one of:
- File FileDirectory Directory LoadEnvironmentFile RealDirectory
- EmptyString String
- Boolean PositiveInteger Integer UnsignedInteger Number
- Executable Callable Function
- URL
Output is a temporary stateFile on line 1
Arguments
this- Function. Required. Function to collect arguments for. Assume handler function is "_$this".source- File. Required. File of the function to collect the specification.--none- Flag. Optional. If specified, state file is deleted prior to return regardless of handling.arguments ...- EmptyString. Optional. One or more arguments to parse.
Return codes
0- Success1- Environment error2- Argument error
_argumentReturn
Handle
exit-> 0
Usage
_argumentReturn
Handle exit -> 0
Arguments
- none
Return codes
0- Success1- Environment error2- Argument error