Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Getopt::Declare A quickie overview.

Getopt::Declare is a module for parsing command line options -- and like many of Damian Conway's modules, this one has obviously been eating its wheaties with large doses of steroids (and it arrives with circa 1,500 lines of documentation to prove it). In short, this is not your mother's command line parser.

Not satisfied with giving us yet another command line parser, Damian has given us a declarative language to specify not only command line options, but descriptions, parameter types, and actions as well. But don't let the size of the docs intimidate you, it is surprisingly simple to use.

The basic mechanics of using the module are as follows:

#!/usr/bin/perl -w use strict; use Getopt::Declare; my $spec = <<'SPEC'; # put your specification here SPEC my $ops = Getopt::Declare->new($spec); # rest of your program

Obviously, it is the specification we are really interested in. So let's look a very trivial greplike script using the module:

#!/usr/bin/perl -w use strict; use Getopt::Declare; use vars qw/$not $re/; $::VERSION = 0.01; my $spec = <<'EOS'; -p <pattern> pattern [required] { $re = $pattern } -f <INFILE>... input filename(s) [required] { defer{process(@INFILE)} } -not print out non-matches { $not = 1 } EOS my $opts = Getopt::Declare->new($spec); sub process { @ARGV = @_; while(<>){ if($::not){ print unless /$::re/; } else { print if /$::re/; } } } __END__

An option declaration is comprised of a few components: the option specification itself (followed by one or more tabs); the option description (with optional directives); and an optional action block to be executed if the option is found. Let's break out the -f option above and look at each component:

-f <INFILE>... # So we specify an option that looks like '-f' which takes one or # more arguments (that's the ... part) that will be stored in the # variable @INFILE for the action block input filename(s) [required] # This is the description of the option followed by the # [required] directive which means this option must be present on # the command line { defer{process(@INFILE)} } # This is the action block. The defer() function is from # Getopt::Declare and takes a code block which will not be # executed until all the command line options have been parsed. # Here we merely provide our own function and pass it the files # from the -f option as arguments.

The option variable is available as a lexical variable within the action block, and you may set or access any globals that are available at the time of parsing. In our example above we set the global $re and $not variables in the action blocks so we can access those later rather than accessing those options via the $opts object. We deferred our file processing action because we want to ensure all options have been parsed (and all globals set) before we start grepping our files.

You can also restrict parameter types when specifying parameter variables using a few predefined parameter types:

-p <var:s> # accept strings -n <var:n> # accept any real number -i <var:i> # accept only integers

And, because this is Perl, you can also specify your own regex to limit the types of things a parameter should accept or define new types:

-hw <hw:/\d+x\d+$/> HeightxWidth coordinates #or [pvtype: coord /\d+x\d+$] -hw <hw:coord> HeightxWidth coordinates

This module also gives us a -help option for free, and a -v option (if you've defined a $VERSION variable). Here's what we get with those:

$ perl -v version 0.01 (Fri Feb 2 09:30:34 2001) $ perl -help Usage: [options] -p <pattern> -f <INFILE>... -help -version Options: -p <pattern> pattern -f <INFILE>... input filename(s) -not print out non-matches

And, with 1,500 lines of documentation I've clearly only scratched the surface in this little overview. Go ahead and grab it, read the docs, and play around (and, if you're feeling brave, read the code too).

In reply to Getopt::Declare by danger

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (2)
    As of 2018-02-19 06:40 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (258 votes). Check out past polls.