Added shfmt to autoformat shell scripts (#86)

This commit is contained in:
Anton Babenko 2020-01-21 11:54:13 +01:00 committed by GitHub
commit 1e5b3af0d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 269 additions and 209 deletions

View file

@ -8,8 +8,8 @@ repos:
- id: check-case-conflict - id: check-case-conflict
- id: check-merge-conflict - id: check-merge-conflict
- id: check-executables-have-shebangs - id: check-executables-have-shebangs
#- repo: git://github.com/jumanjihouse/pre-commit-hooks - repo: git://github.com/jumanjihouse/pre-commit-hooks
# rev: 1.11.2 rev: 1.11.2
# hooks: hooks:
# - id: shellcheck - id: shfmt
# - id: shfmt args: ['-l', '-i', '2', '-ci', '-sr', '-w']

View file

@ -12,12 +12,12 @@ main() {
for argv; do for argv; do
case $argv in case $argv in
(-a|--args) -a | --args)
shift shift
args="$1" args="$1"
shift shift
;; ;;
(--) --)
shift shift
files="$@" files="$@"
break break
@ -353,59 +353,73 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(-a|--alternative) -a | --alternative)
flags=a$flags ;; flags=a$flags
;;
(-h|--help) -h | --help)
_getopt_help _getopt_help
return 2 # as does GNU getopt return 2 # as does GNU getopt
;; ;;
(-l|--longoptions) -l | --longoptions)
long="$long${long:+,}$2" long="$long${long:+,}$2"
shift ;; shift
;;
(-n|--name) -n | --name)
name=$2 name=$2
shift ;; shift
;;
(-o|--options) -o | --options)
short=$2 short=$2
have_short=true have_short=true
shift ;; shift
;;
(-q|--quiet) -q | --quiet)
flags=q$flags ;; flags=q$flags
;;
(-Q|--quiet-output) -Q | --quiet-output)
flags=Q$flags ;; flags=Q$flags
;;
(-s|--shell) -s | --shell)
case $2 in case $2 in
(sh|bash) sh | bash)
flags=${flags//t/} ;; flags=${flags//t/}
(csh|tcsh) ;;
flags=t$flags ;; csh | tcsh)
(*) flags=t$flags
;;
*)
echo 'getopt: unknown shell after -s or --shell argument' >&2 echo 'getopt: unknown shell after -s or --shell argument' >&2
echo "Try \`getopt --help' for more information." >&2 echo "Try \`getopt --help' for more information." >&2
return 2 ;; return 2
;;
esac esac
shift ;;
(-u|--unquoted)
flags=u$flags ;;
(-T|--test)
return 4 ;;
(-V|--version)
echo "pure-getopt 1.4.3"
return 0 ;;
(--)
shift shift
break ;; ;;
-u | --unquoted)
flags=u$flags
;;
-T | --test)
return 4
;;
-V | --version)
echo "pure-getopt 1.4.3"
return 0
;;
--)
shift
break
;;
esac esac
shift shift
@ -476,11 +490,12 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(--) --)
params=("${params[@]}" "${@:2}") params=("${params[@]}" "${@:2}")
break ;; break
;;
(--*=*) --*=*)
o=${1%%=*} o=${1%%=*}
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
@ -499,7 +514,7 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(--?*) --?*)
o=$1 o=$1
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
@ -523,7 +538,7 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(-*) -*)
if [[ $flags == *a* ]]; then if [[ $flags == *a* ]]; then
# Alternative parsing mode! # Alternative parsing mode!
# Try to handle as a long option if any of the following apply: # Try to handle as a long option if any of the following apply:
@ -537,24 +552,28 @@ getopt() {
,$short, != *,"${o#-}"[:,]* ]]; then ,$short, != *,"${o#-}"[:,]* ]]; then
o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null) o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null)
case $? in case $? in
(0) 0)
# Unambiguous match. Let the long options parser handle # Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message. # it, with a flag to get the right error message.
set -- "-$1" "${@:2}" set -- "-$1" "${@:2}"
alt_recycled=true alt_recycled=true
continue ;; continue
(1) ;;
1)
# Ambiguous match, generate error and continue. # Ambiguous match, generate error and continue.
_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null
error=1 error=1
shift shift
continue ;; continue
(2) ;;
2)
# No match, fall through to single-character check. # No match, fall through to single-character check.
true ;; true
(*) ;;
*)
echo "getopt: assertion failed (3)" >&2 echo "getopt: assertion failed (3)" >&2
return 3 ;; return 3
;;
esac esac
fi fi
fi fi
@ -594,9 +613,10 @@ getopt() {
fi fi
fi fi
error=1 error=1
fi ;; fi
;;
(*) *)
# GNU getopt in-place mode (leading dash on short options) # GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT # overrides POSIXLY_CORRECT
if [[ $flags == *i* ]]; then if [[ $flags == *i* ]]; then
@ -607,6 +627,7 @@ getopt() {
else else
params=("${params[@]}" "$1") params=("${params[@]}" "$1")
fi fi
;;
esac esac
shift shift
@ -665,18 +686,24 @@ getopt() {
fi fi
done done
case ${#matches[@]} in case ${#matches[@]} in
(0) 0)
[[ $flags == *q* ]] || \ [[ $flags == *q* ]] ||
printf "$name: unrecognized option %s\\n" >&2 \ printf "$name: unrecognized option %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "$q")" >&2
return 2 ;;
(1) return 2
printf '%s' "${matches[0]}"; return 0 ;; ;;
(*) 1)
[[ $flags == *q* ]] || \ printf '%s' "${matches[0]}"
printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ return 0
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" ;;
return 1 ;; *)
[[ $flags == *q* ]] ||
printf "$name: option %s is ambiguous; possibilities: %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" >&2
return 1
;;
esac esac
} }
@ -704,12 +731,15 @@ getopt() {
for ((i = 0; i < ${#s}; i++)); do for ((i = 0; i < ${#s}; i++)); do
c=${s:i:1} c=${s:i:1}
case $c in case $c in
(\\|\'|!) \\ | \' | !)
echo -n "'\\$c'" ;; echo -n "'\\$c'"
($'\n') ;;
echo -n "\\$c" ;; $'\n')
(*) echo -n "\\$c"
echo -n "$c" ;; ;;
*)
echo -n "$c"
;;
esac esac
done done
echo -n \' echo -n \'

View file

@ -11,12 +11,12 @@ main() {
for argv; do for argv; do
case $argv in case $argv in
(-a|--args) -a | --args)
shift shift
args="$1" args="$1"
shift shift
;; ;;
(--) --)
shift shift
files="$@" files="$@"
break break
@ -120,59 +120,73 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(-a|--alternative) -a | --alternative)
flags=a$flags ;; flags=a$flags
;;
(-h|--help) -h | --help)
_getopt_help _getopt_help
return 2 # as does GNU getopt return 2 # as does GNU getopt
;; ;;
(-l|--longoptions) -l | --longoptions)
long="$long${long:+,}$2" long="$long${long:+,}$2"
shift ;; shift
;;
(-n|--name) -n | --name)
name=$2 name=$2
shift ;; shift
;;
(-o|--options) -o | --options)
short=$2 short=$2
have_short=true have_short=true
shift ;; shift
;;
(-q|--quiet) -q | --quiet)
flags=q$flags ;; flags=q$flags
;;
(-Q|--quiet-output) -Q | --quiet-output)
flags=Q$flags ;; flags=Q$flags
;;
(-s|--shell) -s | --shell)
case $2 in case $2 in
(sh|bash) sh | bash)
flags=${flags//t/} ;; flags=${flags//t/}
(csh|tcsh) ;;
flags=t$flags ;; csh | tcsh)
(*) flags=t$flags
;;
*)
echo 'getopt: unknown shell after -s or --shell argument' >&2 echo 'getopt: unknown shell after -s or --shell argument' >&2
echo "Try \`getopt --help' for more information." >&2 echo "Try \`getopt --help' for more information." >&2
return 2 ;; return 2
;;
esac esac
shift ;;
(-u|--unquoted)
flags=u$flags ;;
(-T|--test)
return 4 ;;
(-V|--version)
echo "pure-getopt 1.4.3"
return 0 ;;
(--)
shift shift
break ;; ;;
-u | --unquoted)
flags=u$flags
;;
-T | --test)
return 4
;;
-V | --version)
echo "pure-getopt 1.4.3"
return 0
;;
--)
shift
break
;;
esac esac
shift shift
@ -243,11 +257,12 @@ getopt() {
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
(--) --)
params=("${params[@]}" "${@:2}") params=("${params[@]}" "${@:2}")
break ;; break
;;
(--*=*) --*=*)
o=${1%%=*} o=${1%%=*}
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
@ -266,7 +281,7 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(--?*) --?*)
o=$1 o=$1
if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then if ! o=$(_getopt_resolve_abbrev "$o" "${longarr[@]}"); then
error=1 error=1
@ -290,7 +305,7 @@ getopt() {
alt_recycled=false alt_recycled=false
;; ;;
(-*) -*)
if [[ $flags == *a* ]]; then if [[ $flags == *a* ]]; then
# Alternative parsing mode! # Alternative parsing mode!
# Try to handle as a long option if any of the following apply: # Try to handle as a long option if any of the following apply:
@ -304,24 +319,28 @@ getopt() {
,$short, != *,"${o#-}"[:,]* ]]; then ,$short, != *,"${o#-}"[:,]* ]]; then
o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null) o=$(_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" 2> /dev/null)
case $? in case $? in
(0) 0)
# Unambiguous match. Let the long options parser handle # Unambiguous match. Let the long options parser handle
# it, with a flag to get the right error message. # it, with a flag to get the right error message.
set -- "-$1" "${@:2}" set -- "-$1" "${@:2}"
alt_recycled=true alt_recycled=true
continue ;; continue
(1) ;;
1)
# Ambiguous match, generate error and continue. # Ambiguous match, generate error and continue.
_getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null _getopt_resolve_abbrev "${1%%=*}" "${longarr[@]}" > /dev/null
error=1 error=1
shift shift
continue ;; continue
(2) ;;
2)
# No match, fall through to single-character check. # No match, fall through to single-character check.
true ;; true
(*) ;;
*)
echo "getopt: assertion failed (3)" >&2 echo "getopt: assertion failed (3)" >&2
return 3 ;; return 3
;;
esac esac
fi fi
fi fi
@ -361,9 +380,10 @@ getopt() {
fi fi
fi fi
error=1 error=1
fi ;; fi
;;
(*) *)
# GNU getopt in-place mode (leading dash on short options) # GNU getopt in-place mode (leading dash on short options)
# overrides POSIXLY_CORRECT # overrides POSIXLY_CORRECT
if [[ $flags == *i* ]]; then if [[ $flags == *i* ]]; then
@ -374,6 +394,7 @@ getopt() {
else else
params=("${params[@]}" "$1") params=("${params[@]}" "$1")
fi fi
;;
esac esac
shift shift
@ -432,18 +453,24 @@ getopt() {
fi fi
done done
case ${#matches[@]} in case ${#matches[@]} in
(0) 0)
[[ $flags == *q* ]] || \ [[ $flags == *q* ]] ||
printf "$name: unrecognized option %s\\n" >&2 \ printf "$name: unrecognized option %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "$q")" >&2
return 2 ;;
(1) return 2
printf '%s' "${matches[0]}"; return 0 ;; ;;
(*) 1)
[[ $flags == *q* ]] || \ printf '%s' "${matches[0]}"
printf "$name: option %s is ambiguous; possibilities: %s\\n" >&2 \ return 0
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" ;;
return 1 ;; *)
[[ $flags == *q* ]] ||
printf "$name: option %s is ambiguous; possibilities: %s\\n" \
"$(_getopt_quote "$q")" "$(_getopt_quote "${matches[@]}")" >&2
return 1
;;
esac esac
} }
@ -471,12 +498,15 @@ getopt() {
for ((i = 0; i < ${#s}; i++)); do for ((i = 0; i < ${#s}; i++)); do
c=${s:i:1} c=${s:i:1}
case $c in case $c in
(\\|\'|!) \\ | \' | !)
echo -n "'\\$c'" ;; echo -n "'\\$c'"
($'\n') ;;
echo -n "\\$c" ;; $'\n')
(*) echo -n "\\$c"
echo -n "$c" ;; ;;
*)
echo -n "$c"
;;
esac esac
done done
echo -n \' echo -n \'