1 | #Last: Keeping doing the processArg for handing different type of arguments
|
---|
2 |
|
---|
3 | #parse2(\@_,$arguments,$self )
|
---|
4 |
|
---|
5 | package parse2;
|
---|
6 |
|
---|
7 | BEGIN {
|
---|
8 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
9 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
10 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
11 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
12 | }
|
---|
13 |
|
---|
14 | use util;
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | #--Local Util Functions----------------------------
|
---|
19 | #-----------------------------------------
|
---|
20 | # Name: transformArg
|
---|
21 | # Parameters: 1.(Array pointer of plugin pre-defined argument list)
|
---|
22 | # Pre-condition: Call this function and pass a array pointer of argument list.
|
---|
23 | # Post-condition: This function will transform the array to a hash table
|
---|
24 | # with "Argument name" as its key
|
---|
25 | # Return value: Return a hash table of plugin pre-defined argument
|
---|
26 | # list with "argument name" as the key
|
---|
27 | #-----------------------------------------
|
---|
28 | sub transformArg
|
---|
29 | {
|
---|
30 | my ($aryptSysArguList) = @_;
|
---|
31 | my %hashArg;
|
---|
32 |
|
---|
33 | foreach my $hashOneArg (@{$aryptSysArguList})
|
---|
34 | {
|
---|
35 | if(!(defined $hashArg{$hashOneArg->{"name"}}))
|
---|
36 | {
|
---|
37 | $hashArg{$hashOneArg->{"name"}} = $hashOneArg;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return %hashArg;
|
---|
41 | }
|
---|
42 |
|
---|
43 | sub checkRange
|
---|
44 | {
|
---|
45 | my ($strRange,$intInputArg,$strArgName) = @_;
|
---|
46 | my @aryRange = split(",",$strRange);
|
---|
47 | if(defined $aryRange[0])
|
---|
48 | {
|
---|
49 | if($intInputArg < $aryRange[0])
|
---|
50 | {
|
---|
51 | print STDERR " Parameter Parsing Error (Incorrect Range): when parse argument parameter for \"-$strArgName\"\n";
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 | else
|
---|
55 | {
|
---|
56 | if(scalar(@aryRange) == 2)
|
---|
57 | {
|
---|
58 | if($intInputArg > $aryRange[1])
|
---|
59 | {
|
---|
60 | print STDERR " Parameter Parsing Error (Incorrect Range): when parse argument parameter for \"-$strArgName\"\n";
|
---|
61 | return 0;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 | else{ die " System error: minimum range is not defined. Possible mistyping in Argument list for $strArgName\n";}
|
---|
67 | return 1;
|
---|
68 | }
|
---|
69 |
|
---|
70 | sub checkCharLength
|
---|
71 | {
|
---|
72 | my ($intCharLength,$intInputArg,$strArgName) = @_;
|
---|
73 | if($intCharLength =~ m/\d/)
|
---|
74 | {
|
---|
75 | if(length($intInputArg) != $intCharLength)
|
---|
76 | {
|
---|
77 | print STDERR " Parameter Parsing Error (Incorrect Char_Length): when parse argument parameter for \"-$strArgName\"\n";
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | die " System error: incorrect char_length. Possible mistyping in Argument list for $strArgName\n";
|
---|
84 | }
|
---|
85 | return 1;
|
---|
86 | }
|
---|
87 | #-----------------------------------------
|
---|
88 | # Name: processArg
|
---|
89 | # Parameters: 1.(Hash pointer of one argument)
|
---|
90 | # 2.(Array pointer of the user given argument)
|
---|
91 | # 3.(Hash pointer of user given arguments' values)
|
---|
92 | # Pre-condition: Given a argument ($hashOneArg)
|
---|
93 | # Post-condition: System will check whether it need to get parameter
|
---|
94 | # from $aryptInputArguList or not, and also check the
|
---|
95 | # given parameter is following the argument description
|
---|
96 | # Return value: 1 is parsing successful, 0 is failed.
|
---|
97 | #-----------------------------------------
|
---|
98 | sub processArg
|
---|
99 | {
|
---|
100 | my ($hashOneArg,$aryptInputArguList,$hashInputArg) = @_;
|
---|
101 |
|
---|
102 | # Since these two variables are going to be
|
---|
103 | # used a lot, store them with some better names.
|
---|
104 | my $strArgName = $hashOneArg->{"name"};
|
---|
105 | my $strArgType = $hashOneArg->{"type"};
|
---|
106 |
|
---|
107 | # If the argument type is "flag" then
|
---|
108 | # set it to 1(which is "true")
|
---|
109 | if($strArgType eq "flag")
|
---|
110 | {
|
---|
111 | $hashInputArg->{$strArgName} = 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | # If the argument type is "int" then
|
---|
115 | # gets the next argument from $aryptInputArguList
|
---|
116 | # and check whether it is a digit
|
---|
117 | # TODO: check its "range" and "char_length"
|
---|
118 | elsif($strArgType eq "int")
|
---|
119 | {
|
---|
120 | my $intInputArg = shift(@{$aryptInputArguList});
|
---|
121 | if ($intInputArg =~ /\d+/)
|
---|
122 | {
|
---|
123 | $hashInputArg->{$strArgName} = $intInputArg;
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | print STDERR " Error: occur in parse2.pm::processArg()\n Unmatched Argument: -$strArgName with type $strArgType\n";
|
---|
128 | return 0;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | # If the argument type is "enum" then
|
---|
133 | elsif($strArgType eq "enum")
|
---|
134 | {
|
---|
135 | if(defined $hashOneArg->{"list"})
|
---|
136 | {
|
---|
137 | my $aryptList = $hashOneArg->{"list"};
|
---|
138 | my $blnCheckInList = "false";
|
---|
139 | my $strInputArg = shift(@{$aryptInputArguList});
|
---|
140 | foreach my $hashEachItem (@$aryptList)
|
---|
141 | {
|
---|
142 | if($strInputArg eq $hashEachItem->{"name"})
|
---|
143 | {
|
---|
144 | $blnCheckInList = "true";
|
---|
145 | }
|
---|
146 | last if($blnCheckInList eq "true");
|
---|
147 | }
|
---|
148 | if($blnCheckInList ne "true")
|
---|
149 | {
|
---|
150 | print STDERR " Error: occur in parse2.pm::processArg()\n Unknown Enum List Type: -$strArgName with parameter: $strInputArg\n";
|
---|
151 | return 0;
|
---|
152 | } else {
|
---|
153 | $hashInputArg->{$strArgName} = $strInputArg;
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|
157 | else
|
---|
158 | {
|
---|
159 | print STDERR " Error: occur in parse2.pm::processArg(2)\n Unknown Type: -$strArgName with type $strArgType\n";
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | # If the argument type is "string" or "metadata" then
|
---|
165 | # just shift the next argument from $aryptInputArguList
|
---|
166 | # TODO: make sure if there is any checking required for this two types
|
---|
167 | elsif($strArgType eq "string" || $strArgType eq "metadata" || $strArgType eq "regexp" || $strArgType eq "url")
|
---|
168 | {
|
---|
169 | $hashInputArg->{$strArgName}= shift(@{$aryptInputArguList});
|
---|
170 | }
|
---|
171 |
|
---|
172 | # Report any undefined types
|
---|
173 | else
|
---|
174 | {
|
---|
175 | print STDERR " Error: occur in parse2.pm::processArg(3)\n Unknown Type: -$strArgName with type $strArgType\n";
|
---|
176 | return 0;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return 1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | #--Main Parsing Function----------------------------
|
---|
183 | #-----------------------------------------
|
---|
184 | # Name: parse
|
---|
185 | # Parameters: 1.(Array pointer of the user given argument)
|
---|
186 | # 2.(Array pointer of plugin pre-defined argument list)
|
---|
187 | # 3.(Hash pointer, where we store all the argument value)
|
---|
188 | # Pre-condition: Plugin gives the parameters to parse function in parse2
|
---|
189 | # Post-condition: Store all the default or user given values to the hash->{$ArgumentName}.
|
---|
190 | # Since hash may be a plugin $self, plugin will have every values we set.
|
---|
191 | # 4. Optional "allow_extra_options" argument. If this is set, then
|
---|
192 | # its ok to have arguments that are not in the predefined list
|
---|
193 | # Return value: -1 if parsing is unsuccessful
|
---|
194 | # other value for success. This will be 0 unless "allow_extra_options" is set, in which case it will be the number of extra arguments found.
|
---|
195 | #-----------------------------------------
|
---|
196 | sub parse
|
---|
197 | {
|
---|
198 | # Get the user supplied arguments pointer "\@_"
|
---|
199 | my $aryptUserArguList = shift;
|
---|
200 |
|
---|
201 | # Check if allow extra arguments
|
---|
202 | my $blnAllowExtraOptions = "false";
|
---|
203 |
|
---|
204 | if(scalar(@_) == 3)
|
---|
205 | {
|
---|
206 | my $strAllowExtraOptions = pop @_;
|
---|
207 |
|
---|
208 | if ($strAllowExtraOptions eq "allow_extra_options")
|
---|
209 | {
|
---|
210 | $blnAllowExtraOptions = "true";
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | my ($aryptSysArguList,$self) = @_;
|
---|
215 | my %hashArg;
|
---|
216 | my %hashInputArg;
|
---|
217 | my @ExtraOption;
|
---|
218 |
|
---|
219 | # Transform the system argument (predefined the code)
|
---|
220 | # from array to hash table for increasing performance
|
---|
221 | %hashArg = &transformArg($aryptSysArguList);
|
---|
222 |
|
---|
223 | # Process each User input argument and store the
|
---|
224 | # information into hashInputArg
|
---|
225 | while (my $strOneArg = shift(@{$aryptUserArguList}))
|
---|
226 | {
|
---|
227 | # Check whether it start with a "-" sign
|
---|
228 | if ($strOneArg =~ /^-+\w/)
|
---|
229 | {
|
---|
230 | # If it is start with a "-" sign then take it off
|
---|
231 | $strOneArg =~ s/^-+//;
|
---|
232 |
|
---|
233 | # If the inputed argument is defined in the argument
|
---|
234 | # list from this plugin then process
|
---|
235 |
|
---|
236 | if(defined $hashArg{$strOneArg})
|
---|
237 | {
|
---|
238 | #$%^
|
---|
239 | #print "($strOneArg) is processed\n";
|
---|
240 | # Process this argument and store the related
|
---|
241 | # information in %hashInputArg
|
---|
242 | if(processArg($hashArg{$strOneArg},$aryptUserArguList,\%hashInputArg) == 0){
|
---|
243 | print STDERR "<BadArgumentValue a=$strOneArg>\n";
|
---|
244 | return -1;}
|
---|
245 | }
|
---|
246 |
|
---|
247 | # Else check if it allows extra options, if yes
|
---|
248 | # then push it to a new array, else return fault
|
---|
249 | else
|
---|
250 | {
|
---|
251 | if($blnAllowExtraOptions eq "true")
|
---|
252 | {
|
---|
253 | push(@ExtraOption,"-$strOneArg");
|
---|
254 | }
|
---|
255 | else
|
---|
256 | {
|
---|
257 | print STDERR "<BadArgument a=$strOneArg>\n";
|
---|
258 | print STDERR " Error: occur in parse2.pm::parse()\n Extra Arguments: $strOneArg\n";
|
---|
259 | return -1;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | # This part follow the previous parsing system.
|
---|
265 | # It doesn't return error message even user
|
---|
266 | # gave a invalid argument.
|
---|
267 | else
|
---|
268 | {
|
---|
269 | if($blnAllowExtraOptions eq "true")
|
---|
270 | {
|
---|
271 | push(@ExtraOption,$strOneArg);
|
---|
272 | }
|
---|
273 | else
|
---|
274 | {
|
---|
275 | print STDERR " Error: occur in parse2.pm::parse()\n Invalid Argument: $strOneArg\n";
|
---|
276 | return -1;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | # Store the extra option back
|
---|
282 | # to the user given argument list.
|
---|
283 | @$aryptUserArguList = @ExtraOption;
|
---|
284 |
|
---|
285 | # Now we go through all the pre defined arguments,
|
---|
286 | # if the user has specified the arguments then just
|
---|
287 | # set to whatever they set. Otherwise use the default value
|
---|
288 | foreach my $hashOneArg (@{$aryptSysArguList})
|
---|
289 | {
|
---|
290 | my $strArgName = $hashOneArg->{"name"};
|
---|
291 |
|
---|
292 | # If the strArgName has defined in the %hashInputArg,
|
---|
293 | # this means users has give this argument, store the
|
---|
294 | # user given to self->{"$strArgName"}
|
---|
295 | if(defined $hashInputArg{$strArgName})
|
---|
296 | {
|
---|
297 | if(defined $hashOneArg->{"range"})
|
---|
298 | {
|
---|
299 | if(checkRange($hashOneArg->{"range"},$hashInputArg{$strArgName},$strArgName) == 0){ return -1;}
|
---|
300 | }
|
---|
301 | if(defined $hashOneArg->{"char_length"})
|
---|
302 | {
|
---|
303 | if(checkCharLength($hashOneArg->{"char_length"},$hashInputArg{$strArgName},$strArgName) == 0){ return -1;}
|
---|
304 | }
|
---|
305 | $self->{"$strArgName"} = $hashInputArg{"$strArgName"};
|
---|
306 | }
|
---|
307 | elsif (!defined $self->{$strArgName})
|
---|
308 | {
|
---|
309 | # don't want to override default with superclass value
|
---|
310 |
|
---|
311 | # Else use the default value of the arguments,
|
---|
312 | # if there is no default value, then it must be a flag,
|
---|
313 | # then set it to 0 (which is false)
|
---|
314 |
|
---|
315 | if(defined $hashOneArg->{"deft"})
|
---|
316 | {
|
---|
317 | $self->{"$strArgName"} = $hashOneArg->{"deft"};
|
---|
318 | }
|
---|
319 | else
|
---|
320 | {
|
---|
321 | if($hashOneArg->{"type"} eq "flag"){
|
---|
322 | $self->{"$strArgName"} = 0;
|
---|
323 | }
|
---|
324 | else {
|
---|
325 | # all other cases, use "" as default
|
---|
326 | $self->{"$strArgName"} = "";
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | # If allow_extra_options is set, then return the number of arguments left in the argument list.
|
---|
333 | if($blnAllowExtraOptions eq "true")
|
---|
334 | {
|
---|
335 | return scalar(@$aryptUserArguList);
|
---|
336 | }
|
---|
337 | else
|
---|
338 | {
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | 1;
|
---|