From: Matthias Kruk Date: Thu, 24 Jun 2021 23:40:01 +0000 (+0900) Subject: include: Explicitly declare arguments at the top of all functions X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=c41ea0c5d9c3d728e9cd5c5f596532de3fa85298;p=toolbox include: Explicitly declare arguments at the top of all functions Several functions do not declare and assign all arguments at the top, violating the toolbox coding style. This commit changes all argument declarations so that they conform to the toolbox style. --- diff --git a/include/array.sh b/include/array.sh index 7800cf7..37554c9 100644 --- a/include/array.sh +++ b/include/array.sh @@ -21,14 +21,11 @@ __init() { } array_contains() { - local needle - local haystack + local needle="$1" + local haystack=("${@:2}") local cur - needle="$1" - haystack=("${@:2}") - for cur in "${haystack[@]}"; do if [[ "$needle" == "$cur" ]]; then return 0 @@ -39,13 +36,16 @@ array_contains() { } array_to_lines() { + local array=("$@") local item - for item in "$@"; do + for item in "${array[@]}"; do echo "$item" done } array_sort() { - array_to_lines "$@" | sort -V + local array=("$@") + + array_to_lines "${array[@]}" | sort -V } diff --git a/include/clip.sh b/include/clip.sh index 4c2bd1e..38322b1 100644 --- a/include/clip.sh +++ b/include/clip.sh @@ -21,9 +21,7 @@ __init() { } clip_get() { - local sel - - sel="$1" + local sel="$1" if ! xclip -selection "$sel" -o 2>/dev/null; then return 1 @@ -49,11 +47,8 @@ clip_get_any() { } clip_set() { - local sel - local data - - sel="$1" - data="$2" + local sel="$1" + local data="$2" if (( $# < 2 )); then data=$( 1 )); then + if (( ${#lines[@]} > 1 )); then local arg - for arg in "${@:2}"; do + for arg in "${lines[@]}"; do echo "$arg" done else @@ -153,22 +148,29 @@ log_highlight() { } log_debug() { + local lines=("$@") + local dbgtag - local line dbgtag="${BASH_SOURCE[1]}:${BASH_LINENO[1]} ${FUNCNAME[1]}:" - log_write "$__log_debug" "[DBG] $dbgtag" "$@" + log_write "$__log_debug" "[DBG] $dbgtag" "${lines[@]}" } log_info() { - log_write "$__log_info" "[INF]" "$@" + local lines=("$@") + + log_write "$__log_info" "[INF]" "${lines[@]}" } log_warn() { - log_write "$__log_warning" "[WRN]" "$@" + local lines=("$@") + + log_write "$__log_warning" "[WRN]" "${lines[@]}" } log_error() { - log_write "$__log_error" "[ERR]" "$@" + local lines=("$@") + + log_write "$__log_error" "[ERR]" "${lines[@]}" } diff --git a/include/opt.sh b/include/opt.sh index 3a32b5a..18c4c59 100644 --- a/include/opt.sh +++ b/include/opt.sh @@ -145,15 +145,17 @@ opt_print_help() { } opt_parse() { + local argv=("$@") + local optname local err local i - declare -argx __opt_argv=("$@") + declare -argx __opt_argv=("${argv[@]}") err=0 - for (( i = 1; i <= $#; i++ )); do + for (( i = 0; i < ${#argv[@]}; i++ )); do local param local long local flags @@ -161,7 +163,7 @@ opt_parse() { local action local regex - param="${!i}" + param="${argv[$i]}" long="${__opt_map[$param]}" if [[ -z "$long" ]]; then @@ -176,12 +178,12 @@ opt_parse() { if (( flags & __opt_flag_has_value )); then ((i++)) - if (( i > $# )); then + if (( i > ${#argv[@]} )); then log_error "Missing argument after $param" return 1 fi - value="${!i}" + value="${argv[$i]}" if [[ -n "$regex" ]] && ! [[ "$value" =~ $regex ]]; then log_error "Value \"$value\" doesn't match \"$regex\"" @@ -225,9 +227,7 @@ opt_parse() { } opt_get() { - local long - - long="$1" + local long="$1" if array_contains "$long" "${!__opt_value[@]}"; then echo "${__opt_value[$long]}" diff --git a/include/ssh.sh b/include/ssh.sh index 39e5ba2..2bc9b5b 100644 --- a/include/ssh.sh +++ b/include/ssh.sh @@ -37,16 +37,12 @@ _ssh_get_socket_dir() { } _ssh_tunnel_ctrl_socket_name() { - local host - local port - local lport + local host="$1" + local port="$2" + local lport="$3" local sockdir - host="$1" - port="$2" - lport="$3" - if ! sockdir=$(_ssh_get_socket_dir); then return 1 fi @@ -56,14 +52,11 @@ _ssh_tunnel_ctrl_socket_name() { } _ssh_proxy_ctrl_socket_name() { - local host - local port + local host="$1" + local port="$2" local sockdir - host="$1" - port="$2" - if ! sockdir=$(_ssh_get_socket_dir); then return 1 fi