#!perl -w use strict; use Getopt::Long; use Data::Dumper; $Data::Dumper::Indent = 0; $|++; use Test::More tests => 12; # using scalars for options # The option name as specified to the GetOptions() function is called the option specification. # Later we'll see that this specification can contain more than just the option name. # The reference to the variable is called the option destination. # Case 1 from module synopsis { my $data = "file.dat"; my $length = 24; my $verbose; my $result = GetOptions( "length=i" => \$length, # numeric "file=s" => \$data, # string "verbose" => \$verbose # flag ); ok($result); is( Data::Dumper->Dump( [ $data, $length, $verbose ], [qw / data length verbose /] ), "\$data = 'file.dat';\$length = 24;\$verbose = undef;" ); } # For options that take values it must be specified whether the option value is required or not, and what kind of value the option expects. # Three kinds of values are supported: integer numbers, floating point numbers, and strings. # If the option value is required, Getopt::Long will take the command line argument that follows the option and assign this to the option variable. # If, however, the option value is specified as optional, this will only be done if that value does not look like a valid command line option itself. # Case 2 { @ARGV = (); my $tag = 'deftag'; # option variable with default value my $result = GetOptions( 'tag=s' => \$tag ); ok($result); is( Data::Dumper->Dump( [$tag], [qw / tag /] ), "\$tag = 'deftag';" ); } # Case 3 { @ARGV = ('--tag=MYTAG'); my $tag = 'deftag'; # option variable with default value my $result = GetOptions( 'tag=s' => \$tag ); ok($result); is( Data::Dumper->Dump( [$tag], [qw / tag /] ), "\$tag = 'MYTAG';" ); } # Case 4 { @ARGV = ('--bingo --tag=OTHERTAG'); my $tag = 'deftag'; # option variable with default value my $result = GetOptions( 'tag=s' => \$tag ); ok( !$result ); is( Data::Dumper->Dump( [$tag], [qw / tag /] ), "\$tag = 'deftag';" ); } # In the option specification, the option name is followed by an equals sign = and the letter s. # The equals sign indicates that this option requires a value. # The letter s indicates that this value is an arbitrary string. # Other possible value types are i for integer values, and f for floating point values. # Using a colon : instead of the equals sign indicates that the option value is optional. # In this case, if no suitable value is supplied, string valued options get an empty string '' assigned, while numeric options are set to 0. # Case 5 { @ARGV = (); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok($result); is( Data::Dumper->Dump( [ $tag, $ival ], [qw / tag ival /] ), "\$tag = undef;\$ival = undef;" ); # why not a failure, ok(! $result); } # Case 6 { @ARGV = (); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok($result); is( Data::Dumper->Dump( [ $tag, $ival ], [qw / tag ival /] ), "\$tag = undef;\$ival = undef;" ); # why not "\$tag = '';\$ival = 0;" } __END__