Skip Menu |

This queue is for tickets about the Getopt-Long CPAN distribution.

Report information
The Basics
Id: 72561
Status: resolved
Priority: 0/
Queue: Getopt-Long

People
Owner: jv [...] cpan.org
Requestors: svgkraju [...] yahoo.com
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long
use strict; use warnings; use Getopt::Long; my @letters; my @words; GetOptions( "letters=s{2}" => \@letters, "words=s{,}" => \@words ); print "Letters: " . join(", ", @letters) . "\n"; print "Words: " . join(", ", @words) . "\n"; When I run this program I get the output as follows: perl getopts.pl --letters a --words he she it Letters: a, --words Words: --words is read as part of --letters arguments itself. I expect GetOptions to throw error message in this scenario. How to get this done. FYI.... Z:\examples\perl>perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x86-multi-t hread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2011, Larry Wall Binary build 1402 [295342] provided by ActiveState http://www.ActiveState.com Built Oct 7 2011 15:49:44 Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.
CC: undisclosed-recipients:;
Subject: Re: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long
Date: Sun, 20 Nov 2011 15:16:17 +0100
To: bug-Getopt-Long [...] rt.cpan.org
From: Johan Vromans <jvromans [...] squirrel.nl>
[Quoting SVGK Raju via RT, on November 20 2011, 03:23, in "[rt.cpan.org #72561]"> GetOptions( Show quoted text
> "letters=s{2}" => \@letters, > "words=s{,}" => \@words > );
What you say here is that you want letters to take exactly two arguments. You probably meant: "letters=s{,2}" => \@letters, which means: up to 2 arguments. Now argument parsing may (and will) stop when it encounters "--words". % perl getopts.pl --letters a --words he she it Letters: a Words: he, she, it HTH, Johan PS: This may also be an option: "letters=s{1,2}" => \@letters, meaning: at least one, at most two.
Subject: RE: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long
Date: Thu, 15 Dec 2011 07:52:38 +0530
To: <bug-Getopt-Long [...] rt.cpan.org>
From: "SVGK Raju" <svgkraju [...] yahoo.com>
I am looking for exactly two, if two arguments are not provided, getoptions must through error message. As per the documentation at http://perldoc.perl.org/Getopt/Long.html ----------------------------- GetOptions('coordinates=f{2}' => \@coor, 'rgbcolor=i{3}' => \@color); The destination for the option must be an array or array reference. It is also possible to specify the minimal and maximal number of arguments an option takes. foo=s{2,4} indicates an option that takes at least two and at most 4 arguments. foo=s{,} indicates one or more values; foo:s{,} indicates zero or more option values. ------------------------------- Bug is that when exact number of arguments are asked, instead of error out, getoptions completely messes it up. Show quoted text
-----Original Message----- From: jvromans@squirrel.nl via RT [mailto:bug-Getopt-Long@rt.cpan.org] Sent: 20 November 2011 19:46 To: svgkraju@yahoo.com Subject: Re: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long <URL: https://rt.cpan.org/Ticket/Display.html?id=72561 > [Quoting SVGK Raju via RT, on November 20 2011, 03:23, in "[rt.cpan.org #72561]"> GetOptions(
> "letters=s{2}" => \@letters, > "words=s{,}" => \@words > );
What you say here is that you want letters to take exactly two arguments. You probably meant: "letters=s{,2}" => \@letters, which means: up to 2 arguments. Now argument parsing may (and will) stop when it encounters "--words". % perl getopts.pl --letters a --words he she it Letters: a Words: he, she, it HTH, Johan PS: This may also be an option: "letters=s{1,2}" => \@letters, meaning: at least one, at most two.
On Wed Dec 14 21:22:40 2011, svgkraju wrote: Show quoted text
> I am looking for exactly two, if two arguments are not provided, > getoptions must through error message.
AFAIK, it does. Show quoted text
> Bug is that when exact number of arguments are asked, instead of error > out, getoptions completely messes it up.
Please supply a small sample program that shows the messing up.
Subject: RE: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long
Date: Fri, 16 Dec 2011 10:05:10 +0530
To: <bug-Getopt-Long [...] rt.cpan.org>
From: "SVGK Raju" <svgkraju [...] yahoo.com>
use strict; use warnings; use Getopt::Long; my @letters; my @words; GetOptions( "letters=s{2}" => \@letters, "words=s{,}" => \@words ); print "Letters: " . join(", ", @letters) . "\n"; print "Words: " . join(", ", @words) . "\n"; When I run this program I get the output as follows: perl getopts.pl --letters a --words he she it Letters: a, --words Words: Show quoted text
-----Original Message----- From: Johan Vromans via RT [mailto:bug-Getopt-Long@rt.cpan.org] Sent: 15 December 2011 12:40 To: svgkraju@yahoo.com Subject: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long <URL: https://rt.cpan.org/Ticket/Display.html?id=72561 > On Wed Dec 14 21:22:40 2011, svgkraju wrote:
> I am looking for exactly two, if two arguments are not provided, > getoptions must through error message.
AFAIK, it does.
> Bug is that when exact number of arguments are asked, instead of error > out, getoptions completely messes it up.
Please supply a small sample program that shows the messing up.
Subject: RE: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long
Date: Fri, 16 Dec 2011 10:08:26 +0530
To: <bug-Getopt-Long [...] rt.cpan.org>
From: "SVGK Raju" <svgkraju [...] yahoo.com>
Even after using die, it does not. use strict; use warnings; use Getopt::Long; my @letters; my @words; GetOptions( "letters=s{2}" => \@letters, "words=s{,}" => \@words ) or die ("incorrect options"); print "Letters: " . join(", ", @letters) . "\n"; print "Words: " . join(", ", @words) . "\n"; When I run this program I get the output as follows: perl getopts.pl --letters a --words he she it Letters: a, --words Words: Show quoted text
-----Original Message----- From: SVGK Raju [mailto:svgkraju@yahoo.com] Sent: 16 December 2011 10:05 To: 'bug-Getopt-Long@rt.cpan.org' Subject: RE: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long use strict; use warnings; use Getopt::Long; my @letters; my @words; GetOptions( "letters=s{2}" => \@letters, "words=s{,}" => \@words ); print "Letters: " . join(", ", @letters) . "\n"; print "Words: " . join(", ", @words) . "\n"; When I run this program I get the output as follows: perl getopts.pl --letters a --words he she it Letters: a, --words Words:
-----Original Message----- From: Johan Vromans via RT [mailto:bug-Getopt-Long@rt.cpan.org] Sent: 15 December 2011 12:40 To: svgkraju@yahoo.com Subject: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long <URL: https://rt.cpan.org/Ticket/Display.html?id=72561 > On Wed Dec 14 21:22:40 2011, svgkraju wrote:
> I am looking for exactly two, if two arguments are not provided, > getoptions must through error message.
AFAIK, it does.
> Bug is that when exact number of arguments are asked, instead of error > out, getoptions completely messes it up.
Please supply a small sample program that shows the messing up.
Thanks for the clarification. The example program behaves exacty as intended. perl getopts.pl --letters a --words he she it Option "letters" is specified to take exactly two arguments. So it gets two arguments: "a" and "--words". See also the difference between option specifier "=s" and ":s". You probably want something like use strict; use warnings; use Getopt::Long; my @letters; my @words; GetOptions( -> "letters=s{,2}" => \@letters, "words=s{,}" => \@words ) or die ("incorrect options"); -> die("not enough values for --letters") unless @letters == 2; print "Letters: " . join(", ", @letters) . "\n"; print "Words: " . join(", ", @words) . "\n";
Subject: RE: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long
Date: Sat, 17 Dec 2011 19:54:09 +0530
To: <bug-Getopt-Long [...] rt.cpan.org>
From: "SVGK Raju" <svgkraju [...] yahoo.com>
Yes, the workaround given by you is the one that I am also using. However if getoptions is taking care of all these would be great. Show quoted text
-----Original Message----- From: Johan Vromans via RT [mailto:bug-Getopt-Long@rt.cpan.org] Sent: 16 December 2011 12:57 To: svgkraju@yahoo.com Subject: [rt.cpan.org #72561] Not able to restrict the number of arguments for an option with GetOptions in Getopt::Long <URL: https://rt.cpan.org/Ticket/Display.html?id=72561 > Thanks for the clarification. The example program behaves exacty as intended. perl getopts.pl --letters a --words he she it Option "letters" is specified to take exactly two arguments. So it gets two arguments: "a" and "--words". See also the difference between option specifier "=s" and ":s". You probably want something like use strict; use warnings; use Getopt::Long; my @letters; my @words; GetOptions( -> "letters=s{,2}" => \@letters, "words=s{,}" => \@words ) or die ("incorrect options"); -> die("not enough values for --letters") unless @letters == 2; print "Letters: " . join(", ", @letters) . "\n"; print "Words: " . join(", ", @words) . "\n";