source: trunk/gsdl/perllib/parse2.pm@ 11650

Last change on this file since 11650 was 11650, checked in by kjdon, 18 years ago

added url type in line 167 for jeffrey's new downloading code

  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1#Last: Keeping doing the processArg for handing different type of arguments
2
3#parse2(\@_,$arguments,$self )
4
5package parse2;
6
7BEGIN {
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
14use 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#-----------------------------------------
28sub 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
43sub 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
70sub 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#-----------------------------------------
98sub 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.(Self 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 $self->{$ArgumentName}.
190# Since $self is a pointer from plugin, plugin will have every values we set.
191# Return value: 1 is parsing successful, 0 is failed.
192#-----------------------------------------
193sub parse
194{
195 # Get the user supplied arguments pointer "\@_"
196 my $aryptUserArguList = shift;
197
198 # Check if allow extra arguments
199 my $blnAllowExtraOptions = "false";
200
201 if(scalar(@_) == 3)
202 {
203 my $strAllowExtraOptions = pop @_;
204
205 if ($strAllowExtraOptions eq "allow_extra_options")
206 {
207 $blnAllowExtraOptions = "true";
208 }
209 }
210
211 my ($aryptSysArguList,$self) = @_;
212 my %hashArg;
213 my %hashInputArg;
214 my @ExtraOption;
215
216 # Transform the system argument (predefined the code)
217 # from array to hash table for increasing performance
218 %hashArg = &transformArg($aryptSysArguList);
219
220 # Process each User input argument and store the
221 # information into hashInputArg
222 while (my $strOneArg = shift(@{$aryptUserArguList}))
223 {
224 # Check whether it start with a "-" sign
225 if ($strOneArg =~ /^-+\w/)
226 {
227 # If it is start with a "-" sign then take it off
228 $strOneArg =~ s/^-+//;
229
230 # If the inputed argument is defined in the argument
231 # list from this plugin then process
232
233 if(defined $hashArg{$strOneArg})
234 {
235 #$%^
236 #print "($strOneArg) is processed\n";
237 # Process this argument and store the related
238 # information in %hashInputArg
239 if(processArg($hashArg{$strOneArg},$aryptUserArguList,\%hashInputArg) == 0){
240 print STDERR "<BadArgumentValue a=$strOneArg>\n";
241 return 0;}
242 }
243
244 # Else check if it allows extra options, if yes
245 # then push it to a new array, else return fault
246 else
247 {
248 if($blnAllowExtraOptions eq "true")
249 {
250 push(@ExtraOption,"-$strOneArg");
251 }
252 else
253 {
254 print STDERR "<BadArgument a=$strOneArg>\n";
255 print STDERR " Error: occur in parse2.pm::parse()\n Extra Arguments: $strOneArg\n";
256 return 0;
257 }
258 }
259 }
260
261 # This part follow the previous parsing system.
262 # It doesn't return error message even user
263 # gave a invalid argument.
264 else
265 {
266 if($blnAllowExtraOptions eq "true")
267 {
268 push(@ExtraOption,$strOneArg);
269 }
270 else
271 {
272 print STDERR " Error: occur in parse2.pm::parse()\n Invalid Argument: $strOneArg\n";
273 return 0;
274 }
275 }
276 }
277
278 # Store the extra option back
279 # to the user given argument list.
280 @$aryptUserArguList = @ExtraOption;
281
282 # Now we go through all the pre defined arguments,
283 # if the user has specified the arguments then just
284 # set to whatever they set. Otherwise use the default value
285 foreach my $hashOneArg (@{$aryptSysArguList})
286 {
287 my $strArgName = $hashOneArg->{"name"};
288
289 # If the strArgName has defined in the %hashInputArg,
290 # this means users has give this argument, store the
291 # user given to self->{"$strArgName"}
292 if(defined $hashInputArg{$strArgName})
293 {
294 if(defined $hashOneArg->{"range"})
295 {
296 if(checkRange($hashOneArg->{"range"},$hashInputArg{$strArgName},$strArgName) == 0){ return 0;}
297 }
298 if(defined $hashOneArg->{"char_length"})
299 {
300 if(checkCharLength($hashOneArg->{"char_length"},$hashInputArg{$strArgName},$strArgName) == 0){ return 0;}
301 }
302 $self->{"$strArgName"} = $hashInputArg{"$strArgName"};
303 }
304 elsif (!defined $self->{$strArgName})
305 {
306 # don't want to override default with superclass value
307
308 # Else use the default value of the arguments,
309 # if there is no default value, then it must be a flag,
310 # then set it to 0 (which is false)
311
312 if(defined $hashOneArg->{"deft"})
313 {
314 $self->{"$strArgName"} = $hashOneArg->{"deft"};
315 }
316 else
317 {
318 if($hashOneArg->{"type"} eq "flag"){
319 $self->{"$strArgName"} = 0;
320 }
321 else {
322 # all other cases, use "" as default
323 $self->{"$strArgName"} = "";
324 }
325 }
326 }
327 }
328
329 # If allow_extra_options is set, then return the number of arguments left in the argument list.
330 if($blnAllowExtraOptions eq "true")
331 {
332 return scalar(@$aryptUserArguList);
333 }
334 else
335 {
336 return 1;
337 }
338}
339
3401;
Note: See TracBrowser for help on using the repository browser.