Skip to content

Commit

Permalink
refactor: use ${array[@]:-}/${array[@]:+} simplify codes 🚞
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Sep 7, 2023
1 parent 6da59cb commit 51ef409
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
3 changes: 2 additions & 1 deletion bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ while [ $# -gt 0 ]; do
esac
done

[ ${#files[@]} -eq 0 ] && files=(.)
# if files is empty, use "."
files=("${files[@]:-.}")
readonly files

################################################################################
Expand Down
4 changes: 1 addition & 3 deletions bin/find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,7 @@ readonly dirs
# convert extensions to find -iname options
find_iname_options=()
for e in "${extensions[@]}"; do
(("${#find_iname_options[@]}" == 0)) &&
find_iname_options=(-iname "*.$e") ||
find_iname_options=("${find_iname_options[@]}" -o -iname "*.$e")
find_iname_options=(${find_iname_options[@]:+"${find_iname_options[@]}" -o} -iname "*.$e")
done
readonly find_iname_options

Expand Down
2 changes: 1 addition & 1 deletion bin/uq
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ while (($# > 0)); do
;;
--)
shift
argv=("${argv[@]}" "$@")
argv=(${argv[@]:+"${argv[@]}"} "$@")
break
;;
-)
Expand Down
12 changes: 7 additions & 5 deletions bin/xpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ progVersion() {
# parse options
################################################################################

declare -a args=()
declare -a files=()
selected=false
while [ $# -gt 0 ]; do
case "$1" in
Expand All @@ -82,23 +82,26 @@ while [ $# -gt 0 ]; do
;;
--)
shift
args=(${args[@]:+"${args[@]}"} "$@")
files=(${files[@]:+"${files[@]}"} "$@")
break
;;
-*)
usage 2 "${PROG}: unrecognized option '$1'"
;;
*)
args=(${args[@]:+"${args[@]}"} "$1")
files=(${files[@]:+"${files[@]}"} "$1")
shift
;;
esac
done

# if files is empty, use one element "."
files=("${files[@]:-.}")

# if program name is xpf, set option selected!
[ "xpf" == "${PROG}" ] && selected=true

readonly args selected
readonly files selected

################################################################################
# biz logic
Expand Down Expand Up @@ -143,7 +146,6 @@ openOneFile() {
printf 'open %14s: %s\n' "$selected_msg" "$file"
}

[ "${#args[@]}" == 0 ] && files=(.) || files=("${args[@]}")
has_error=false

for file in "${files[@]}"; do
Expand Down
3 changes: 2 additions & 1 deletion legacy-bin/swtrunk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ greenEcho() {
colorEcho 32 "$@"
}

[ $# -eq 0 ] && dirs=(.) || dirs=("$@")
# if dirs is empty, use "."
dirs=("${dirs[@]:-.}")

for d in "${dirs[@]}"; do
[ ! -d "${d}/.svn" ] && {
Expand Down

0 comments on commit 51ef409

Please sign in to comment.