Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
There's more than one way to do things
 
PerlMonks  

MooseX::Getopt help without nagging

by Boldra (Deacon)
on Aug 24, 2010 at 10:07 UTC ( #856896=perlquestion: print w/ replies, xml ) Need Help??
Boldra has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple MooseX::GetOpt class:
package MXGOTest; use Moose; use 5.010; with 'MooseX::Getopt'; has name => ( is => 'rw', isa => 'Str', required => 1); sub run { say "NAME=" . shift->{name}; } 1;
but when I run with -? --man or --help I get reminded that I'm missing a required switch:
$ perl mxgotest.pl --help Required option missing: name usage: mxgotest.pl [-?] [long options...] -? --usage --help Prints this usage information. --name
Is there a way to get the usage information without getting complaints about missing args?

Thanks




- Boldra

Comment on MooseX::Getopt help without nagging
Select or Download Code
Re: MooseX::Getopt help without nagging
by stonecolddevin (Vicar) on Aug 24, 2010 at 19:08 UTC

    change

    has name => ( is => 'rw', isa => 'Str', required => 1);

    to

    has name => ( is => 'rw', isa => 'Str' );

    mtfnpy

Re: MooseX::Getopt help without nagging
by bobr (Monk) on Aug 24, 2010 at 19:52 UTC
    Hi,

    MooseX::GetOpt just translates options to following call to Getopt::Long::Descriptive

    use Getopt::Long::Descriptive; my ($opt, $usage) = describe_options( 'usage: mxgotest.pl [-?] [long options...]', [ 'help|usage|?!' => 'Prints this usage information.' ], [ 'name=s' => 'Sets the name', { required => 1 } ], );

    The message is caused by required parameter. From the GLD docs:

    If an option is required, failure to provide the option will result in describe_options printing the usage message and exiting.

    I would remove the required from the name attribute and implemented the functionality after new_with_options call.

    -- Roman

      Ok, then this looks to me like a bug in Getopt::Long::Descriptive . I want something that behaves more like standard unix utils like rm:

      If I type just rm, I get a message that I'm missing a parameter AND the usage information. If I type rm --help I get the help information and no complaints.

      In my simple example leaving required out might appear to be a solution, but in practice I might have four or six required arguments, and using moose features could save me about ten lines of code.





      - Boldra
Re: MooseX::Getopt help without nagging
by TomDLux (Vicar) on Aug 25, 2010 at 14:50 UTC

    I have all the functionality in Moose module files, and then have a short .pl file to bring it all together.

    prepaprse_args() my $xyz = XYZ::ABC->new_with_options(); # obfuscated to protect my e +mployment $xyz->run(); __END__

    In preparse_args I quickly scan the args for -man or --man, which I handle with pod2usage. I also accept '-man modules', in which case I print a list of all the custom modules in the program ... the non CPAN ones. With the request '-man XYZ::ABC.pm', I print the pod for that one module. This way I have access to user documentation as well as inner details.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      This looks like a good solution. I don't expect Getopt::Long::Descriptive will provide a fix, as I think it would involve an API change.

      Thanks



      - Boldra

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://856896]
Approved by moritz
Front-paged by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (21)
As of 2014-04-18 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (471 votes), past polls