parse_options: adjust error messages
[pacman-ng.git] / scripts / library / parse_options.sh
blob8057c288adcebe2e775159eb953cb0aa1f81d2fb
1 # getopt like parser
2 parse_options() {
3 local short_options=$1; shift;
4 local long_options=$1; shift;
5 local ret=0;
6 local unused_options=""
7 local i
9 while [[ -n $1 ]]; do
10 if [[ ${1:0:2} = '--' ]]; then
11 if [[ -n ${1:2} ]]; then
12 local match=""
13 for i in ${long_options//,/ }; do
14 if [[ ${1:2} = ${i//:} ]]; then
15 match=$i
16 break
18 done
19 if [[ -n $match ]]; then
20 if [[ ${1:2} = $match ]]; then
21 printf ' %s' "$1"
22 else
23 if [[ -n $2 ]]; then
24 printf ' %s' "$1"
25 shift
26 printf " '%s'" "$1"
27 else
28 printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'$1'" >&2
29 ret=1
32 else
33 echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'" >&2
34 ret=1
36 else
37 shift
38 break
40 elif [[ ${1:0:1} = '-' ]]; then
41 for ((i=1; i<${#1}; i++)); do
42 if [[ $short_options =~ ${1:i:1} ]]; then
43 if [[ $short_options =~ ${1:i:1}: ]]; then
44 if [[ -n ${1:$i+1} ]]; then
45 printf ' -%s' "${1:i:1}"
46 printf " '%s'" "${1:$i+1}"
47 else
48 if [[ -n $2 ]]; then
49 printf ' -%s' "${1:i:1}"
50 shift
51 printf " '%s'" "${1}"
52 else
53 printf "@SCRIPTNAME@: $(gettext "option %s requires an argument")" "'-${1:i:1}'" >&2
54 ret=1
57 break
58 else
59 printf ' -%s' "${1:i:1}"
61 else
62 echo "@SCRIPTNAME@: $(gettext "unrecognized option") '-${1:i:1}'" >&2
63 ret=1
65 done
66 else
67 unused_options="${unused_options} '$1'"
69 shift
70 done
72 printf " --"
73 if [[ -n $unused_options ]]; then
74 for i in ${unused_options[@]}; do
75 printf ' %s' "$i"
76 done
78 if [[ -n $1 ]]; then
79 while [[ -n $1 ]]; do
80 printf " '%s'" "${1}"
81 shift
82 done
84 printf "\n"
86 return $ret