Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://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 dopt.pl -v dopt.pl: version 0.01 (Fri Feb 2 09:30:34 2001) $ perl dopt.pl -help Usage: dopt.pl [options] -p <pattern> -f <INFILE>... dopt.pl -help dopt.pl -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

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 learning in the Monastery: (3)
As of 2024-04-16 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found