in reply to GetOpt ignore required arguments
"How to get getopt to ignore this field?"
Getopt already ignores what it does not understand. In the option string, you specify that an option takes an argument by the ':',semi-colon character. Getopt takes all of the stuff that it understands out of @ARGV and you process what it left over! In your case, sounds like exactly ONE thing should be left in @ARGV (see below). If you don't specify that an option needs an argument, then Getopt will leave the token after the option alone!
I show some examples that sound like they are close to your situation below. Having an "standalone" argument that looks like an option (-abc or whatever) is a very unusual situation. One way of dealing with this has been posted and there are others. Before rolling further down that rabbit hole, show us an actual example of your problem.
#!/usr/bin/perl -w use strict; use Getopt::Std; my %opts; my $usage = "usage: options [-a arga] [-b] mandatory_arg"; die "$usage \n" if !getopts("a:b", \%opts); die "error: arguments=".@ARGV." @ARGV\n$usage \n" if @ARGV !=1; print "ok\n"; __END__ # Wrong, "mandatory" used as -a's argument! C:\TEMP>option.pl -a mandatory error: arguments=0 usage: options [-a arga] [-b] mandatory_arg # Now ok! C:\TEMP>option -a arga mandatory ok # Error: b doesn't take an arg # looks like another mandatory arg! C:\TEMP>option -a -b arga mandatory error: arguments=2 arga mandatory usage: options [-a arga] [-b] mandatory_arg # -b doesn't expect an arg and that causes # error C:\TEMP>option.pl -b extra -a arga mandatory error: arguments=4 extra -a arga mandatory usage: options [-a arga] [-b] mandatory_arg # Now ok! C:\TEMP>option.pl -b -a arga mandatory ok
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: GetOpt ignore required arguments
by stevensw (Acolyte) on Aug 22, 2011 at 17:54 UTC | |
by Marshall (Canon) on Aug 24, 2011 at 11:00 UTC |