Getoptions Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business perl getoptions example Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation
Perl Getoptions Multiple Values
Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just perl command line parameters like you, helping each other. Join them; it only takes a minute: Sign up GetOptions Check Option Values up vote 0 down vote favorite I am updating an existing Perl script that uses GetOptions from Getopt::Long. I
Getopt::long Example
want to add an option that takes a string as its parameter and can only have one of 3 values: small, medium, or large. Is there any way to make Perl throw an error or kill the script if any other string value is specified? So far I have: my $value = 'small'; GetOptions('size=s' => \$value); perl parameters options share|improve this question edited Jul 22 '15 at 14:20 asked Jul 22 '15 at 13:58 Samuel perl command line options 1,47911827 add a comment| 5 Answers 5 active oldest votes up vote 2 down vote You could use a subroutine to handle the processing of that option. User-defined subroutines to handle options my $size = 'small'; # default GetOptions('size=s' => \&size); print "$size\n"; sub size { my %sizes = ( small => 1, medium => 1, large => 1 ); if (! exists $sizes{$_[1]}) { # die "$_[1] is not a valid size\n"; # Changing it to use an exit statement works as expected print "$_[1] is not a valid size\n"; exit; } $size = $_[1]; } I put the sizes into a hash, but you could use an array and grep as toolic showed. share|improve this answer edited Jul 22 '15 at 14:38 answered Jul 22 '15 at 14:19 Ron Bergin 910156 1 Strange. That doesn't die for me if I use something like -size foo –toolic Jul 22 '15 at 14:25 According to the docs it should have died, but you're right, it didn't. Let me run another test. –Ron Bergin Jul 22 '15 at 14:27 Perl v5.12.2. It does die if I use GetOptions('size=s' => \&size) or die; –toolic Jul 22 '15 at 14:32 2 From the docs - "If the subroutine needs to signal an error, it should call die() with the desi
CoolUsesForPerl PerlNews Q&A Tutorials Poetry RecentThreads NewestNodes Donate What'sNew on Aug 24, 2010 at 14:07UTC ( #856941=perlquestion: print w/replies, xml ) Need Help?? shan_emails has asked for the wisdom of
Perl Optional Command Line Arguments
the Perl Monks concerning the following question: Hi All, I
Perl Pod2usage
used Getopt::Long module for command line Input Arguments. The perl file is executed without parameters. But getopt long c i want the perl file won't execute without parameters. How can i control this case using Getopt::Long. example use Getopt::Long; my $data = "file1"; my $length http://stackoverflow.com/questions/31565326/getoptions-check-option-values = 4; my $verbose; GetOptions ("length=i" => \$length, "file=s" => \$data,"verbose" => + \$verbose); [download] Here filename is - testcmd.pl when i run in command prompt as below it doesn't show any error. perl testcmd.pl Thanks in advance, Shanmugam A. Comment on using "Getopt::Long" how to check parameter mandatoryDownload Code Replies are http://www.perlmonks.org/?node_id=856941 listed 'Best First'. Re: using "Getopt::Long" how to check parameter mandatory by VinsWorldcom (Vicar) on Aug 24, 2010 at 14:45UTC Just check @ARGV after you parse your options: use Getopt::Long; my $data = "file1"; my $length = 4; my $verbose; GetOptions ( "length=i" => \$length, "file=s" => \$data, "verbose" => \$verbose ); if (!@ARGV) { print "$0: Argument required.\n"; exit 1 } [download] I also like to use POD to document my code; thus, pod2usage can be invoked to produce 'nicer' usage output: use Pod::Usage; ... GetOptions( ... ) or pod2usage(-verbose => 0); ... # Make sure at least one argument provided if (!@ARGV) { pod2usage(-verbose => 0, -message => "$0: argument required\n") } [download] [reply][d/l][select] Re: using "Getopt::Long" how to check parameter mandatory by JavaFan (Canon) on Aug 24, 2010 at 14:46UTC How can i control this case using Getopt::Long. As Johan Vromans (author of Getopt::Long) tends to say "mandatory arguments aren't option
Function Description ____________________________________________________________________________ GetOptions ____________________________________________________________________________ Get options from command line parameters. Syntax: ${GetOptions} "[Parameters]" "[Option]" $var "[Parameters]" ; command line parameters ; "[Option]" ; option http://nsis.sourceforge.net/GetOptions name ; $var ; Result: option string Note: -Error flag if option not found -First option symbol it is delimiter Example1: Section ${GetOptions} "/S /T" "/T" $R0 IfErrors 0 +2 MessageBox MB_OK "Not found" IDOK +2 MessageBox MB_OK "Found" SectionEnd Example2: Section ${GetOptions} "-INSTDIR=C:\Program Files\Common Files -SILENT=yes" "-INSTDIR=" $R0 ;$R0=C:\Program Files\Common Files SectionEnd Example3: Section ${GetOptions} '/SILENT=yes /INSTDIR="C:/Program Files/Common Files" /ADMIN=password' "/INSTDIR=" $R0 ;$R0=C:/Program Files/Common Files command line SectionEnd Example4: Section ${GetOptions} `-SILENT=yes -INSTDIR='"C:/Program Files/Common Files"' -ADMIN=password` "-INSTDIR=" $R0 ;$R0="C:/Program Files/Common Files" SectionEnd Function Code Function GetOptions !define GetOptions `!insertmacro GetOptionsCall` !macro GetOptionsCall _PARAMETERS _OPTION _RESULT Push `${_PARAMETERS}` Push `${_OPTION}` Call GetOptions Pop ${_RESULT} !macroend Exch $1 Exch Exch $0 Exch Push $2 Push $3 Push $4 Push $5 Push $6 Push $7 ClearErrors StrCpy $2 $1 '' 1 StrCpy $1 $1 1 StrLen $3 perl command line $2 StrCpy $7 0 begin: StrCpy $4 -1 StrCpy $6 '' quote: IntOp $4 $4 + 1 StrCpy $5 $0 1 $4 StrCmp $5$7 '0' notfound StrCmp $5 '' trimright StrCmp $5 '"' 0 +7 StrCmp $6 '' 0 +3 StrCpy $6 '"' goto quote StrCmp $6 '"' 0 +3 StrCpy $6 '' goto quote StrCmp $5 `'` 0 +7 StrCmp $6 `` 0 +3 StrCpy $6 `'` goto quote StrCmp $6 `'` 0 +3 StrCpy $6 `` goto quote StrCmp $5 '`' 0 +7 StrCmp $6 '' 0 +3 StrCpy $6 '`' goto quote StrCmp $6 '`' 0 +3 StrCpy $6 '' goto quote StrCmp $6 '"' quote StrCmp $6 `'` quote StrCmp $6 '`' quote StrCmp $5 $1 0 quote StrCmp $7 0 trimleft trimright trimleft: IntOp $4 $4 + 1 StrCpy $5 $0 $3 $4 StrCmp $5 '' notfound StrCmp $5 $2 0 quote IntOp $4 $4 + $3 StrCpy $0 $0 '' $4 StrCpy $4 $0 1 StrCmp $4 ' ' 0 +3 StrCpy $0 $0 '' 1 goto -3 StrCpy $7 1 goto begin trimright: StrCpy $0 $0 $4 StrCpy $4 $0 1 -1 StrCmp $4 ' ' 0 +3 StrCpy $0 $0 -1 goto -3 St