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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.