From 79a5aa95fc5cd37a676c1f19c6840e381a097e18 Mon Sep 17 00:00:00 2001 From: William Melody Date: Mon, 14 May 2018 20:22:35 -0700 Subject: [PATCH] Update boilerplate to latest from bash-boilerplate. Source: https://github.com/alphabetum/bash-boilerplate --- hosts | 175 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 107 insertions(+), 68 deletions(-) diff --git a/hosts b/hosts index 7f9ae55..d185828 100755 --- a/hosts +++ b/hosts @@ -68,11 +68,13 @@ export _TAB_SPACE_CC_="[${_TAB_SPACE_}]" # A simple function for executing a specified command if the `$_USE_DEBUG` # variable has been set. The command is expected to print a message and # should typically be either `echo`, `printf`, or `cat`. +__DEBUG_COUNTER=0 _debug() { if [[ "${_USE_DEBUG:-"0"}" -eq 1 ]] - then + then + __DEBUG_COUNTER=$((__DEBUG_COUNTER+1)) # Prefix debug message with "bug (U+1F41B)" - printf "🐛 " + printf "🐛 %s " "${__DEBUG_COUNTER}" "${@}" printf "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\\n" fi @@ -366,8 +368,7 @@ _main() { _debug printf "main(): entering...\\n" _debug printf "main() \${_CMD} (upon entering): %s\\n" "${_CMD}" - # If $_CMD is blank, then set to `$HOSTS_DEFAULT_COMMAND` - if [[ -z ${_CMD} ]] + if [[ -z "${_CMD}" ]] then _CMD="${HOSTS_DEFAULT_COMMAND}" fi @@ -381,11 +382,7 @@ _main() { # Pass all comment arguments to the program except for the first ($0). ${_CMD} "${_COMMAND_PARAMETERS[@]:-}" else - local _parameters=() - _parameters+=("${_CMD}") - _parameters+=(${_COMMAND_PARAMETERS[@]:-}) - - "${HOSTS_DEFAULT_COMMAND}" "${_parameters[@]:-}" + _die printf "Unknown command: %s\\n" "${_CMD}" fi } @@ -398,8 +395,12 @@ _main() { # Usage: # _function_exists "possible_function_name" # -# Takes a potential function name as an argument and returns whether a function -# exists with that name. +# Returns: +# 0 If a function with the given name is defined in the current environment. +# 1 If not. +# +# Other implementations, some with better performance: +# http://stackoverflow.com/q/85880 _function_exists() { [ "$(type -t "${1}")" == 'function' ] } @@ -409,10 +410,11 @@ _function_exists() { # Usage: # _command_exists "possible_command_name" # -# Takes a potential command name as an argument and returns whether a command -# exists with that name. +# Returns: +# 0 If a command with the given name is defined in the current environment. +# 1 If not. # -# For information on why `hash` is used here, see: +# Information on why `hash` is used here: # http://stackoverflow.com/a/677212 _command_exists() { hash "${1}" 2>/dev/null @@ -423,14 +425,15 @@ _command_exists() { # Usage: # _contains "$item" "${list[*]}" # -# Takes an item and a list and determines whether the list contains the item. +# Returns: +# 0 If the item is included in the list. +# 1 If not. _contains() { local _test_list=(${*:2}) - - for _test_element in "${_test_list[@]:-}" + for __test_element in "${_test_list[@]:-}" do - _debug printf "_contains() \${_test_element}: %s\\n" "${_test_element}" - if [[ "${_test_element}" == "${1}" ]] + _debug printf "_contains() \${__test_element}: %s\\n" "${__test_element}" + if [[ "${__test_element}" == "${1}" ]] then _debug printf "_contains() match: %s\\n" "${1}" return 0 @@ -462,8 +465,10 @@ _join() { # Usage: # _command_argv_includes "an_argument" # -# Takes a possible command argument and determines whether it is included in -# the command argument list. +# Returns: +# 0 If the argument is included in `$_COMMAND_ARGV`, the program's command +# argument list. +# 1 If not. # # This is a shortcut for simple cases where a command wants to check for the # presence of options quickly without parsing the options again. @@ -476,7 +481,9 @@ _command_argv_includes() { # Usage: # _blank "$an_argument" # -# Takes an argument and returns true if it is blank. +# Returns: +# 0 If the argument is not present or null. +# 1 If the argument is present and not null. _blank() { [[ -z "${1:-}" ]] } @@ -486,11 +493,37 @@ _blank() { # Usage: # _present "$an_argument" # -# Takes an argument and returns true if it is present. +# Returns: +# 0 If the argument is present and not null. +# 1 If the argument is not present or null. _present() { [[ -n "${1:-}" ]] } +# _interactive_input() +# +# Usage: +# _interactive_input +# +# Returns: +# 0 If the current input is interactive (eg, a shell). +# 1 If the current input is stdin / piped input. +_interactive_input() { + [[ -t 0 ]] +} + +# _piped_input() +# +# Usage: +# _piped_input +# +# Returns: +# 0 If the current input is stdin / piped input. +# 1 If the current input is interactive (eg, a shell). +_piped_input() { + ! _interactive_input +} + # _verify_write_permissions # # Print a helpful error message when the specified operation can't be @@ -518,59 +551,65 @@ sudo !!\\n" # desc() # # Usage: -# desc command "description" +# desc +# desc --get # -# Create a description for a specified command name. The command description -# text can be passed as the second argument or as standard input. +# Options: +# --get Print the description for if one has been set. # -# To make the description text available to other functions, desc() assigns the -# text to a variable with the format $_desc_function_name +# Examples: +# ``` +# desc "list" <. The +# text can be passed as the second argument or as standard input. +# +# To make the text available to other functions, `desc()` assigns +# the text to a variable with the format `$___desc_`. +# +# When the `--get` option is used, the description for is printed, if +# one has been set. desc() { - set +e - [[ -z ${1:-} ]] && _die printf "desc: No command name specified.\\n" - if [[ -n ${2:-} ]] - then - read -r -d '' "_desc_${1}" < -# -# Prints the description for a given command, provided the description has been -# set using the desc() function. -_print_desc() { - local _var="_desc_${1:-}" + _debug printf "desc() set with argument: \${___desc_%s}\\n" "${1}" + else # no argument is present, so assume piped input + # `read` exits with non-zero status when a delimeter is not found, so + # avoid errors by ending statement with `|| true`. + read -r -d '' "___desc_${1}" || true - if [[ -n "${!_var:-}" ]] - then - printf "%s\\n" "${!_var}" - else - printf "No additional information for \`%s\`\\n" "${1}" + _debug printf "desc() set with pipe: \${___desc_%s}\\n" "${1}" + fi fi } @@ -646,7 +685,7 @@ Help: $(commands) HEREDOC else - _print_desc "${1}" + desc --get "${1}" fi } @@ -1246,5 +1285,5 @@ unblock() { # Run Program ############################################################################### -# Calling the _main function after everything has been defined. +# Call the `_main` function after everything has been defined. _main