summaryrefslogtreecommitdiff
path: root/scripts/library/parse_options.sh
blob: 39038de6dbba0263acb37dddc2002342dcb53c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# getopt like parser
parse_options() {
        local short_options=$1; shift;
        local long_options=$1; shift;
        local ret=0;
        local unused_options=""
        local i

        while [[ -n $1 ]]; do
                if [[ ${1:0:2} = '--' ]]; then
                        if [[ -n ${1:2} ]]; then
                                local match=""
                                for i in ${long_options//,/ }; do
                                        if [[ ${1:2} = ${i//:} ]]; then
                                                match=$i
                                                break
                                        fi
                                done
                                if [[ -n $match ]]; then
                                        local needsargument=0

                                        [[ ${match} = ${1:2}: ]] && needsargument=1
                                        [[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1

                                        if (( ! needsargument )); then
                                                OPTRET+=("$1")
                                        else
                                                if [[ -n $2 ]]; then
                                                        OPTRET+=("$1" "$2")
                                                        shift
                                                        while [[ -n $2 && ${2:0:1} != "-" ]]; do
                                                                shift
                                                                OPTRET+=("$1")
                                                        done
                                                else
                                                        printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2
                                                        ret=1
                                                fi
                                        fi
                                else
                                        echo "@SCRIPTNAME@: $(gettext "unrecognized option") '$1'" >&2
                                        ret=1
                                fi
                        else
                                shift
                                break
                        fi
                elif [[ ${1:0:1} = '-' ]]; then
                        for ((i=1; i<${#1}; i++)); do
                                if [[ $short_options =~ ${1:i:1} ]]; then
                                        local needsargument=0

                                        [[ $short_options =~ ${1:i:1}: && ! $short_options =~ ${1:i:1}:: ]] && needsargument=1
                                        [[ $short_options =~ ${1:i:1}:: && \
                                                ( -n ${1:$i+1} || ( -n $2 && ${2:0:1} != "-" ) ) ]] && needsargument=1

                                        if (( ! needsargument )); then
                                                OPTRET+=("-${1:i:1}")
                                        else
                                                if [[ -n ${1:$i+1} ]]; then
                                                        OPTRET+=("-${1:i:1}" "${1:i+1}")
                                                        while [[ -n $2 && ${2:0:1} != "-" ]]; do
                                                                shift
                                                                OPTRET+=("$1")
                                                        done
                                                else
                                                        if [[ -n $2 ]]; then
                                                                OPTRET+=("-${1:i:1}" "$2")
                                                                shift
                                                                while [[ -n $2 && ${2:0:1} != "-" ]]; do
                                                                        shift
                                                                        OPTRET+=("$1")
                                                                done

                                                        else
                                                                printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2
                                                                ret=1
                                                        fi
                                                fi
                                                break
                                        fi
                                else
                                        echo "@SCRIPTNAME@: $(gettext "unrecognized option") '-${1:i:1}'" >&2
                                        ret=1
                                fi
                        done
                else
                        unused_options+=("$1")
                fi
                shift
        done

        OPTRET+=('--' "${unused_options[@]}")
        return $ret
}