Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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 --help Required option missing: name usage: [-?] [long options...] -? --usage --help Prints this usage information. --name
Is there a way to get the usage information without getting complaints about missing args?


- Boldra

Replies are listed 'Best First'.
Re: MooseX::Getopt help without nagging
by stonecolddevin (Vicar) on Aug 24, 2010 at 19:08 UTC


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


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


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

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

    use Getopt::Long::Descriptive; my ($opt, $usage) = describe_options( 'usage: [-?] [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', 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.


      - Boldra

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://856896]
Approved by moritz
Front-paged by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-05-25 20:57 GMT
Find Nodes?
    Voting Booth?