source: gs2-extensions/parallel-building/trunk/src/perllib/parse3.pm@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

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