Fix `--force` behavior in `remove`.

The `--force` option is passed to the function in `$_COMMAND_ARGV`,
which means that assigning arguments from positions in this array leads
to unexpected behavior depending on where in the argument list `--force`
is included. As a result, the `remove` function must parse the
arguments, removing the `--force` argument before assigning function
arguments to local variables.

resolves #2
This commit is contained in:
William Melody 2015-11-23 10:38:45 -08:00
parent a869da8e4e
commit 09d6d83093
1 changed files with 24 additions and 6 deletions

30
hosts
View File

@ -945,6 +945,8 @@ EOM
remove() {
_verify_write_permissions
local is_search_pair=0
local force_skip_prompt=0
local arguments=()
local search_ip=""
local search_hostname=""
local search_string=""
@ -952,18 +954,34 @@ remove() {
_debug printf "remove() \$1: %s\n" "${1:-}"
_debug printf "remove() \$2: %s\n" "${2:-}"
if [[ -z "${1:-}" ]]
for arg in "${_COMMAND_ARGV[@]:-}"
do
case $arg in
--force)
force_skip_prompt=1
;;
*)
arguments+=($arg)
;;
esac
done
_debug printf "remove() \${arguments[0]}: %s\n" "${arguments[0]:-}"
_debug printf "remove() \${arguments[1]}: %s\n" "${arguments[1]:-}"
_debug printf "remove() \${arguments[2]}: %s\n" "${arguments[2]:-}"
if [[ -z "${arguments[1]:-}" ]]
then
$_ME help remove
exit 1
elif [[ -n "${2:-}" ]] && [[ ! "${2}" =~ ^-\* ]]
elif [[ -n "${arguments[2]:-}" ]]
then
search_ip="${1}"
search_hostname="${2}"
search_ip="${arguments[1]}"
search_hostname="${arguments[2]}"
is_search_pair=1
_debug printf "remove() \$is_search_pair: %s\n" "$is_search_pair"
else
search_string=${1:-}
search_string="${arguments[1]:-}"
_debug printf "remove() \$search_string: %s\n" "$search_string"
fi
@ -1004,7 +1022,7 @@ remove() {
printf "No matching records found.\n"
exit 1
fi
if ! _command_argv_includes "--force"
if ! ((force_skip_prompt))
then
printf "Removing the following records:\n%s\n" "$target_records"
while true