4 # Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
5 # This file is part of openmano
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
12 # http://www.apache.org/licenses/LICENSE-2.0
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
25 #Utility for getting options, must be call with source
26 #for every <option> it sets a variable 'option_<option>="-"'
27 #if the option appears more than once, it concatenates a "-"
28 #if the option contains an argument: 'option_<option>="argument"'
29 #if the long option name contains "-" they are converted to "_"
30 #params that are not options are stored in 'params'
31 #the options to look for is received in the first argument,
32 #a blank separator list with short and long options without the leading - or --
33 #options to be stored in the same variable must appear in the same word separated by ':'
34 #insert a trailing = if the option requires an argument
35 #insert a trailing ? if the option may have an argument NOT IMPLEMENTED
36 #option -- means get the rest of argument returned as 'option__=$*'
38 #example: to allow options -h --help -j -k(with argument) --my-long-option(with argument)
39 # and other parameters after -- provide
40 # "help:h j k= my-long-option="
41 #parsing "-h -karg pepe --my-long-option=otherar -- -s" will set variables
44 # option_my_long_option="otherarg"
49 #detect if is called with a source to use the 'exit'/'return' command for exiting
50 [[ ${BASH_SOURCE[0]} != $0 ]] && ___exit
="return" || ___exit
="exit"
58 for option_group
in $options
60 _name
=${option_group%%:*}
63 eval option_
${_name}='""'
70 if [[ -n $get_argument ]]
72 [[ ${argument:0:1} == "-" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1
73 eval ${get_argument}='"$argument"'
74 #echo option $get_argument with argument
81 if [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
84 while index
=$
((index
+1)) && [[ $index -lt ${#argument} ]]
86 option
=${argument:$index:1}
88 for option_group
in $options
91 for o
in $
(echo $option_group |
tr ":=" " ")
93 [[ -z "$_name" ]] && _name
=${o//-/_}
94 #echo option $option versus $o
95 if [[ "$option" == "${o}" ]]
97 eval option_
${_name}='${option_'${_name}'}-'
99 if [[ ${option_group:${#option_group}-1} != "=" ]]
103 if [[ ${#argument} -gt $((index+1)) ]]
105 eval option_${_name}='"${argument:$((index+1))}"'
108 get_argument=option_${_name}
109 #echo next should be argument $argument
116 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
118 elif [[ ${argument:0:2} == "--" ]] && [[ ${#argument} -ge 3 ]]
121 option_argument=${option#*=}
122 option_name=${option%%=*}
123 [[ "$option_name" == "$option" ]] && option_argument=""
125 for option_group in $options
128 for o in $(echo $option_group | tr ":=" " ")
130 [[ -z "$_name" ]] && _name=${o//-/_}
131 #echo option $option versus $o
132 if [[ "$option_name" == "${o}" ]]
135 if [[ ${option_group:${#option_group}-1} != "=" ]]
136 then #not an argument
137 [[ -n "${option_argument}" ]] && echo "option '--${option%%=*}' do not accept an argument " >&2 && $___exit 1
138 eval option_${_name}='"${option_'${_name}'}-"'
139 elif [[ -n "${option_argument}" ]]
141 eval option_
${_name}='"${option_argument}"'
143 get_argument
=option_
${_name}
144 #echo next should be argument $argument
150 [[ $bad_option == y
]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
151 elif [[ ${argument:0:2} == "--" ]]
157 if [[ "$o" == "--" ]]
164 [[ $bad_option == y
]] && echo "invalid argument '--'? Type -h for help" >&2 && $___exit 1
167 params
="$params ${argument}"
172 [[ -n "$get_argument" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1