in reply to Moo and command line scripts

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.

Replies are listed 'Best First'.
Re^2: Moo and command line scripts
by boftx (Deacon) on Nov 09, 2019 at 20:11 UTC

    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;