O Wise Sages,

I'm developing a small netadmin-type script. I don't intend this to have much of an audience beyond a few folks in my immediate group, but as these things tend to grow in ways unimaginable, I want to make it somewhat flexible. I'm playing around with Getopt::Std to allow for switches.

I'd like the script to handle 5 switches, all of which are optional:
-h = help
-i and -o = input and output filenames
-w and -s = numeric values

If any of the -iows switches are used, they must be accompanied by a valid argument (filename for -io, numeric for -ws). The &usage sub should be called if -h is used, or if any of the other switches are used incorrectly (i.e., with a missing or invalid argument).

Coding these rules is more difficult than I thought. My best attempt is this:

#!/usr/bin/perl -w use strict; use Getopt::Std; my %args; getopts('w:s:i:o:h', \%args); &usage() if @ARGV || $args{h} || (exists $args{w} && !defined $args{w} || $args{w} !~ /^\d*$/) || (exists $args{s} && !defined $args{s} || $args{s} !~ /^\d*$/) || (exists $args{i} && !defined $args{i} || $args{i} =~ /^-\w$/) || (exists $args{o} && !defined $args{o} || $args{o} =~ /^-\w$/) ;

This $args{i} =~ /^-\w$/ was added to catch the situation where the script was called with just "-i -o" as parameters. getopts saw the "-o" as a valid argument to the "-i" switch.

This is close, but there are a few things wrong. It doesn't complain about unknown options (-x); it produces "Use of uninitialized value in pattern match" warnings when switches aren't used (I'm picky); and worst of all, adding all these specific catchalls doesn't seem very Perlish.

Seems like a good time to Ask The Experts. Is there a simple way to accomplish this with Getopt::Std, or would I need to move to Getopt::Long or some other mod?

Thanks!


In reply to Using GetOpt::Std to parse switches with arguments by EyeOpener

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.