Skip to content

Common Argument Handling

🛠️ Tools · ⬅ Top


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:

Argument: argumentName [ variable ... ] - [ Optional | Required ]. argumentType. Description.`

... 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

And uses the associated usageArgument function for validation. Output is a temporary stateFile on line 1

Arguments

  • this - Required. Function. Function to collect arguments for. Assume handler function is "_$this".
  • source - Required. File. File of the function to collect the specification.
  • --none - Flag. Optional. If specified, state file is deleted prior to return regardless of handling.
  • arguments - Optional. String. 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