How to create application level options using Perl App :: Cmd?

FMc update

I put generosity on this question because I am puzzled by the same problem. To rephrase the question, how do you implement application-level parameters (those that apply to the entire program, script.pl ), as opposed to those that apply to individual commands ( search in this example).

Original question

How can I use App :: Cmd to create such an interface

 script.pl --config <file> search --options args 

?

I can do:

 ./script.pl search --options args ./script.pl search args ./script.pl search --options 

What I'm trying to achieve is to get an option for a configuration file, for example:

 ./script.pl --config file.conf search --options args 

I looked at App :: Cmd :: Tutorial on cpan, but so far I have not been able to get it to work.

+6
command-line perl perl-module
source share
1 answer

You can specify global parameters in App :: Cmd , as shown below. We need three files:

script.pl

 use Tool; Tool->run; 

Tool.pm

 package Tool; use strict; use warnings; use base 'App::Cmd'; sub global_opt_spec { ['config=s' => "Specify configuration file"]; } 1; 

and Tool / Command / search.pm :

 package Tool::Command::search; use strict; use warnings; use base 'App::Cmd::Command'; sub opt_spec { ["option" => "switch on something"], } sub execute { my ($self, $opt, $args) = @_; warn "Running some action\n"; warn 'Config file = ' . $self->app->global_options->{config}, "\n"; warn 'Option = ' . $opt->{option}, "\n"; } 1; 

This example shows how to define a global option and access it from search .

+6
source share

All Articles