Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Moo and command line scripts

by boftx (Deacon)
on Nov 09, 2019 at 01:02 UTC ( #11108502=perlmeditation: print w/replies, xml ) Need Help??

Hello fellow monks! It is good to once again tread these hallowed halls after all these moons of moons.

I have been a fan of modulinos (thanks Brian D'Foy!) for a long time now because of the advantages for testability. I have also been a fan of Moo for several years. And of course, like many others, I have combined both techniques with Getopt::Long. I have also been using a standard set of command line options such as "--debug" and "--help", with "--debug" defaulting true so "--nodebug" is required to turn it off.

In an effort to reduce boilerplate and a lot of copy-and-paste I wrote a simple Moo role to handle the basics for me. I have since refined it with a proper test suite and POD and it has now been released as MooX::Role::CliOptions. I would greatly appreciated any feedback from my brothers and sisters of the Monastery on any and all aspects.

I am well aware that there are other ways of doing this, including a couple of other modules on CPAN. I hope that some will find this new Role to be easier to use in some respects, as well as being more "Moo-ish".

You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Replies are listed 'Best First'.
Re: Moo and command line scripts
by tobyink (Canon) on Nov 09, 2019 at 11:23 UTC

    Have you considered using Package::Variant instead of the environment variable MRC_NO_STDOPTS to configure the role?

    The environment variable might lead to inconsistent behaviour if an application includes multiple classes that all consume your role, with some of them wanting MRC_NO_STDOPTS to be true and others wanting it to be false.

      Thanks, I'll take a look at that. I'm not completely comfortable with the environment variable myself but all of my tests seem to show it works properly.

      You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

        Yeah, it only becomes an issue if you're loading two classes that consume MooX::Role::CliOptions, and they each want different settings for the environment variable. None of your tests do that.

        use 5.006; use strict; use warnings; use Test::More; { package My::Class1; use Moo; $ENV{MRC_NO_STDOPTS} = 1; # do NOT want 'debug' and 'verbose' with 'MooX::Role::CliOptions'; } { package My::Class2; use Moo; $ENV{MRC_NO_STDOPTS} = 0; # do want 'debug' and 'verbose' with 'MooX::Role::CliOptions'; } ok( ! My::Class1->new->can('verbose') ); ok( My::Class2->new->can('verbose') ); done_testing;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://11108502]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2019-12-13 20:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?