[16125] | 1 | #Last: Keeping doing the processArg for handing different type of arguments
|
---|
| 2 |
|
---|
| 3 | #parse3(\@_,$arguments,$self )
|
---|
| 4 |
|
---|
| 5 | package parse3;
|
---|
| 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 parse3.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 parse3.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 parse3.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 | #if the argument type is "quotestr", then the next several arguments must be shifted from $aryptInputArugList
|
---|
| 172 | #lets see if we can detect an end double quote
|
---|
| 173 | elsif($strArgType eq "quotestr")
|
---|
| 174 | {
|
---|
| 175 | my $tmpstr = shift(@{$aryptInputArguList});
|
---|
| 176 | $hashInputArg->{$strArgName} = "";
|
---|
| 177 | while($tmpstr !~ /\"$/) {
|
---|
| 178 | $hashInputArg->{$strArgName} = $hashInputArg->{$strArgName}." ".$tmpstr;
|
---|
| 179 | $tmpstr = shift(@{$aryptInputArguList});
|
---|
| 180 | }
|
---|
| 181 | $hashInputArg->{$strArgName} = $hashInputArg->{$strArgName}." ".$tmpstr;
|
---|
| 182 | }
|
---|
| 183 | else
|
---|
| 184 | {
|
---|
| 185 | print STDERR " Error: occur in parse3.pm::processArg(3)\n Unknown Type: -$strArgName with type $strArgType\n";
|
---|
| 186 | return 0;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | return 1;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | #--Main Parsing Function----------------------------
|
---|
| 193 | #-----------------------------------------
|
---|
| 194 | # Name: parse
|
---|
| 195 | # Parameters: 1.(Array pointer of the user given argument)
|
---|
| 196 | # 2.(Array pointer of plugin pre-defined argument list)
|
---|
| 197 | # 3.(Hash pointer, where we store all the argument value)
|
---|
| 198 | # Pre-condition: Plugin gives the parameters to parse function in parse3
|
---|
| 199 | # Post-condition: Store all the default or user given values to the hash->{$ArgumentName}.
|
---|
| 200 | # Since hash may be a plugin $self, plugin will have every values we set.
|
---|
| 201 | # 4. Optional "allow_extra_options" argument. If this is set, then
|
---|
| 202 | # its ok to have arguments that are not in the predefined list
|
---|
| 203 | # Return value: -1 if parsing is unsuccessful
|
---|
| 204 | # 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.
|
---|
| 205 | #-----------------------------------------
|
---|
| 206 | sub parse
|
---|
| 207 | {
|
---|
| 208 | # Get the user supplied arguments pointer "\@_"
|
---|
| 209 | my $aryptUserArguList = shift;
|
---|
| 210 |
|
---|
| 211 | # Check if allow extra arguments
|
---|
| 212 | my $blnAllowExtraOptions = "false";
|
---|
| 213 |
|
---|
| 214 | if(scalar(@_) == 3)
|
---|
| 215 | {
|
---|
| 216 | my $strAllowExtraOptions = pop @_;
|
---|
| 217 |
|
---|
| 218 | if ($strAllowExtraOptions eq "allow_extra_options")
|
---|
| 219 | {
|
---|
| 220 | $blnAllowExtraOptions = "true";
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | my ($aryptSysArguList,$self) = @_;
|
---|
| 225 | my %hashArg;
|
---|
| 226 | my %hashInputArg;
|
---|
| 227 | my @ExtraOption;
|
---|
| 228 |
|
---|
| 229 | # Transform the system argument (predefined the code)
|
---|
| 230 | # from array to hash table for increasing performance
|
---|
| 231 | %hashArg = &transformArg($aryptSysArguList);
|
---|
| 232 |
|
---|
| 233 | # Process each User input argument and store the
|
---|
| 234 | # information into hashInputArg
|
---|
| 235 | while (my $strOneArg = shift(@{$aryptUserArguList}))
|
---|
| 236 | {
|
---|
| 237 | # Check whether it start with a "-" sign
|
---|
| 238 | if ($strOneArg =~ /^-+\w/)
|
---|
| 239 | {
|
---|
| 240 | # If it is start with a "-" sign then take it off
|
---|
| 241 | $strOneArg =~ s/^-+//;
|
---|
| 242 |
|
---|
| 243 | # If the inputed argument is defined in the argument
|
---|
| 244 | # list from this plugin then process
|
---|
| 245 |
|
---|
| 246 | if(defined $hashArg{$strOneArg})
|
---|
| 247 | {
|
---|
| 248 | #$%^
|
---|
| 249 | #print "($strOneArg) is processed\n";
|
---|
| 250 | # Process this argument and store the related
|
---|
| 251 | # information in %hashInputArg
|
---|
| 252 | if(processArg($hashArg{$strOneArg},$aryptUserArguList,\%hashInputArg) == 0){
|
---|
| 253 | print STDERR "<BadArgumentValue a=$strOneArg>\n";
|
---|
| 254 | return -1;}
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | # Else check if it allows extra options, if yes
|
---|
| 258 | # then push it to a new array, else return fault
|
---|
| 259 | else
|
---|
| 260 | {
|
---|
| 261 | if($blnAllowExtraOptions eq "true")
|
---|
| 262 | {
|
---|
| 263 | push(@ExtraOption,"-$strOneArg");
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | {
|
---|
| 267 | print STDERR "<BadArgument a=$strOneArg>\n";
|
---|
| 268 | print STDERR " Error: occur in parse3.pm::parse()\n Extra Arguments: $strOneArg\n";
|
---|
| 269 | return -1;
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | # This part follow the previous parsing system.
|
---|
| 275 | # It doesn't return error message even user
|
---|
| 276 | # gave a invalid argument.
|
---|
| 277 | else
|
---|
| 278 | {
|
---|
| 279 | if($blnAllowExtraOptions eq "true")
|
---|
| 280 | {
|
---|
| 281 | push(@ExtraOption,$strOneArg);
|
---|
| 282 | }
|
---|
| 283 | else
|
---|
| 284 | {
|
---|
| 285 | print STDERR " Error: occur in parse3.pm::parse()\n Invalid Argument: $strOneArg\n";
|
---|
| 286 | return -1;
|
---|
| 287 | }
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | # Store the extra option back
|
---|
| 292 | # to the user given argument list.
|
---|
| 293 | @$aryptUserArguList = @ExtraOption;
|
---|
| 294 |
|
---|
| 295 | # Now we go through all the pre defined arguments,
|
---|
| 296 | # if the user has specified the arguments then just
|
---|
| 297 | # set to whatever they set. Otherwise use the default value
|
---|
| 298 | foreach my $hashOneArg (@{$aryptSysArguList})
|
---|
| 299 | {
|
---|
| 300 | my $strArgName = $hashOneArg->{"name"};
|
---|
| 301 |
|
---|
| 302 | # If the strArgName has defined in the %hashInputArg,
|
---|
| 303 | # this means users has give this argument, store the
|
---|
| 304 | # user given to self->{"$strArgName"}
|
---|
| 305 | if(defined $hashInputArg{$strArgName})
|
---|
| 306 | {
|
---|
| 307 | if(defined $hashOneArg->{"range"})
|
---|
| 308 | {
|
---|
| 309 | if(checkRange($hashOneArg->{"range"},$hashInputArg{$strArgName},$strArgName) == 0){ return -1;}
|
---|
| 310 | }
|
---|
| 311 | if(defined $hashOneArg->{"char_length"})
|
---|
| 312 | {
|
---|
| 313 | if(checkCharLength($hashOneArg->{"char_length"},$hashInputArg{$strArgName},$strArgName) == 0){ return -1;}
|
---|
| 314 | }
|
---|
| 315 | $self->{"$strArgName"} = $hashInputArg{"$strArgName"};
|
---|
| 316 | }
|
---|
| 317 | elsif (!defined $self->{$strArgName})
|
---|
| 318 | {
|
---|
| 319 | # don't want to override default with superclass value
|
---|
| 320 |
|
---|
| 321 | # Else use the default value of the arguments,
|
---|
| 322 | # if there is no default value, then it must be a flag,
|
---|
| 323 | # then set it to 0 (which is false)
|
---|
| 324 |
|
---|
| 325 | if(defined $hashOneArg->{"deft"})
|
---|
| 326 | {
|
---|
| 327 | $self->{"$strArgName"} = $hashOneArg->{"deft"};
|
---|
| 328 | }
|
---|
| 329 | else
|
---|
| 330 | {
|
---|
| 331 | if($hashOneArg->{"type"} eq "flag"){
|
---|
| 332 | $self->{"$strArgName"} = 0;
|
---|
| 333 | }
|
---|
| 334 | else {
|
---|
| 335 | # all other cases, use "" as default
|
---|
| 336 | $self->{"$strArgName"} = "";
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | # If allow_extra_options is set, then return the number of arguments left in the argument list.
|
---|
| 343 | if($blnAllowExtraOptions eq "true")
|
---|
| 344 | {
|
---|
| 345 | return scalar(@$aryptUserArguList);
|
---|
| 346 | }
|
---|
| 347 | else
|
---|
| 348 | {
|
---|
| 349 | return 0;
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | 1;
|
---|