Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Showing global options while using App::Cmd

by McA (Priest)
on Sep 25, 2012 at 08:19 UTC ( #995508=perlquestion: print w/replies, xml ) Need Help??
McA has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

just started to dig into App::Cmd and can't find the answer to the follworing question:

When I have defined global options with global_opt_spec how can I achieve that the usage of these options is printed when I call script help or script alone.

The command specific options are printed when you call script help command.

At the moment it seems to me that the only way is to call script --rubbish while --rubbish is an unknown option.

Hints appreciated.

Best regards

Replies are listed 'Best First'.
Re: Showing global options while using App::Cmd
by tobyink (Abbot) on Sep 25, 2012 at 08:29 UTC

    Personally I avoid global_opt_spec and instead use a base class for all commands. Let's say my app is called MyApp, then I'll do it like this:

    { package MyApp::Command; # abstract use strict; use App::Cmd::Setup-command; sub opt_spec { return ( [ 'someopt' => 'This is a global option' ], ); } # you can also define any helper methods you like here # and all your commands will have easy access to them. } { package MyApp::Command::foo; use strict; use MyApp-command; sub opt_spec { my $self = shift; return ( $self->SUPER::opt_spec(@_), [ 'otheropt' => 'This is another option' ], ); } }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Hi tobyink,

      thank you for the fast reply.

      Am I right, that all these "base" options would show up as command options?

      Do you know a way to solve my initial problem if I really want the distinction between global and command specific options?

      Best regards

Re: Showing global options while using App::Cmd
by McA (Priest) on Sep 25, 2012 at 09:16 UTC

    Hi all,

    I just found out, that calling script commands shows the global options. Only script help doesn't show them.

    So, my initial question is answered by myself. Does this raise my reputation? ;-)

    Best regards

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995508]
Approved by Corion
[atcroft]: james28909: That particular questions was a bit of trick, actually (depending on the country you are in). More interesting is, if you are trying to subtract from an epoch time, for instance, you might have to consider when/if DST occurs for a location,
[atcroft]: because you may have to adjust the number of seconds you change from an epoch from 86400 (not to mention leap seconds)....
[atcroft]: james28909: Although if your program is using a database, you might be able to "pass the buck" to the database and ask it to do the date change for you....
[stevieb]: ++ atcroft

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-04-29 04:42 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.