CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Getopt::Std question

 



islanderman
Novice

Mar 7, 2014, 8:26 AM

Post #1 of 5 (2187 views)
Getopt::Std question Can't Post

Can someone tell me how I can catch an issue with a script where the option has a required parameter that is left blank.

my %opts;
getopts("vd:a:", \%opts);
my ($opt_v, $opt_d, $opt_a);
die $USAGE if (keys( %opts ) == 0);
print Dumper(%opts);


=> testopts.pl -d
$VAR1 = 'd';
$VAR2 = undef;

I can't say something like 'if ($opt_d) {' because the option is swalled by the required parameter?


islanderman
Novice

Mar 7, 2014, 8:44 AM

Post #2 of 5 (2186 views)
Re: [islanderman] Getopt::Std question [In reply to] Can't Post

Sorry, forgot to include the assign of the variables

my %opts;
getopts("vd:a:", \%opts);
my ($opt_y, $opt_a, $opt_v, $opt_d, $opt_m) = @opts{ qw(y a v d m) };

die $USAGE if (keys( %opts ) == 0);
print Dumper(%opts);


=> testopts.pl -d
$VAR1 = 'd';
$VAR2 = undef;

I can't say something like 'if ($opt_d) {' because the option is swalled by the required parameter?


FishMonger
Veteran / Moderator

Mar 7, 2014, 10:17 AM

Post #3 of 5 (2182 views)
Re: [islanderman] Getopt::Std question [In reply to] Can't Post

You'll need to manually test each var to verify that it was used and has a value.

Personally, I prefer to use the Getopt::Long module. It handles some of these checks for you and has more features. It also merges very nicely with Pod::Usage which makes it very easy to output a minimal or verbose usage statement.

http://search.cpan.org/~jv/Getopt-Long-2.42/lib/Getopt/Long.pm

The Getopt::Long documentation has a good example of the combined usage, but the Pod::Usage doc goes into more detail.

http://search.cpan.org/~marekr/Pod-Usage-1.63/lib/Pod/Usage.pm


FishMonger
Veteran / Moderator

Mar 7, 2014, 1:11 PM

Post #4 of 5 (2144 views)
Re: [islanderman] Getopt::Std question [In reply to] Can't Post

Here's an example of using the Getopt::Long and Pod::Usage modules from one of my scripts.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Getopt::Long;
use Text::CSV_XS;
use DBI;
use DBD::mysql;
use Pod::Usage;
use POSIX qw(strftime);
use File::Spec::Win32;

=pod

=head1 NAME

pf_csv.pl - Create csv file for PacketFence

=head1 SYNOPSIS

pf_csv.pl -help

pf_csv.pl -?

pf_csv.pl -man

pf_csv.pl -d 99

pf_csv.pl -d 99 -n 10.100.0.0

pf_csv.pl -v -d 99 -n 10.100.0.0 -o pfISO.csv

=cut

$|++;

my ($div, $net, $name, $help, $man, $verbose);

GetOptions (
'division|d=i' => \$div,
'network|n=s' => \$net,
'output|o=s' => \$name,
'help|?' => \$help,
'man' => \$man,
'verbose|v' => \$verbose
)or pod2usage(2);

pod2usage(-verbose => 1) if $help or !$div;
pod2usage(-verbose => 2) if $man;

open STDOUT, '>', File::Spec->devnull unless $verbose;
print "Generating csv file\n\n";

$div = sprintf "%03d", $div;
my $devices = db_query($div, $net);

create_csv_file($div, $devices, $name);

exit;

###############################################################################

# subroutine definitions removed

=pod

=head1 OPTIONS

=over 8

=item B<-?>, B<-help>

Prints the usage message and exits.

=item B<-man>

Prints the manual page and exits.

=item B<-v>, B<-verbose>

Enables outputting of status messages

=item B<-d> <div number>, B<-division> <div number>

Specify the required division number.

=item B<-n> <network address>, B<-net> <network address>

Specify a specific network address within the division to filter on when creating the csv file.

=item B<-o> <filename>, B<-output> <filename>

Override default output filename to be used when creating the csv file.

=back

=head1 DESCRIPTION

This program generates a csv file to be used in the PacketFence db.

The -division (aka -d) option is the only required parameter needed to build the csv file. The division number is used to query the database to extract the hostname and MAC address of all allocated devices in that division. That list is then looped over combined with other data to build a row of csv data and is sent to the output file.

The default output filename is in the format of pfXXX.csv where XXX is the division number. The -o (-output) option can be used to override the default name by specifying a different output filename.

If a division has multiple LAN's and the -n (-network) address is specified, the network address will be used as a filter to extract only those devices within that LAN. The -network and -output options are typically used together so that you can easily build a separate csv file for each LAN.

=head1 To-Do

TBD

=head1 Author

Ron Bergin <me@company.com>

=cut


If I execute it without any parameters or if I specify the -? or -help options, here's what I get.

Code
F:\App Support\code\Perl>pf_csv.pl 
Usage:
pf_csv.pl -help

pf_csv.pl -?

pf_csv.pl -man

pf_csv.pl -d 99

pf_csv.pl -d 99 -n 10.100.0.0

pf_csv.pl -v -d 99 -n 10.100.0.0 -o pfISO.csv

Options:
-?, -help
Prints the usage message and exits.

-man Prints the manual page and exits.

-v, -verbose
Enables outputting of status messages

-d <div number>, -division <div number>
Specify the required division number.

-n <network address>, -net <network address>
Specify a specific network address within the division to filter
on when creating the csv file.

-o <filename>, -output <filename>
Override default output filename to be used when creating the
csv file.


If I pass the -d option (which is the only required option) without a value, this is what I get:

Code
F:\App Support\code\Perl>pf_csv.pl -d 
Option d requires an argument
Usage:
pf_csv.pl -help

pf_csv.pl -?

pf_csv.pl -man

pf_csv.pl -d 99

pf_csv.pl -d 99 -n 10.100.0.0

pf_csv.pl -v -d 99 -n 10.100.0.0 -o pfISO.csv



(This post was edited by FishMonger on Mar 8, 2014, 12:01 PM)


BillKSmith
Veteran

Mar 8, 2014, 2:57 PM

Post #5 of 5 (1849 views)
Re: [islanderman] Getopt::Std question [In reply to] Can't Post

It is possible to test for required parameters.

Code
use strict; 
use warnings;
use Data::Dumper;
use Getopt::Std;
my $USAGE = "USAGE: $0 -a=num -d=num [-y] [-v] [-m]\n";
my %opts;
getopts("ymvd:a:", \%opts);
print Dumper(\%opts);
my @required = qw(d a);
die $USAGE if grep {!exists $opts{$_} or !defined $opts{$_}} @required;

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives