Jakobo / snaptest
- Source
- Commits
- Network (0)
- Issues (1)
- Downloads (4)
- Wiki (18)
- Graphs
-
Tree:
21f361e
commit 21f361e04e32a7b35f6ed0001437aff78afe00a3
tree b01ab38e8e0a635c7b8c1015153c312e8653983f
parent 7db4664b4f39dddc6c864f23692f4e0672ac8ac1
tree b01ab38e8e0a635c7b8c1015153c312e8653983f
parent 7db4664b4f39dddc6c864f23692f4e0672ac8ac1
snaptest / getoptx.sh
| 21f361e0 » | Jakobo | 2008-01-16 | 1 | #! /bin/bash | |
| 2 | |||||
| 3 | # getopt.sh: | ||||
| 4 | # functions like getopts but do long-named options parsing | ||||
| 5 | # and support optional arguments | ||||
| 6 | # | ||||
| 7 | # Version 1.0 1997 by Grigoriy Strokin (grg@philol.msu.ru), Public Domain | ||||
| 8 | # Date created: December 21, 1997 | ||||
| 9 | # Date modified: December 21, 1997 | ||||
| 10 | # | ||||
| 11 | # IMPORTANT FEATURES | ||||
| 12 | # | ||||
| 13 | # 1) Parses both short and long-named options | ||||
| 14 | # 2) Supports optional arguments | ||||
| 15 | # 3) Only uses bash builtins, thus no calls to external | ||||
| 16 | # utilities such as expr or sed is done. Therefore, | ||||
| 17 | # parsing speed is high enough | ||||
| 18 | # | ||||
| 19 | # | ||||
| 20 | # DESCRIPTION | ||||
| 21 | # | ||||
| 22 | # FUNCTION getopt | ||||
| 23 | # Usage: getopt OPTLIST {"$@"|ALTERNATIVE_PARAMETERS} | ||||
| 24 | # | ||||
| 25 | # like getopts, but parse options with both required and optional arguments, | ||||
| 26 | # Options with optional arguments must have "." instead of ":" after them. | ||||
| 27 | # Furthemore, a variable name to place option name cannot be specified | ||||
| 28 | # and is always placed in OPTOPT variable | ||||
| 29 | # | ||||
| 30 | # This function is provided for compatibility with getopts() | ||||
| 31 | # OPTLIST style, and it actually calls getoptex (see bellow) | ||||
| 32 | # | ||||
| 33 | # NOTE that a list of parameters is required and must be either "$@", | ||||
| 34 | # if processing command line arguments, or some alternative parameters. | ||||
| 35 | # | ||||
| 36 | # FUNCTION getoptex | ||||
| 37 | # Usage: getoptex OPTION_LIST {"$@"|ALTERNATIVE_PARAMETERS} | ||||
| 38 | # | ||||
| 39 | # like getopts, but parse long-named options. | ||||
| 40 | # | ||||
| 41 | # Both getopt and getoptex return 0 if an option has been parsed, | ||||
| 42 | # and 1 if all options are already parsed or an error occured | ||||
| 43 | # | ||||
| 44 | # Both getopt and getoptex set or test the following variables: | ||||
| 45 | # | ||||
| 46 | # OPTERR -- tested for whether error messages must be given for invalid options | ||||
| 47 | # | ||||
| 48 | # OPTOPT -- set to the name of an option parsed, | ||||
| 49 | # or to "?" if no more options or error | ||||
| 50 | # OPTARG -- set to the option argument, if any; | ||||
| 51 | # unset if ther is no argument; | ||||
| 52 | # on error, set to the erroneous option name | ||||
| 53 | # | ||||
| 54 | # OPTIND -- Initialized to 1. | ||||
| 55 | # Then set to the number of the next parameter to be parsed | ||||
| 56 | # when getopt or getoptex will be called next time. | ||||
| 57 | # When all options are parsed, contains a number of | ||||
| 58 | # the first non-option argument. | ||||
| 59 | # | ||||
| 60 | # | ||||
| 61 | # OPTOFS -- If a parameter number $OPTIND containg an option parsed | ||||
| 62 | # does not contain any more options, OPTOFS is unset; | ||||
| 63 | # otherwise, OPTOFS is set to such a number of "?" signs | ||||
| 64 | # which is equal to the number of options parsed | ||||
| 65 | # | ||||
| 66 | # You might not set variables OPTIND and OPTOFS yourself | ||||
| 67 | # unless you want to parse a list of parameters more than once. | ||||
| 68 | # Otherwise, you whould unset OPTIND (or set it to 1) | ||||
| 69 | # and unset OPTOFS each time you want to parse a new parameters list | ||||
| 70 | # | ||||
| 71 | # Option list format is DIFFERENT from one for getopts or getopt. getopts-style | ||||
| 72 | # option list can be converted to getoptex-style using a function optlistex | ||||
| 73 | # (see bellow) | ||||
| 74 | # | ||||
| 75 | # DESCRIPTION of option list used with getoptex: | ||||
| 76 | # Option names are separated by whitespace. Options consiting of | ||||
| 77 | # more than one character are treated as long-named (--option) | ||||
| 78 | # | ||||
| 79 | # Special characters can appear at the and of option names specifying | ||||
| 80 | # whether an argument is required (default is ";"): | ||||
| 81 | # ";" (default) -- no argument | ||||
| 82 | # ":" -- required argument | ||||
| 83 | # "," -- optional argument | ||||
| 84 | # | ||||
| 85 | # For example, an option list "a b c help version f: file: separator." | ||||
| 86 | # defines the following options: | ||||
| 87 | # -a, -b, -c, --help, --version -- no argument | ||||
| 88 | # -f, --file -- argument required | ||||
| 89 | # --separator -- optional argument | ||||
| 90 | # | ||||
| 91 | # FUNCTION optlistex | ||||
| 92 | # Usage new_style_optlist=`optlistex OLD_STYLE_OPTLIST` | ||||
| 93 | # | ||||
| 94 | # Converts getopts-style option list in a format suitable for use with getoptex | ||||
| 95 | # Namely, it inserts spaces after each option name. | ||||
| 96 | # | ||||
| 97 | # | ||||
| 98 | # HOW TO USE | ||||
| 99 | # | ||||
| 100 | # In order o use in your bash scripts the functions described, | ||||
| 101 | # include a command ". getopt.sh" to the file containing the script, | ||||
| 102 | # which will define functions getopt, getoptex, and optlistex | ||||
| 103 | # | ||||
| 104 | # EXAMPLES | ||||
| 105 | # | ||||
| 106 | # See files 'getopt1' and 'getopt2' that contain sample scripts that use | ||||
| 107 | # getopt and getoptex functions respectively | ||||
| 108 | # | ||||
| 109 | # | ||||
| 110 | # Please send your comments to grg@philol.msu.ru | ||||
| 111 | |||||
| 112 | function getoptex() | ||||
| 113 | { | ||||
| 114 | let $# || return 1 | ||||
| 115 | local optlist="${1#;}" | ||||
| 116 | let OPTIND || OPTIND=1 | ||||
| 117 | [ $OPTIND -lt $# ] || return 1 | ||||
| 118 | shift $OPTIND | ||||
| 119 | if [ "$1" != "-" -a "$1" != "${1#-}" ] | ||||
| 120 | then OPTIND=$[OPTIND+1]; if [ "$1" != "--" ] | ||||
| 121 | then | ||||
| 122 | local o | ||||
| 123 | o="-${1#-$OPTOFS}" | ||||
| 124 | for opt in ${optlist#;} | ||||
| 125 | do | ||||
| 126 | OPTOPT="${opt%[;.:]}" | ||||
| 127 | unset OPTARG | ||||
| 128 | local opttype="${opt##*[^;:.]}" | ||||
| 129 | [ -z "$opttype" ] && opttype=";" | ||||
| 130 | if [ ${#OPTOPT} -gt 1 ] | ||||
| 131 | then # long-named option | ||||
| 132 | case $o in | ||||
| 133 | "--$OPTOPT") | ||||
| 134 | if [ "$opttype" != ":" ]; then return 0; fi | ||||
| 135 | OPTARG="$2" | ||||
| 136 | if [ -z "$OPTARG" ]; | ||||
| 137 | then # error: must have an agrument | ||||
| 138 | let OPTERR && echo "$0: error: $OPTOPT must have an argument" >&2 | ||||
| 139 | OPTARG="$OPTOPT"; | ||||
| 140 | OPTOPT="?" | ||||
| 141 | return 1; | ||||
| 142 | fi | ||||
| 143 | OPTIND=$[OPTIND+1] # skip option's argument | ||||
| 144 | return 0 | ||||
| 145 | ;; | ||||
| 146 | "--$OPTOPT="*) | ||||
| 147 | if [ "$opttype" = ";" ]; | ||||
| 148 | then # error: must not have arguments | ||||
| 149 | let OPTERR && echo "$0: error: $OPTOPT must not have arguments" >&2 | ||||
| 150 | OPTARG="$OPTOPT" | ||||
| 151 | OPTOPT="?" | ||||
| 152 | return 1 | ||||
| 153 | fi | ||||
| 154 | OPTARG=${o#"--$OPTOPT="} | ||||
| 155 | return 0 | ||||
| 156 | ;; | ||||
| 157 | esac | ||||
| 158 | else # short-named option | ||||
| 159 | case "$o" in | ||||
| 160 | "-$OPTOPT") | ||||
| 161 | unset OPTOFS | ||||
| 162 | [ "$opttype" != ":" ] && return 0 | ||||
| 163 | OPTARG="$2" | ||||
| 164 | if [ -z "$OPTARG" ] | ||||
| 165 | then | ||||
| 166 | echo "$0: error: -$OPTOPT must have an argument" >&2 | ||||
| 167 | OPTARG="$OPTOPT" | ||||
| 168 | OPTOPT="?" | ||||
| 169 | return 1 | ||||
| 170 | fi | ||||
| 171 | OPTIND=$[OPTIND+1] # skip option's argument | ||||
| 172 | return 0 | ||||
| 173 | ;; | ||||
| 174 | "-$OPTOPT"*) | ||||
| 175 | if [ $opttype = ";" ] | ||||
| 176 | then # an option with no argument is in a chain of options | ||||
| 177 | OPTOFS="$OPTOFS?" # move to the next option in the chain | ||||
| 178 | OPTIND=$[OPTIND-1] # the chain still has other options | ||||
| 179 | return 0 | ||||
| 180 | else | ||||
| 181 | unset OPTOFS | ||||
| 182 | OPTARG="${o#-$OPTOPT}" | ||||
| 183 | return 0 | ||||
| 184 | fi | ||||
| 185 | ;; | ||||
| 186 | esac | ||||
| 187 | fi | ||||
| 188 | done | ||||
| 189 | echo "$0: error: invalid option: $o" | ||||
| 190 | fi; fi | ||||
| 191 | OPTOPT="?" | ||||
| 192 | unset OPTARG | ||||
| 193 | return 1 | ||||
| 194 | } | ||||
| 195 | function optlistex | ||||
| 196 | { | ||||
| 197 | local l="$1" | ||||
| 198 | local m # mask | ||||
| 199 | local r # to store result | ||||
| 200 | while [ ${#m} -lt $[${#l}-1] ]; do m="$m?"; done # create a "???..." mask | ||||
| 201 | while [ -n "$l" ] | ||||
| 202 | do | ||||
| 203 | r="${r:+"$r "}${l%$m}" # append the first character of $l to $r | ||||
| 204 | l="${l#?}" # cut the first charecter from $l | ||||
| 205 | m="${m#?}" # cut one "?" sign from m | ||||
| 206 | if [ -n "${l%%[^:.;]*}" ] | ||||
| 207 | then # a special character (";", ".", or ":") was found | ||||
| 208 | r="$r${l%$m}" # append it to $r | ||||
| 209 | l="${l#?}" # cut the special character from l | ||||
| 210 | m="${m#?}" # cut one more "?" sign | ||||
| 211 | fi | ||||
| 212 | done | ||||
| 213 | echo $r | ||||
| 214 | } | ||||
| 215 | function getopt() | ||||
| 216 | { | ||||
| 217 | local optlist=`optlistex "$1"` | ||||
| 218 | shift | ||||
| 219 | getoptex "$optlist" "$@" | ||||
| 220 | return $? | ||||
| 221 | } | ||||
