blob: 82ec06ca00e3f48023268a0bd9e1c07184cafded [file] [log] [blame]
tiernoc4864732016-09-20 11:07:00 +00001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5# This file is part of openmano
6# All Rights Reserved.
7#
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
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
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
18# under the License.
19#
20# For those usages not covered by the Apache License, Version 2.0 please
21# contact with: nfvlabs@tid.es
22##
23
24
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__=$*'
37
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
42# option_help="-"
43# option_k="arg"
44# option_my_long_option="otherarg"
45# params=" pepe"
46# option__="-s"
47
48
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"
51
52options="$1"
53shift
54
55get_argument=""
56#reset variables
57params=""
58for option_group in $options
59do
60 _name=${option_group%%:*}
61 _name=${_name%=}
62 _name=${_name//-/_}
63 eval option_${_name}='""'
64done
65
66while [[ $# -gt 0 ]]
67do
68 argument="$1"
69 shift
70 if [[ -n $get_argument ]]
71 then
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
75 get_argument=""
76 continue
77 fi
78
79
80 #short options
81 if [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
82 then
83 index=0
84 while index=$((index+1)) && [[ $index -lt ${#argument} ]]
85 do
86 option=${argument:$index:1}
87 bad_option=y
88 for option_group in $options
89 do
90 _name=""
91 for o in $(echo $option_group | tr ":=" " ")
92 do
93 [[ -z "$_name" ]] && _name=${o//-/_}
94 #echo option $option versus $o
95 if [[ "$option" == "${o}" ]]
96 then
97 eval option_${_name}='${option_'${_name}'}-'
98 bad_option=n
99 if [[ ${option_group:${#option_group}-1} != "=" ]]
100 then
101 continue
102 fi
103 if [[ ${#argument} -gt $((index+1)) ]]
104 then
105 eval option_${_name}='"${argument:$((index+1))}"'
106 index=${#argument}
107 else
108 get_argument=option_${_name}
109 #echo next should be argument $argument
110 fi
111
112 break
113 fi
114 done
115 done
116 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
117 done
118 elif [[ ${argument:0:2} == "--" ]] && [[ ${#argument} -ge 3 ]]
119 then
120 option=${argument:2}
121 option_argument=${option#*=}
122 option_name=${option%%=*}
123 [[ "$option_name" == "$option" ]] && option_argument=""
124 bad_option=y
125 for option_group in $options
126 do
127 _name=""
128 for o in $(echo $option_group | tr ":=" " ")
129 do
130 [[ -z "$_name" ]] && _name=${o//-/_}
131 #echo option $option versus $o
132 if [[ "$option_name" == "${o}" ]]
133 then
134 bad_option=n
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}" ]]
140 then
141 eval option_${_name}='"${option_argument}"'
142 else
143 get_argument=option_${_name}
144 #echo next should be argument $argument
145 fi
146 break
147 fi
148 done
149 done
150 [[ $bad_option == y ]] && echo "invalid argument '-$option'? Type -h for help" >&2 && $___exit 1
151 elif [[ ${argument:0:2} == "--" ]]
152 then
153 option__="$*"
154 bad_option=y
155 for o in $options
156 do
157 if [[ "$o" == "--" ]]
158 then
159 bad_option=n
160 option__=" $*"
161 break
162 fi
163 done
164 [[ $bad_option == y ]] && echo "invalid argument '--'? Type -h for help" >&2 && $___exit 1
165 break
166 else
167 params="$params ${argument}"
168 fi
169
170done
171
172[[ -n "$get_argument" ]] && echo "option '-$option' requires an argument" >&2 && $___exit 1
173$___exit 0
174#echo params $params
175