neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
Greetings, Is the following possible? I'm making a template for future scripts and programs. I thought it would be useful to separate the standard cli args (man, help, usage, etc) to a separate package. My first try returns an error. Why?
#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Getopt::Long; use Pod::Usage; use Data::Dumper; my $VERSION = 1; # Get standard options from reusable module my $stdopts = Std::Opts->new(); my $std_cli_arg_ref = $stdopts->get_standard_args; # Read, process, and validate cli args my $cli_arg_ref; GetOptions( $cli_arg_ref, $std_cli_arg_ref, # These two samples can be removed 'myarg=s', 'arg2=i', ); # # Modules # package Std::Opts; use strict; use warnings; sub new { my ( $class ) = @_; return bless {}, $class; } sub get_standard_args { my $self = shift; my $std_cli_arg_ref = { 'version' => sub { say $VERSION; exit + }, 'test' => sub { _run_tests(); exit + }, 'man' => sub { pod2usage( -verbose => 2, -exitval => 0 ) + }, 'dumpargs' => sub { say '$cli_arg_ref = '. Dumper( $cli_arg_ref ); exit }, 'help|?' => sub { pod2usage( -sections => ['OPTIONS'], -exitval => 0, -verbose + => 99) }, 'usage' => sub { pod2usage( -sections => ['SYNOPSIS'], -exitval => 0, -verbose + => 99) }, 'examples' => sub { pod2usage( -sections => 'EXAMPLES', -exitval => 0, -verbose + => 99) }, }; return $std_cli_arg_ref; } 1; Returns: $ ./foo.pl -du Undefined argument in option spec Error in option spec: "HASH(0x1772fc8)"
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing data to getopts::long
by 1nickt (Canon) on Jul 29, 2015 at 00:39 UTC | |
|
Re: Passing data to Getopt::Long
by Laurent_R (Canon) on Jul 29, 2015 at 08:38 UTC | |
|
Re: Passing data to getopts::long
by Anonymous Monk on Jul 29, 2015 at 00:13 UTC |