line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooX::Role::CliOptions; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
54081
|
use 5.006; |
|
4
|
|
|
|
|
12
|
|
4
|
4
|
|
|
4
|
|
17
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
67
|
|
5
|
4
|
|
|
4
|
|
15
|
use warnings; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
90
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
47
|
use Carp qw( croak ); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
214
|
|
8
|
|
|
|
|
|
|
|
9
|
4
|
|
|
4
|
|
2284
|
use Getopt::Long v2.36 qw(GetOptionsFromArray); |
|
4
|
|
|
|
|
36675
|
|
|
4
|
|
|
|
|
83
|
|
10
|
4
|
|
|
4
|
|
2307
|
use Pod::Usage; |
|
4
|
|
|
|
|
158708
|
|
|
4
|
|
|
|
|
502
|
|
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
|
|
|
4
|
|
2010
|
use Types::Standard qw( ArrayRef Bool ); |
|
4
|
|
|
|
|
267184
|
|
|
4
|
|
|
|
|
56
|
|
15
|
|
|
|
|
|
|
|
16
|
4
|
|
|
4
|
|
3683
|
use Moo::Role; |
|
4
|
|
|
|
|
13176
|
|
|
4
|
|
|
|
|
31
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my @options = ( 'help', 'man' ); |
21
|
|
|
|
|
|
|
do { |
22
|
|
|
|
|
|
|
has debug => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => Bool, |
25
|
|
|
|
|
|
|
default => 1, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has verbose => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
isa => Bool, |
31
|
|
|
|
|
|
|
default => 1, |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
push( @options, qw( debug! verbose! ) ); |
35
|
|
|
|
|
|
|
} unless $ENV{MRC_NO_STDOPTS}; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has argv => ( |
38
|
|
|
|
|
|
|
is => 'ro', |
39
|
|
|
|
|
|
|
isa => ArrayRef, |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub init { |
43
|
25
|
|
|
25
|
1
|
52382
|
my $class = shift; |
44
|
25
|
|
|
|
|
63
|
my %args = @_; |
45
|
|
|
|
|
|
|
|
46
|
25
|
50
|
|
|
|
73
|
my $argv = delete( $args{argv} ) or croak q{'argv' argument is required}; |
47
|
25
|
50
|
33
|
|
|
138
|
croak q{'argv' must be an array reference'} |
48
|
|
|
|
|
|
|
unless ref($argv) && ref($argv) eq 'ARRAY'; |
49
|
|
|
|
|
|
|
|
50
|
25
|
|
100
|
|
|
66
|
my $add_opts = delete( $args{add_opts} ) || []; |
51
|
25
|
50
|
33
|
|
|
82
|
croak q{'add_opts' must be an array reference'} |
52
|
|
|
|
|
|
|
unless ref($add_opts) && ref($add_opts) eq 'ARRAY'; |
53
|
|
|
|
|
|
|
|
54
|
25
|
50
|
|
|
|
63
|
croak q{unknown argument supplied for 'init'} if keys(%args); |
55
|
|
|
|
|
|
|
|
56
|
25
|
|
|
|
|
31
|
push( @options, $_ ) for ( @{$add_opts} ); |
|
25
|
|
|
|
|
48
|
|
57
|
|
|
|
|
|
|
|
58
|
25
|
|
|
|
|
38
|
my %values; |
59
|
25
|
100
|
|
|
|
68
|
GetOptionsFromArray( $argv, \%values, @options ) or _pod2usage(2); |
60
|
25
|
50
|
|
|
|
7319
|
_pod2usage(1) if $values{help}; |
61
|
25
|
50
|
|
|
|
46
|
_pod2usage( -exitstatus => 0, -verbose => 2 ) if $values{man}; |
62
|
|
|
|
|
|
|
|
63
|
25
|
|
|
|
|
41
|
$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
|
100
|
66
|
|
|
56
|
if defined( $values{debug} ) && !defined( $values{verbose} ); |
|
|
|
100
|
|
|
|
|
71
|
25
|
100
|
|
|
|
58
|
} unless $ENV{MRC_NO_STDOPTS}; |
72
|
|
|
|
|
|
|
|
73
|
25
|
|
|
|
|
31
|
my $app = eval { $class->new(%values); }; |
|
25
|
|
|
|
|
479
|
|
74
|
25
|
50
|
|
|
|
5526
|
do { |
75
|
0
|
|
|
|
|
0
|
print $@ . "\n"; |
76
|
0
|
|
|
|
|
0
|
_pod2usage(2); |
77
|
|
|
|
|
|
|
} if $@; |
78
|
|
|
|
|
|
|
|
79
|
25
|
|
|
|
|
124
|
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
|
|
|
|
|
|
|
sub _pod2usage { |
85
|
0
|
|
|
0
|
|
|
pod2usage(@_); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
1; # End of MooX::Role::CliOptions |
89
|
|
|
|
|
|
|
__END__ |