Skip to content

Argument Tools

🛠️ Tools · ⬅ Home


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:

Arguments

  • --only - Flag. Optional. Must be first parameter. If calling function ONLY takes the --help parameter 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 --help parameter then throw an argument error if the argument is anything but --help.
  • arguments ... - Arguments. Optional. Arguments passed to calling function to check for --help argument.

Return codes

  • 0 - Help was not found or displayed
  • 1 - Help was found and displayed
  • 2 - Argument error

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]}" "${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 - Success
  • 1 - Environment error
  • 2 - Argument error

_argumentReturn

Handle exit -> 0

Usage

_argumentReturn

Handle exit -> 0

Arguments

  • none

Return codes

  • 0 - Success
  • 1 - Environment error
  • 2 - Argument error

🛠️ Tools · ⬅ Top