| 1 |
#!/usr/local/bin/perl5 -w |
| 2 |
|
| 3 |
# Example program for the Getopt::Tabular package. See Getopt/Tabular.pod |
| 4 |
# for detailed explanation of How Things Work. |
| 5 |
# |
| 6 |
# originally by Greg Ward 1995/07/06 - 1995/07/09 (for ParseArgs package) |
| 7 |
# adapted to Getopt::Tabular 1996/11/10 |
| 8 |
|
| 9 |
use Getopt::Tabular qw/GetOptions SetError/; |
| 10 |
|
| 11 |
# Data needed for parsing command line options |
| 12 |
|
| 13 |
@Ints = (0, 0); # you don't really need to supply pre-defined |
| 14 |
$Float = 0; # values -- I just do it here to avoid |
| 15 |
$String = ""; # "Identifier used only once" warnings |
| 16 |
@UStrings = (); |
| 17 |
$Flag = 0; |
| 18 |
@Foo = (); |
| 19 |
|
| 20 |
$help = <<"EOH"; |
| 21 |
This is a pretty useless program. All it does is demonstrate my |
| 22 |
Getopt::Tabular package. |
| 23 |
EOH |
| 24 |
|
| 25 |
$usage = <<"EOH"; |
| 26 |
Usage: $0 [options] |
| 27 |
EOH |
| 28 |
|
| 29 |
# Here's the important bit: the option table. The *first* element of each |
| 30 |
# entry is the option name, which can be whatever you like; the *second* |
| 31 |
# element is the option type, which must be one of string, integer, float, |
| 32 |
# constant, boolean, copy, arrayconst, hashconst, call, or eval. |
| 33 |
|
| 34 |
&Getopt::Tabular::AddPatternType |
| 35 |
("upperstring", "[A-Z]+", |
| 36 |
["string of uppercase letters", "strings of uppercase letters"]); |
| 37 |
|
| 38 |
@opt_table = (["-int", "integer", 2, \@Ints, "two integers", |
| 39 |
"i1 i2"], |
| 40 |
["-float", "float", 1, \$Float, "a floating-point number" ], |
| 41 |
["-string", "string", 1, \$String, "a string" ], |
| 42 |
["-ustring","upperstring",3,\@UStrings, |
| 43 |
"an uppercase string (example of a user-defined pattern type)"], |
| 44 |
["-flag", "boolean", 0, \$Flag, "a boolean flag" ], |
| 45 |
["-foo", "call", 0, \&get_foo, "do nothing important"], |
| 46 |
["-show", "eval", 0, 'print "Ints = @Ints\n";', |
| 47 |
"print the current values of -int option"] |
| 48 |
); |
| 49 |
|
| 50 |
# Here's an example subroutine used by the "-foo" option -- note that |
| 51 |
# it modifies the list referenced by its second argument, which is perfectly |
| 52 |
# legal; this modification propagates back up to change @ARGV after |
| 53 |
# &GetOptions is finished. |
| 54 |
|
| 55 |
sub get_foo |
| 56 |
{ |
| 57 |
my ($arg, $args) = @_; |
| 58 |
my $next; |
| 59 |
|
| 60 |
print "Hello, you have used the $arg option\n"; |
| 61 |
unless (@$args) |
| 62 |
{ |
| 63 |
&SetError ("bad_foo", "no arguments found for $arg option"); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
while ($next = shift @$args) |
| 68 |
{ |
| 69 |
last if $next =~ /^-/; |
| 70 |
push (@Foo, $next); |
| 71 |
print "Got $next from \@\$args\n"; |
| 72 |
} |
| 73 |
|
| 74 |
if (defined $next) # not the last option? |
| 75 |
{ |
| 76 |
print "Putting $next back on \@\$args\n"; |
| 77 |
unshift (@$args, $next); |
| 78 |
} |
| 79 |
1; |
| 80 |
} |
| 81 |
|
| 82 |
# Here's where we actually do real work -- set the two help messages |
| 83 |
# (the summary of options is generated automatically) and then parse |
| 84 |
# those arguments. |
| 85 |
|
| 86 |
&Getopt::Tabular::SetHelp ($help, $usage); |
| 87 |
#&GetOptions (\@opt_table, \@ARGV) || exit 1; |
| 88 |
if (! &GetOptions (\@opt_table, \@ARGV, \@newARGV)) |
| 89 |
{ |
| 90 |
die "GetOptions returned error status; reason: $Getopt::Tabular::ErrorClass\n"; |
| 91 |
} |
| 92 |
|
| 93 |
print <<"END"; |
| 94 |
Values after parsing: |
| 95 |
\$Ints = @Ints |
| 96 |
\$Float = $Float |
| 97 |
\$String = $String |
| 98 |
\@UStrings = @UStrings |
| 99 |
\$Flag = $Flag |
| 100 |
\@Foo = @Foo |
| 101 |
END |
| 102 |
print " Original arguments: @ARGV\n"; |
| 103 |
print "Remaining arguments: @newARGV\n"; |