I'm not an expert in command line parsing; but command line parsing is a part of your program that must be easy and quick to program, and very easy to extend (program options, if there are not planified, may grow without control!).
I like very much the Getopt::Std and Getopt::Long (dragonchild mentioned yet), because its similarity to the 'getopt' (man 3 getopt) function in C.
Anyway, i usually program my own function for parsing arguments, like this (for example):
#!/usr/bin/perl -w
use strict;
my $logfile = "/var/log/whatever.log";
my $verbose = 0;
parseArgs($#ARGV + 1, @ARGV);
#
# ... do stuff with the logfile ...
#
# subroutine parseArgs
sub parseArgs {
my ($argc, @argv) = @_;
my $need_help = 0;
# is mandatory to receive comand args?
usage() if ( $argc == 0 );
for ( my $i=0; $i < $argc; $i++ ) {
foreach ($argv[$i]) {
if ( /^-logfile$|^-l$/ ) {
if ( $argv[$i + 1] ) {
#here you must do, if you want, some checks
#before asing nothing ...
$logfile = $argv[++$i];
} else {
#complain!
$needs_help = 1;
}
} elsif (/^-v$|^--verbose$/) {
$verbose = 1;
} elsif (/^-h$|^--help$) {
$needs_help = 1;
} else {
print "Command argument ",
($argv[$i]),
" is not valid\n");
$needs_help = 1;
}
}
}
usage() if ( $needs_help );
}
sub usage {
print <<EOF
usage: program [-l logfile] [-v] [-h]
blablabla ....
EOF
exit 0;
}
It simple, and not difficult to understand and modify. Yes i know its not exactly what you are asking for, sorry ...
Good Luck!
perl -Te 'print map { chr((ord)-((10,20,2,7)
$i++)) } split //,"turo"'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.