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