Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
It would really help if you showed some code and a specific situation.

"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

In reply to Re: GetOpt ignore required arguments by Marshall
in thread GetOpt ignore required arguments by stevensw

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found