| 1 | |||||||||||||||||||||||||||||||||||||||||||
| 2 | package MooseX::Getopt::Usage; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 44 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 44 | our $VERSION = '0.24'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 44 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 44 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 44 | ▶ | use MooseX::Getopt::Usage::Formatter; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 44 | with 'MooseX::Getopt::Basic'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # As we don't use GLD insert our own help_flag. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 44 | has help_flag => ( | |||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 16 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | traits => ['Getopt'], | ||||||||||||||||||||||||||||||||||||||||||
| 18 | cmd_flag => 'help', | ||||||||||||||||||||||||||||||||||||||||||
| 19 | cmd_aliases => [qw/? usage/], | ||||||||||||||||||||||||||||||||||||||||||
| 20 | documentation => "Display the usage message and exit" | ||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # Promote warnings to errors to capture invalid and missing options errors from | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # Getopt::Long::GetOptions. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | around _getopt_spec_warnings => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | shift; my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | die @_; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 44 | }; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 50 | ▶ | sub getopt_usage_config { () } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getopt_usage_config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 40 | ▶ | sub getopt_usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getopt_usage: called CC=5 SCAR=17.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 40 | my $proto = shift; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 40 | ▶ | my $class = ref $proto || $proto; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 40 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 40 | my $conf = { $class->getopt_usage_config, %args }; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 40 | ▶ | if ( ! exists $conf->{colours} && exists $conf->{colors} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not exists $conf->{"colours"} and exists $conf->{"colors"}
MC/DC0%0/2
not exists $conf->{"colours"} and exists $conf->{"colors"}
not exists $conf->{"colours"}
exists $conf->{"colors"}
Truth table33%1/3
not(A) and B
A= not exists $conf->{"colours"}B= exists $conf->{"colors"}
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | $conf->{colours} = delete $conf->{colors} | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 40 | 40 | $conf->{getopt_class} = $class; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 40 | my $fmtr = MooseX::Getopt::Usage::Formatter->new($conf); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 40 | ▶ | return $args{man} ? $fmtr->manpage(%args) : $fmtr->usage(%args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # Replace new_with_options. The way it decides if usage is needed does not fit | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # our needs as we don't supply a usage object. So we do it here. We also want | ||||||||||||||||||||||||||||||||||||||||||
| 47 | # access to the $pa object returned from process_argv. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 40 | ▶ | around new_with_options => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 40 | my $orig = shift; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 40 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 40 | my @params = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 40 | my $self; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 40 | my $conf = { $class->getopt_usage_config }; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 40 | ▶ | $conf->{auto_man} = 1 if not exists $conf->{auto_man}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 40 | ▶ | $conf->{auto_help} = 1 if not exists $conf->{auto_help}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 40 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Get in early on the arg passing to look for help or man options. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # This makes sure they still work even when required options are missing, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # (which would fail construction and stop us seeing the man option). | ||||||||||||||||||||||||||||||||||||||||||
| 62 | # See github issue #4 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 40 | my $pa = $class->process_argv(@params); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 36 | my $cli_params = $pa->cli_params; | |||||||||||||||||||||||||||||||||||||||||
| 65 | $class->getopt_usage( man => 1) | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 36 | ▶ | if $conf->{auto_man} and $class->can('man') and $cli_params->{man}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$conf->{"auto_man"} and $class->can("man") and $cli_params->{"man"}
$conf->{"auto_man"} and $class->can("man")
MC/DC100%3/3
$conf->{"auto_man"} and $class->can("man") and $cli_params->{"man"}
$conf->{"auto_man"}
$class->can("man")
$cli_params->{"man"}
Truth table100%4/4
A and B and C
A= $conf->{"auto_man"}B= $class->can("man")C= $cli_params->{"man"}
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | $class->getopt_usage( exit => 0 ) | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 22 | ▶ | if $conf->{auto_help} and $cli_params->{help_flag}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$conf->{"auto_help"} and $cli_params->{"help_flag"}
MC/DC100%2/2
$conf->{"auto_help"} and $cli_params->{"help_flag"}
$conf->{"auto_help"}
$cli_params->{"help_flag"}
Truth table100%3/3
A and B
A= $conf->{"auto_help"}B= $cli_params->{"help_flag"}
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # Construct the object in the same way as our super new_with_options | ||||||||||||||||||||||||||||||||||||||||||
| 71 | $self = $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 72 | ARGV => $pa->argv_copy, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | extra_argv => $pa->extra_argv, | ||||||||||||||||||||||||||||||||||||||||||
| 74 | ( $pa->usage ? ( usage => $pa->usage ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | %{ $pa->constructor_params }, # explicit params to ->new | |||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | ▶ | %{ $pa->cli_params }, # params from CLI | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 6 | ▶ | catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | /Attribute \((\w+)\) does not pass the type constraint because: (.*?) at/ | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | $class->getopt_usage( exit => 1, err => "Invalid '$1' : $2" ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | elsif (/Attribute \((\w+)\) is required /) { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | $class->getopt_usage( exit => 2, err => "Required option missing: $1" ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | elsif (/^Unknown option:|^Value .*? for option |Option .* does not take an argument/) { | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # Getopt::Long warnings we promoted in _getopt_spec_warnings | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | s/\n+$//; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | $class->getopt_usage( exit => 3, err => $_ ); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | die $_; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 40 | }; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 44 | }; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 44 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 44 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||||||||||||||||