| 1 | package MooX::Role::CliOptions; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Getopt::Long v2.36 qw(GetOptionsFromArray); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # this could have been replaced with hand-rolled "isa" clauses, but if | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # one is using Moo then the odds are good that one already has this | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Types::Standard qw( ArrayRef Bool ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | my @options = ( 'help', 'man' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | do { | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | has debug => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | has verbose => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => Bool, | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | default => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | push( @options, qw( debug! verbose! ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | } unless $ENV{MRC_NO_STDOPTS}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | has argv => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | isa => ArrayRef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 25 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=17 SCAR=30.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 25 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 25 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 25 | ▶ | my $argv = delete( $args{argv} ) or croak q{'argv' argument is required}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 25 | ▶ | croak q{'argv' must be an array reference'} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $argv and ref $argv eq "ARRAY"
MC/DC0%0/2
ref $argv and ref $argv eq "ARRAY"
ref $argv
ref $argv eq "ARRAY"
Truth table33%1/3
A and B
A= ref $argvB= ref $argv eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | unless ref($argv) && ref($argv) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 25 | ▶ | my $add_opts = delete( $args{add_opts} ) || []; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{'add_opts'} || []
MC/DC100%1/1
delete $args{'add_opts'} || []
delete $args{'add_opts'}
Truth table100%2/2
A
A= delete $args{'add_opts'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 25 | ▶ | croak q{'add_opts' must be an array reference'} | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $add_opts and ref $add_opts eq "ARRAY"
MC/DC0%0/2
ref $add_opts and ref $add_opts eq "ARRAY"
ref $add_opts
ref $add_opts eq "ARRAY"
Truth table33%1/3
A and B
A= ref $add_optsB= ref $add_opts eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | unless ref($add_opts) && ref($add_opts) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 25 | ▶ | croak q{unknown argument supplied for 'init'} if keys(%args); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 25 | push( @options, $_ ) for ( @{$add_opts} ); | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 25 | my %values; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 25 | ▶ | GetOptionsFromArray( $argv, \%values, @options ) or _pod2usage(2); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 25 | ▶ | _pod2usage(1) if $values{help}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 25 | ▶ | _pod2usage( -exitstatus => 0, -verbose => 2 ) if $values{man}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 25 | $values{argv} = $argv; | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | do { | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # have (no)debug imply verbose if that is not not specificed. | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # This is a # personal preference based on my experience of how | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # these options are most commonly used. | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | $values{verbose} ||= $values{debug} | ||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 16 | ▶ | if defined( $values{debug} ) && !defined( $values{verbose} ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $values{"debug"} and not defined $values{"verbose"}
$values{"verbose"} ||= $values{"debug"}
MC/DC100%2/2
defined $values{"debug"} and not defined $values{"verbose"}
defined $values{"debug"}
not defined $values{"verbose"}
MC/DC50%1/2
$values{"verbose"} ||= $values{"debug"}
$values{"verbose"}
$values{"debug"}
Truth table100%3/3
A and not(B)
A= defined $values{"debug"}B= not defined $values{"verbose"}
Truth table66%2/3
A ||= B
A= $values{"verbose"}B= $values{"debug"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 25 | ▶ | } unless $ENV{MRC_NO_STDOPTS}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 25 | my $app = eval { $class->new(%values); }; | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 25 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | print $@ . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | _pod2usage(2); | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | } if $@; | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 25 | return $app; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | # this is needed so test scripts can intercept the call to pod2usage and | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # prevent the exit from happening | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub _pod2usage { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pod2usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | pod2usage(@_); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | 1; # End of MooX::Role::CliOptions | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||