package DNO::Options; use strict; use Exporter (); use vars qw( @ISA @EXPORT @EXPORT_OK ); @ISA = qw( Exporter ); @EXPORT = qw( get_options ); @EXPORT_OK = qw( uncommify ); use Getopt::Long qw( :config pass_through ); sub uncommify { my $aref = shift; return split /,/, join ',', @$aref; } sub get_options { my $def = shift; my $cur = {}; $cur->{$_->[0]} = $_->[2] for @$def; my @def = map {$_->[0].$_->[1]} @$def; GetOptions( $cur, @def ); return $cur; } 1; package DNO::Passwords; use strict; use vars qw( @ISA @EXPORT ); @ISA = qw( Exporter ); @EXPORT = qw( passwords ); use DNO::Options qw( get_options ); use DNO::Documentation qw( usage ); my ($def, $opt); $def = [ [ 'password', '=s%', {} ], ]; sub passwords { my ( @want ) = @_; $opt = get_options( $def ); my %pw = map { $_ => undef } @want; $pw{$_} = $opt->{password}{$_} for keys %{ $opt->{password} }; my @need = grep { not defined $pw{$_} } keys %pw; if ( @need ) { require DNO::Util; DNO::Util->import( qw/ get_passwords / ); my $pw = get_passwords( @need ); unless ( defined $pw ) { usage( -verbose => 1, -message => "$0: no passwords. did you kinit?$/" ); } $pw{$_} = $pw->{$_} for @need; @need = grep { not defined $pw{$_} } keys %pw; } if ( @need ) { usage( -verbose => 1, -message => "$0: missing passwords: @need$/" ); } return \%pw; } 1; package DNO::Devices; use strict; use vars qw( @ISA @EXPORT @EXPORT_OK ); @ISA = qw( Exporter ); @EXPORT = qw( devices ); @EXPORT_OK = qw( ); use DNO::Options qw( get_options uncommify ); use DNO::Documentation qw( usage ); my ($def, $opt); my @hosts; $def = [ [ 'host', '=s@', [] ], [ 'list', '=s@', [] ], ]; sub devices { $opt = get_options( $def ); foreach my $file ( uncommify($opt->{list}) ) { local *F; open F, '<', $file or usage( -verbose => 1, -message => "$0: open $file: $!$/"); while ( ) { s/#.*//; next if /^\s*$/; push @hosts, (split)[0]; } close F; } push @hosts, uncommify($opt->{host}); #unless ( @hosts ) { usage( -verbose => 1, -message => "$0: no devices to work with.$/" ); } return @hosts; } 1; package DNO::Documentation; use strict; # Globals use vars qw( @ISA @EXPORT @EXPORT_OK %INC ); # Exports @ISA = qw( Exporter ); @EXPORT = qw( usage ); @EXPORT_OK = qw( doc_setup ); # Use use Pod::Usage; use DNO::Options qw( get_options ); # Options definitions my ($def, $opt); $def = [ [ 'help', '', 0 ], [ 'man', '', 0 ], ]; # Class Data my %Default = ( ); # Find our documentation directory and add to searchpath my $class = __PACKAGE__ . '.pm'; $class =~ s/::/\//g; my $path = $INC{$class}; $path =~ s/\.pm$//; $Default{'-pathlist'} = "$path:$ENV{PATH}"; 1; #### #!/usr/bin/perl use strict; use warnings; $|++; #use Data::Dumper; use lib '../lib'; use DNO::Documentation qw( doc_setup usage ); doc_setup(); use DNO::Passwords qw( passwords ); my $pw = passwords( qw/ snmp.ro / ); #print Dumper($pw); use DNO::Devices qw( devices ); my @hosts = devices(); #print Dumper(\@hosts); use DNO::Options qw( get_options ); my ($def, $opt); $def = [ [ 'maxfork', '=i', 20 ], [ 'timeout', '=i', 2 ], [ 'retries', '=i', 2 ], ]; $opt = get_options( $def ); # ... do something __END__ $ dosumthin --host mary --list domain1,domain3 --password snmp.ro=public --timeout 5 --retries 4