parameter_check.pl -b -ddd produces non-intuitive result, where -ddd is eaten by -b and becomes the value of option -b.

This would be a mistake on the part of the user invoking the command. You've told the module that option -b takes a value, so the module is simply doing what you asked: taking the next value in @ARGV as the value of the option. Note that other command-line tools act the same way, e.g. grep -e -i causes -i to be taken as the value for the -e option. This does not look like a bug in the library to me.

parameter_check.pl -ddd -b the result is as expected: options('b') is set to zero length string.

This is actually an invalid command: option -b requires a value, and it's not getting one. You're not checking the return value of getopts, which would have told you something is wrong. A correct command line to specify the empty string for the option -b would have been parameter_check.pl -ddd -b ''.

the behavior of this module varies depending on if the parameter is the last in the line or not

Based on the above, this is not the correct conclusion to draw.

Not all command-line tools agree on a standard way of processing options, so if you want different behavior, you may have to use a different library or implement it yourself - though I wouldn't advise the latter, since you'd just be adding yet another differing way to process options to the mix.

During testing I discovered that this module correctly processes setting the value of an option via repetition of the option letter , like -ddd.

Not really, it's simply taking dd as the value of the -d option. Getopt::Long actually handles this as a separate case - otherwise, its behavior is identical to Getopt::Std for the following test cases, plus it gives a helpful error message.

use warnings; use strict; use Getopt::Std; # Note that removing posix_default and gnu_compat has no effect # on the output of this particular script. use Getopt::Long qw/ :config posix_default gnu_compat bundling /; use Data::Dump; my @tests = ( [qw/ -b -ddd /], [qw/ -b -c -ddd /], [qw/ -ddd -b /], [qw/ -ddd -c -b /], [qw/ -ddd -b -c /], [qw/ -ddd -c -b -- /], [qw/ -c -b -ddd /], [qw/ -b -c -ddd /], ); for my $t (@tests) { print "##### "; dd $t; { local @ARGV = @$t; if ( getopts("b:cd:", \my %opts) ) { dd \%opts } else { warn "Bad options\n" } } { local @ARGV = @$t; if ( GetOptions(\my %opts, 'b=s', 'c', 'd+') ) { dd \%opts } else { warn "Bad options\n" } } } __END__ ##### ["-b", "-ddd"] { b => "-ddd" } { b => "-ddd" } ##### ["-b", "-c", "-ddd"] { b => "-c", d => "dd" } { b => "-c", d => 3 } ##### ["-ddd", "-b"] Bad options Option b requires an argument Bad options ##### ["-ddd", "-c", "-b"] Bad options Option b requires an argument Bad options ##### ["-ddd", "-b", "-c"] { b => "-c", d => "dd" } { b => "-c", d => 3 } ##### ["-ddd", "-c", "-b", "--"] { b => "--", c => 1, d => "dd" } { b => "--", c => 1, d => 3 } ##### ["-c", "-b", "-ddd"] { b => "-ddd", c => 1 } { b => "-ddd", c => 1 } ##### ["-b", "-c", "-ddd"] { b => "-c", d => "dd" } { b => "-c", d => 3 }

In reply to Re: Inconsistent behavior of Getopt::Std by haukex
in thread Inconsistent behavior of Getopt::Std by likbez

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.