Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Thank you ikegami, Joost and Sidhekin

The *option's argument* is required, not the option itself.

What is "mandatory" and "optional" is not the option itself (options are by their nature always optional) but rather the associated argument.

In other words, the additional processing is only invoked if the arguments are specified.

Right, you clarified what was confusing me.

I would prefer the is_deeply method of comparing complex structure.

I agree, now that you mentioned it: easier to read, easier to update when developing the script.

I also found an interesting discussion of its limitations and alternatives in Does Data::Dumper give you eye strain? Use Test::More::is_deeply() for debugging!

The revised script below reflects the insights.

Rudif

#!perl -w use strict; my $version = 0.02; use Getopt::Long; $|++; use Test::More tests => 32; ## 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. { my $case = "Case 1 - from module synopsis"; local @ARGV = (); 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, 'Case 1 from module synopsis' ); is_deeply( [ $data, $length, $verbose ], [ 'file.dat', 24, undef ] +, 'Case 1 from module synopsis' ); } ## For options that take values it must be specified whether the optio +n value is required or not, and what kind of value the option expects +. ## Three kinds of values are supported: integer numbers, floating poin +t 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 o +nly be done if that value does not look like a valid command line opt +ion itself. { my $case = "Case 2 - option has default value, none given"; local @ARGV = (); my $tag = 'deftag'; my $result = GetOptions( 'tag=s' => \$tag ); ok( $result, $case ); is_deeply( [$tag], ['deftag'], $case ); } { my $case = "Case 3 - option has default value, option with value g +iven"; local @ARGV = ('--tag=MYTAG'); my $tag = 'deftag'; my $result = GetOptions( 'tag=s' => \$tag ); ok( $result, $case ); is_deeply( [$tag], ['MYTAG'], $case ); } { my $case = "Case 4 - option has default value, option with value a +nd unknown option given"; local @ARGV = ('--bingo --tag=OTHERTAG'); my $tag = 'deftag'; my $result = GetOptions( 'tag=s' => \$tag ); ok( !$result, $case ); is_deeply( [$tag], ['deftag'], $case ); } ## In the option specification, the option name is followed by an equa +ls 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 floa +ting point values. { my $case = "Case 5a - options have no default values, option value +s required if option given, no options given"; local @ARGV = (); my $tag; my $ival; my $result = GetOptions( 'tag=s' => \$tag, 'ival=i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ undef, undef ], $case ); } { my $case = "Case 5b - options have no default values, option value +s required if option given, options with values given"; local @ARGV = ( '--tag=thisyear', '--ival=2007' ); my $tag; my $ival; my $result = GetOptions( 'tag=s' => \$tag, 'ival=i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ 'thisyear', 2007 ], $case ); } { my $case = "Case 5c - options have no default values, option value +s required if option given, options without values given"; local @ARGV = ( '--tag', '--ival' ); my $tag; my $ival; my $result = GetOptions( 'tag=s' => \$tag, 'ival=i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ '--ival', undef ], $case ); } { my $case = "Case 5x - options have default values, option values r +equired if option given, no options given"; local @ARGV = (); my $tag = 'deftag'; my $ival = -1; my $result = GetOptions( 'tag=s' => \$tag, 'ival=i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ 'deftag', -1 ], $case ); } { my $case = "Case 5y - options have default values, option values r +equired if option given, options with values given"; local @ARGV = ( '--tag=thisyear', '--ival=2007' ); my $tag = 'deftag'; my $ival = -1; my $result = GetOptions( 'tag=s' => \$tag, 'ival=i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ 'thisyear', 2007 ], $case ); } { my $case = "Case 5z - options have default values, option values r +equired if option given, options without values given"; local @ARGV = ( '--tag', '--ival' ); my $tag = 'deftag'; my $ival = -1; my $result = GetOptions( 'tag=s' => \$tag, 'ival=i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ '--ival', -1 ], $case ); } ## Using a colon : instead of the equals sign indicates that the optio +n value is optional. ## In this case, if no suitable value is supplied, string valued optio +ns get an empty string '' assigned, while numeric options are set to +0. { my $case = "Case 6a - options have no default values, option value +s not required if options given, no options given"; local @ARGV = (); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ undef, undef ], $case ); } { my $case = "Case 6b - options have no default values, option value + not required if option given, options with values given"; local @ARGV = ( '--tag=thisyear', '--ival=2007' ); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ 'thisyear', 2007 ], $case ); } { my $case = "Case 6c - options have no default values, option value + not required if option given, options without values given"; local @ARGV = ( '--tag', '--ival' ); my $tag; my $ival; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ '', 0 ], $case ); } { my $case = "Case 6x - options have default values, option values n +ot required if options given, no options given"; local @ARGV = (); my $tag = 'deftag'; my $ival = -1; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ 'deftag', -1 ], $case ); } { my $case = "Case 6y - options have default values, option value no +t required if option given, options with values given"; local @ARGV = ( '--tag=thisyear', '--ival=2007' ); my $tag = 'deftag'; my $ival = -1; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ 'thisyear', 2007 ], $case ); } { my $case = "Case 6z - options have default values, option value no +t required if option given, options without values given"; local @ARGV = ( '--tag', '--ival' ); my $tag = 'deftag'; my $ival = -1; my $result = GetOptions( 'tag:s' => \$tag, 'ival:i' => \$ival ); ok( $result, $case ); is_deeply( [ $tag, $ival ], [ '', 0 ], $case ); } __END__

In reply to Re: Trying to understand subtleties of Getopt::Long, please help by Rudif
in thread Trying to understand subtleties of Getopt::Long, please help by Rudif

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found