Skip to content

Instantly share code, notes, and snippets.

@hidekuro
Created January 29, 2016 22:16
Show Gist options
  • Save hidekuro/83f64c6548845797c64b to your computer and use it in GitHub Desktop.
Save hidekuro/83f64c6548845797c64b to your computer and use it in GitHub Desktop.
bash引数解析サンプル
declare -i argc=0
declare -a argv=()
while (( $# > 0 )); do
case "$1" in
- | -- )
shift
argc+=$#
argv+=("$@")
break
;;
--* )
opt_name="${1#--}"
opt_name="${opt_name%%=*}"
delim_by_space=true
if [[ "$1" =~ = ]]; then
delim_by_space=false
opt_value="${1#*=}"
fi
case "$opt_name" in
'help' )
usage_exit
;;
'tag' )
OPT_TAGS+=("${opt_value:-$2}")
($delim_by_space) && shift
;;
'enable-a' )
OPT_A=1
;;
esac
unset opt_value
;;
-* )
for (( i=1; i < ${#1}; i++ )); do
opt_name="${1:$i:1}";
case "$opt_name" in
'h' )
usage_exit
;;
'a' )
OPT_A=1
;;
't' )
OPT_TAGS+=("$2")
shift
;;
esac
done
;;
* )
(( ++argc ))
argv+=("$1")
;;
esac
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment