First, I should qualify all the following with the comment that I use Getopt::Long in all my own work; I've found it sufficiently powerful for all my needs, straight forward to use, and it's CORE so I don't have to worry about library management.
You've got a number of code problems, most of which which would be caught by strict. The strict pragma implements a number of restrictions on your code, such as requiring all variables be either declared with a my or our or have a fully qualified package name. See Use strict warnings and diagnostics or die.
So bugs in the provided code are:
or something close.$ap = Getopt::ArgParse->new_parser( { prog => "test_argparse.pl", description => "Test the Getopt::Argparse module" } );
=> stringifies the preceding argument, but you must still wrap the trailing argument in quotes. Other than that, it's just a comma.$ap->add_argument('-c', '--count', type => 'Scalar'); $ap->add_argument('-f', '--flag', type => 'Bool');
$ap->add_argument('--count', '-c', type => 'Scalar'); $ap->add_argument('--flag', '-f', type => 'Bool');
HOWEVER, since $args is not just an ordinary hash ref, but an object, it is good practice to use the provided accessors. This protects you from typos in hash keys. The module provides two methods for accessing field names: either via the $args->get_attr(ARG) method, or via the Moose-like getter $args->ARG So you could get your desire result with eitherif (! defined($args->{'count'})) {
orif (! defined($args->get_attr('count'))) {
assuming you've made the argument order correction above.if (! defined($args->count)) {
#!/usr/bin/perl -w use strict; require Getopt::ArgParse; my $ap = Getopt::ArgParse->new_parser( { prog => "test_argparse.pl", description => "Test the Getopt::Argparse module" }); $ap->add_argument('--count', '-c', type => 'Scalar'); $ap->add_argument('--flag', '-f', type => 'Bool'); my $args = $ap->parse_args(); # Now, I am assuming that $args is a pointer to a hash array # that contains elements for "count" and "flag" if (! defined($args->count)) { print "count not defined\n"; }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
In reply to Re: How to access GetOpt::ArgParse arguments?
by kennethk
in thread How to access GetOpt::ArgParse arguments?
by SQADude
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |