J
jrwats
Challenge:
3 options that take an arbitrary parameter afterwards => (n, flavor,
path)
2 options that serve as flags (take no params) => (noupload, debug)
1 option that takes in everything (no matter what follows) to pass to
the helper script as its argument => (options)
The spec requires handling of DOS and UNIX style command line option
prefixes (/ and - respectively). Note that double dashes for prefixes
(--) are not handled. I'm created a hash with the option as key and
the optional param(s) as its value. If the option had no value I just
store the option as the value as well for boolean's sake.
The following is what I did. I feel that someone, somewhere, somehow
can do better:
#!/usr/bin/perl
#perl test.pl -n 3 -flavor retail -debug -path /path/to/file first.dll
second.dll third.dll -options /matches:highlight
use strict;
my $strUsage = <<HEREDOC;
This is a heredoc string!
One day we'll have a real Usage string...
Usage:
-n {number} : Number of times to run
-flavor {flavor} : Flavor or build
-path {path/to/exe} : Exe path
-noupload: Do not upload results
-debug: Run in debugger
-options {options to pass}: Options to pass to cmd
HEREDOC
my @rgArgOptions = ('n', 'flavor', 'path'); #options taking one single
argument
my @rgFlagOptions = ('noupload', 'debug'); #options which take no
argument (flags)
my $strDLL = "";
my %hOpts;
for(my $nArg = 0; $nArg <= $#ARGV; ++$nArg ){
#Look for option prefix
if ($ARGV[$nArg] =~ m/^[\/-].*/ ) {
my $bFound = 0;
# if option matches an option requiring other input add that #
to the options hash and increment $nArg
map {
if (!$bFound && @ARGV[$nArg] =~ m/[\/-]$_/) { $bFound = 1;
$hOpts{$_} = $ARGV[++$nArg]; }
} @rgArgOptions;
# if option matches an option flag (not requiring any input) #
add that to the options hash and do NOT increment $nArg
map {
if (!$bFound && @ARGV[$nArg] =~ m/[\/-]$_/) { $bFound = 1;
$hOpts{$_} = $ARGV[$nArg]; }
} @rgFlagOptions;
if (!$bFound && @ARGV[$nArg] =~ m/[\/-]options/){
$hOpts{'options'} = join (' ', @ARGV[++$nArg .. $#ARGV]);
last; #analagous to 'break;' in C
}
if(!$bFound){
print $strUsage;
exit -1;
}
}else{
# if not an option, must be a DLL
$strDLL .= $ARGV[$nArg] . " ";
}
}
print "strDLL: $strDLL\n";
map { print "$_: $hOpts{$_}\n"} keys %hOpts;
3 options that take an arbitrary parameter afterwards => (n, flavor,
path)
2 options that serve as flags (take no params) => (noupload, debug)
1 option that takes in everything (no matter what follows) to pass to
the helper script as its argument => (options)
The spec requires handling of DOS and UNIX style command line option
prefixes (/ and - respectively). Note that double dashes for prefixes
(--) are not handled. I'm created a hash with the option as key and
the optional param(s) as its value. If the option had no value I just
store the option as the value as well for boolean's sake.
The following is what I did. I feel that someone, somewhere, somehow
can do better:
#!/usr/bin/perl
#perl test.pl -n 3 -flavor retail -debug -path /path/to/file first.dll
second.dll third.dll -options /matches:highlight
use strict;
my $strUsage = <<HEREDOC;
This is a heredoc string!
One day we'll have a real Usage string...
Usage:
-n {number} : Number of times to run
-flavor {flavor} : Flavor or build
-path {path/to/exe} : Exe path
-noupload: Do not upload results
-debug: Run in debugger
-options {options to pass}: Options to pass to cmd
HEREDOC
my @rgArgOptions = ('n', 'flavor', 'path'); #options taking one single
argument
my @rgFlagOptions = ('noupload', 'debug'); #options which take no
argument (flags)
my $strDLL = "";
my %hOpts;
for(my $nArg = 0; $nArg <= $#ARGV; ++$nArg ){
#Look for option prefix
if ($ARGV[$nArg] =~ m/^[\/-].*/ ) {
my $bFound = 0;
# if option matches an option requiring other input add that #
to the options hash and increment $nArg
map {
if (!$bFound && @ARGV[$nArg] =~ m/[\/-]$_/) { $bFound = 1;
$hOpts{$_} = $ARGV[++$nArg]; }
} @rgArgOptions;
# if option matches an option flag (not requiring any input) #
add that to the options hash and do NOT increment $nArg
map {
if (!$bFound && @ARGV[$nArg] =~ m/[\/-]$_/) { $bFound = 1;
$hOpts{$_} = $ARGV[$nArg]; }
} @rgFlagOptions;
if (!$bFound && @ARGV[$nArg] =~ m/[\/-]options/){
$hOpts{'options'} = join (' ', @ARGV[++$nArg .. $#ARGV]);
last; #analagous to 'break;' in C
}
if(!$bFound){
print $strUsage;
exit -1;
}
}else{
# if not an option, must be a DLL
$strDLL .= $ARGV[$nArg] . " ";
}
}
print "strDLL: $strDLL\n";
map { print "$_: $hOpts{$_}\n"} keys %hOpts;