1
0
mirror of https://github.com/octoleo/hosts.git synced 2024-11-26 06:46:43 +00:00

Update boilerplate to reflect latest updates from source.

This commit is contained in:
William Melody 2015-06-27 20:42:55 -07:00
parent 8b0fc0fcfe
commit a09d0fc893

138
hosts
View File

@ -117,7 +117,7 @@ die() {
############################################################################### ###############################################################################
# Get raw options for any commands that expect them. # Get raw options for any commands that expect them.
raw_options="$*" _RAW_OPTIONS="$*"
# Steps: # Steps:
# #
@ -202,17 +202,17 @@ unset options
# Parse Options ############################################################### # Parse Options ###############################################################
# Initialize command_argv array # Initialize $_COMMAND_ARGV array
# #
# This array contains all of the arguments that get passed along to each # This array contains all of the arguments that get passed along to each
# command. This is essentially the same as the program arguments, minus those # command. This is essentially the same as the program arguments, minus those
# that have been filtered out in the program option parsing loop. This array # that have been filtered out in the program option parsing loop. This array
# is initialized with $0, which is the program's name. # is initialized with $0, which is the program's name.
command_argv=("$0") _COMMAND_ARGV=("$0")
# Initialize $cmd and $_use_debug, which can continue to be blank depending on # Initialize $_CMD and `$_USE_DEBUG`, which can continue to be blank depending
# what the program needs. # on what the program needs.
cmd="" _CMD=""
_use_debug=0 _USE_DEBUG=0
while [ $# -gt 0 ] while [ $# -gt 0 ]
do do
@ -220,36 +220,36 @@ do
shift shift
case "$opt" in case "$opt" in
-h|--help) -h|--help)
cmd="help" _CMD="help"
;; ;;
--version) --version)
cmd="version" _CMD="version"
;; ;;
--debug) --debug)
_use_debug=1 _USE_DEBUG=1
;; ;;
*) *)
# The first non-option argument is assumed to be the command name. # The first non-option argument is assumed to be the command name.
# All subsequent arguments are added to $command_arguments. # All subsequent arguments are added to $_COMMAND_ARGV.
if [[ -n $cmd ]] if [[ -n $_CMD ]]
then then
command_argv+=("$opt") _COMMAND_ARGV+=("$opt")
else else
cmd="$opt" _CMD="$opt"
fi fi
;; ;;
esac esac
done done
# Set $command_parameters to $command_argv, minus the initial element, $0. This # Set $_COMMAND_PARAMETERS to $_COMMAND_ARGV, minus the initial element, $0. This
# provides an array that is equivalent to $* and $@ within each command # provides an array that is equivalent to $* and $@ within each command
# function, though the array is zero-indexed, which could lead to confusion. # function, though the array is zero-indexed, which could lead to confusion.
command_parameters=("${command_argv[@]:1}") _COMMAND_PARAMETERS=("${_COMMAND_ARGV[@]:1}")
_debug printf "\$cmd: %s\n" "$cmd" _debug printf "\$_CMD: %s\n" "$_CMD"
_debug printf "\$raw_options (one per line):\n%s\n" "$raw_options" _debug printf "\$_RAW_OPTIONS (one per line):\n%s\n" "$_RAW_OPTIONS"
_debug printf "\$command_argv: %s\n" "${command_argv[*]}" _debug printf "\$_COMMAND_ARGV: %s\n" "${_COMMAND_ARGV[*]}"
_debug printf "\$command_parameters: %s\n" "${command_parameters[*]:-}" _debug printf "\$_COMMAND_PARAMETERS: %s\n" "${_COMMAND_PARAMETERS[*]:-}"
############################################################################### ###############################################################################
# Environment # Environment
@ -266,15 +266,15 @@ _debug printf "\$_ME: %s\n" "$_ME"
# Load Commands # Load Commands
############################################################################### ###############################################################################
# Initialize defined_commands array. # Initialize $_DEFINED_COMMANDS array.
defined_commands=() _DEFINED_COMMANDS=()
# _load_commands() # _load_commands()
# #
# Loads all of the commands sourced in the environment.
#
# Usage: # Usage:
# _load_commands # _load_commands
#
# Loads all of the commands sourced in the environment.
_load_commands() { _load_commands() {
_debug printf "_load_commands(): entering...\n" _debug printf "_load_commands(): entering...\n"
@ -295,7 +295,7 @@ _load_commands() {
_debug printf "_load_commands() \$function_name: %s\n" "$function_name" _debug printf "_load_commands() \$function_name: %s\n" "$function_name"
# Add the function name to the $defined_commands array unless it starts # Add the function name to the $_DEFINED_COMMANDS array unless it starts
# with an underscore or is one of the desc(), debug(), or die() functions, # with an underscore or is one of the desc(), debug(), or die() functions,
# since these are treated as having 'private' visibility. # since these are treated as having 'private' visibility.
if ! ( [[ "$function_name" =~ ^_(.*) ]] || \ if ! ( [[ "$function_name" =~ ^_(.*) ]] || \
@ -304,13 +304,13 @@ _load_commands() {
[[ "$function_name" == "die" ]] [[ "$function_name" == "die" ]]
) )
then then
defined_commands+=("$function_name") _DEFINED_COMMANDS+=("$function_name")
fi fi
done done
_debug printf \ _debug printf \
"commands() \$defined_commands:\n%s\n" \ "commands() \$_DEFINED_COMMANDS:\n%s\n" \
"${defined_commands[*]:-}" "${_DEFINED_COMMANDS[*]:-}"
} }
############################################################################### ###############################################################################
@ -327,24 +327,24 @@ _load_commands() {
# NOTE: must be called at end of program after all commands have been defined. # NOTE: must be called at end of program after all commands have been defined.
_main() { _main() {
_debug printf "main(): entering...\n" _debug printf "main(): entering...\n"
_debug printf "main() \$cmd (upon entering): %s\n" "$cmd" _debug printf "main() \$_CMD (upon entering): %s\n" "$_CMD"
# If $cmd is blank, then set to help # If $_CMD is blank, then set to `$DEFAULT_COMMAND`
if [[ -z $cmd ]] if [[ -z $_CMD ]]
then then
cmd="$DEFAULT_COMMAND" _CMD="$DEFAULT_COMMAND"
fi fi
# Load all of the commands. # Load all of the commands.
_load_commands _load_commands
# If the command is defined, run it, otherwise return an error. # If the command is defined, run it, otherwise return an error.
if ( _contains "$cmd" "${defined_commands[*]:-}" ) if _contains "$_CMD" "${_DEFINED_COMMANDS[*]:-}"
then then
# Pass all comment arguments to the program except for the first ($0). # Pass all comment arguments to the program except for the first ($0).
$cmd "${command_parameters[@]:-}" $_CMD "${_COMMAND_PARAMETERS[@]:-}"
else else
_die printf "Unknown command: %s\n" "$cmd" _die printf "Unknown command: %s\n" "$_CMD"
fi fi
} }
@ -354,18 +354,35 @@ _main() {
# _function_exists() # _function_exists()
# #
# Usage:
# _function_exists "possible_function_name"
#
# Takes a potential function name as an argument and returns whether a function # Takes a potential function name as an argument and returns whether a function
# exists with that name. # exists with that name.
_function_exists() { _function_exists() {
[ "$(type -t "$1")" == 'function' ] [ "$(type -t "$1")" == 'function' ]
} }
# _contains() # _command_exists()
# #
# Takes an item and a list and determines whether the list contains the item. # Usage:
# _command_exists "possible_command_name"
#
# Takes a potential command name as an argument and returns whether a command
# exists with that name.
#
# For information on why `hash` is used here, see:
# http://stackoverflow.com/a/677212
_command_exists() {
hash "$1" 2>/dev/null
}
# _contains()
# #
# Usage: # Usage:
# _contains "$item" "${list[*]}" # _contains "$item" "${list[*]}"
#
# Takes an item and a list and determines whether the list contains the item.
_contains() { _contains() {
local test_list=(${*:2}) local test_list=(${*:2})
for _test_element in "${test_list[@]:-}" for _test_element in "${test_list[@]:-}"
@ -382,33 +399,56 @@ _contains() {
# _join() # _join()
# #
# Takes a separator and a list of items, joining that list of items with the
# separator.
#
# Usage: # Usage:
# _join "," a b c # _join "," a b c
# _join "${an_array[@]}" # _join "${an_array[@]}"
#
# Takes a separator and a list of items, joining that list of items with the
# separator.
_join() { _join() {
local separator="$1" local separator
local target_array=(${@:2}) local target_array
local dirty local dirty
local clean
separator="$1"
target_array=(${@:2})
dirty="$(printf "${separator}%s" "${target_array[@]}")" dirty="$(printf "${separator}%s" "${target_array[@]}")"
local clean="${dirty:${#separator}}" clean="${dirty:${#separator}}"
printf "%s" "${clean}" printf "%s" "${clean}"
} }
# _command_argv_includes() # _command_argv_includes()
# #
# Usage:
# _command_argv_includes "an_argument"
#
# Takes a possible command argument and determines whether it is included in # Takes a possible command argument and determines whether it is included in
# the command argument list. # the command argument list.
# #
# This is a shortcut for simple cases where a command wants to check for the # This is a shortcut for simple cases where a command wants to check for the
# presence of options quickly without parsing the options again. # presence of options quickly without parsing the options again.
_command_argv_includes() {
_contains "$1" "${_COMMAND_ARGV[*]}"
}
# _blank()
# #
# Usage: # Usage:
# _command_argv_includes "an_argument" # _blank "$an_argument"
_command_argv_includes() { #
_contains "$1" "${command_argv[*]}" # Takes an argument and returns true if it is blank.
_blank() {
[[ -z "${1:-}" ]]
}
# _present()
#
# Usage:
# _present "$an_argument"
#
# Takes an argument and returns true if it is present.
_present() {
[[ -n "${1:-}" ]]
} }
# _verify_write_permissions # _verify_write_permissions
@ -515,7 +555,7 @@ Description:
Display help information for $_ME or a specified command. Display help information for $_ME or a specified command.
EOM EOM
help() { help() {
if [[ ${#command_argv[@]} = 1 ]] if [[ ${#_COMMAND_ARGV[@]} = 1 ]]
then then
cat <<EOM cat <<EOM
__ __ __ __
@ -572,10 +612,10 @@ EOM
commands() { commands() {
if _command_argv_includes "--raw" if _command_argv_includes "--raw"
then then
printf "%s\n" "${defined_commands[@]}" printf "%s\n" "${_DEFINED_COMMANDS[@]}"
else else
printf "Available commands:\n" printf "Available commands:\n"
printf " %s\n" "${defined_commands[@]}" printf " %s\n" "${_DEFINED_COMMANDS[@]}"
fi fi
} }