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 }