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: Re: [islanderman] Getopt::Std question: Edit Log



FishMonger
Veteran / Moderator

Mar 7, 2014, 1:11 PM


Views: 3712
Re: [islanderman] Getopt::Std question

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)


Edit Log:
Post edited by FishMonger (Veteran) on Mar 7, 2014, 1:15 PM
Post edited by FishMonger (Veteran) on Mar 8, 2014, 12:01 PM


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

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