Skip to content

Commit

Permalink
Add function example with improved option parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwmx committed Jun 1, 2020
1 parent 0d1de12 commit c30f4e5
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
79 changes: 79 additions & 0 deletions functions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,85 @@ HEREDOC
fi
}

###############################################################################
# Shell function with parsing for options with values.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
# This example also shows how to do option parsing with values in a function.
###############################################################################

sup() {
# Usage: __option_get_value <option> <value>
__option_get_value() {
local __arg="${1:-}"
local __val="${2:-}"

if [[ -n "${__val:-}" ]] && [[ ! "${__val:-}" =~ ^- ]]
then
printf "%s\\n" "${__val}"
else
_exit_1 printf "%s requires a valid argument.\\n" "${__arg}"
fi
}

local _all=0
local _arguments=()
local _help=0
local _to=

while ((${#}))
do
local __arg="${1:-}"
local __val="${2:-}"

case "${__arg}" in
-a|--all)
_all=1
;;
-h|--help)
_help=1
;;
-t|--to)
_to="$(__option_get_value "${__arg}" "${__val:-}")"
shift
;;
*)
_arguments+=("${__arg}")
;;
esac

shift
done

if ((_help))
then
cat <<HEREDOC
Usage:
sup
sup --all
sup -h | --help
sup (-t | --to) <name>
Options:
--all Say "sup" to everyone.
-h --help Display this usage information.
-t <name> --to <name>
Description:
Say sup.
HEREDOC
elif ((_all))
then
printf "Sup, everyone!\\n"
elif [[ -n "${_to:-}" ]]
then
printf "Sup, %s!\\n" "${_to:-}"
else
printf "Sup!\\n"
fi
}

###############################################################################
# Simple wrapper with help / usage and option flags.
#
Expand Down
87 changes: 87 additions & 0 deletions test/functions.bats
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,90 @@ HEREDOC
_compare "Hey, everyone!" "${output}"
[[ "${output}" == "Hey, everyone!" ]]
}

###############################################################################
# sup()
###############################################################################

_SUP_HELP="$(
cat <<HEREDOC
Usage:
sup
sup --all
sup -h | --help
sup (-t | --to) <name>
Options:
--all Say "sup" to everyone.
-h --help Display this usage information.
-t <name> --to <name>
Description:
Say sup.
HEREDOC
)"

@test "\`sup\` with no arguments returns status 0." {
run sup
[[ "${status}" -eq 0 ]]
}

@test "\`sup\` with no arguments prints a string." {
run sup
[[ "${output}" == "Sup!" ]]
}

@test "\`sup -h\` returns status 0." {
run sup -h
[[ "${status}" -eq 0 ]]
}

@test "\`sup -h\` prints help." {
run sup -h
_compare "${_SUP_HELP}" "${output}"
[[ "${output}" == "${_SUP_HELP}" ]]
}

@test "\`sup --help\` returns status 0." {
run sup --help
[[ "${status}" -eq 0 ]]
}

@test "\`sup --help\` prints help." {
run sup --help
_compare "${_SUP_HELP}" "${output}"
[[ "${output}" == "${_SUP_HELP}" ]]
}

@test "\`sup --all\` returns status 0." {
run sup --all
[[ "${status}" -eq 0 ]]
}

@test "\`sup --all\` prints a string." {
run sup --all
_compare "Sup, everyone!" "${output}"
[[ "${output}" == "Sup, everyone!" ]]
}

@test "\`sup --to Jack\` returns status 0." {
run sup --to Jack
[[ "${status}" -eq 0 ]]
}

@test "\`sup --to Jack\` prints a string." {
run sup --to Jack
_compare "Sup, Jack!" "${output}"
[[ "${output}" == "Sup, Jack!" ]]
}

@test "\`sup -t Jack\` returns status 0." {
run sup -t Jack
[[ "${status}" -eq 0 ]]
}

@test "\`sup -t Jack\` prints a string." {
run sup -t Jack
_compare "Sup, Jack!" "${output}"
[[ "${output}" == "Sup, Jack!" ]]
}

0 comments on commit c30f4e5

Please sign in to comment.