| 1 | package MooX::Options::Role; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 22 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | ## no critic (ProhibitExcessComplexity) | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 22 | our $VERSION = "4.102"; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | MooX::Options::Role - role that is apply to your object | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 USAGE | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Don't use MooX::Options::Role directly. It is used by L<MooX::Options> to upgrade your module. But it is useless alone. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 22 | ▶ | use Module::Runtime qw(use_module); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 22 | ▶ | use MooX::Options::Descriptive; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 22 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | ### PRIVATE | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub _option_name { | ||||||||||||||||||||||||||||
| 27 | 89 | ▶ | my ( $name, %data ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_option_name: called CC=7 SCAR=19.9
| |||||||||||||||||||||||||||||
| 28 | 89 | my $cmdline_name = join( '|', grep {defined} ( $name, $data{short} ) ); | |||||||||||||||||||||||||||
| 29 | ## no critic (RegularExpressions::RequireExtendedFormatting) | ||||||||||||||||||||||||||||
| 30 | 89 | ▶ | $cmdline_name =~ m/[^\w]$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | and croak | ||||||||||||||||||||||||||||
| 32 | "cmdline argument '$cmdline_name' should end with a word character"; | ||||||||||||||||||||||||||||
| 33 | 88 | ▶ | $cmdline_name .= '+' if $data{repeatable} && !defined $data{format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data{"repeatable"} and not defined $data{"format"}
MC/DC0%0/2
$data{"repeatable"} and not defined $data{"format"}
$data{"repeatable"}
not defined $data{"format"}
Truth table66%2/3
A and not(B)
A= $data{"repeatable"}B= not defined $data{"format"}
| |||||||||||||||||||||||||||||
| 34 | 88 | ▶ | $cmdline_name .= '!' if $data{negativable}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 88 | ▶ | $cmdline_name .= '!' if $data{negatable}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 88 | ▶ | $cmdline_name .= '=' . $data{format} if defined $data{format}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 88 | return $cmdline_name; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub _options_prepare_descriptive { | ||||||||||||||||||||||||||||
| 41 | 60 | ▶ | my ($options_data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_options_prepare_descriptive: called CC=16 SCAR=27.9
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 60 | my @options; | |||||||||||||||||||||||||||
| 44 | my %all_options; | ||||||||||||||||||||||||||||
| 45 | 60 | my %has_to_split; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 60 | my $data_record_loaded = 0; | |||||||||||||||||||||||||||
| 48 | 60 | for my $name ( | |||||||||||||||||||||||||||
| 49 | sort { | ||||||||||||||||||||||||||||
| 50 | $options_data->{$a}{order} | ||||||||||||||||||||||||||||
| 51 | <=> $options_data->{$b}{order} # sort by order | ||||||||||||||||||||||||||||
| 52 | 49 | ▶ | or $a cmp $b # sort by attr name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } keys %$options_data | ||||||||||||||||||||||||||||
| 54 | ) | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 89 | my %data = %{ $options_data->{$name} }; | |||||||||||||||||||||||||||
| 57 | 89 | my $doc = $data{doc}; | |||||||||||||||||||||||||||
| 58 | 89 | ▶ | $doc = "no doc for $name" if !defined $doc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 89 | my $option = {}; | |||||||||||||||||||||||||||
| 60 | 89 | ▶ | $option->{hidden} = 1 if $data{hidden}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 89 | ▶ | push @options, [] if $data{spacer_before}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 89 | push @options, [ _option_name( $name, %data ), $doc, $option ]; | |||||||||||||||||||||||||||
| 64 | 88 | ▶ | push @options, [] if $data{spacer_after}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 88 | push @{ $all_options{$name} }, $name; | |||||||||||||||||||||||||||
| 67 | 88 | ▶ | if ( $data{short} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 1 | my @shrt_list = split( "|", $data{short} ); | |||||||||||||||||||||||||||
| 69 | 1 | foreach my $shrt (@shrt_list) { | |||||||||||||||||||||||||||
| 70 | croak | ||||||||||||||||||||||||||||
| 71 | "There is already an option '$shrt' - can't use it to shorten '$name'" | ||||||||||||||||||||||||||||
| 72 | 1 | ▶ | if exists $options_data->{$shrt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | croak | ||||||||||||||||||||||||||||
| 74 | "There is already an abbreviation '$shrt' - can't use it to shorten '$name'" | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if defined $all_options{$shrt}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | push @{ $all_options{$shrt} }, $name; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 87 | ▶ | if ( defined $data{autosplit} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | if ( !$data_record_loaded ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | use_module("Data::Record"); | |||||||||||||||||||||||||||
| 83 | 2 | use_module("Regexp::Common"); | |||||||||||||||||||||||||||
| 84 | 2 | Regexp::Common->import; | |||||||||||||||||||||||||||
| 85 | 2 | $data_record_loaded = 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | $has_to_split{$name} = Data::Record->new( | ||||||||||||||||||||||||||||
| 88 | { split => $data{autosplit}, | ||||||||||||||||||||||||||||
| 89 | unless => $Regexp::Common::RE{quoted} | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 3 | ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # singleton algorithm taken from List::MoreUtils | ||||||||||||||||||||||||||||
| 96 | 58 | my $k; | |||||||||||||||||||||||||||
| 97 | my %abbrev_dd; | ||||||||||||||||||||||||||||
| 98 | ## no critic (BuiltinFunctions::ProhibitComplexMappings) | ||||||||||||||||||||||||||||
| 99 | 58 | foreach my $combo ( | |||||||||||||||||||||||||||
| 100 | 341 | grep { 1 == $abbrev_dd{ $k = $_->[1] } } | |||||||||||||||||||||||||||
| 101 | 378 | grep { not $abbrev_dd{ $k = $_->[1] }++ } | |||||||||||||||||||||||||||
| 102 | map { | ||||||||||||||||||||||||||||
| 103 | 86 | my $fa = $_; | |||||||||||||||||||||||||||
| 104 | 86 | map { [ $fa => substr $fa, 0, $_ ] } 1 .. length($fa) | |||||||||||||||||||||||||||
| 105 | } keys %all_options | ||||||||||||||||||||||||||||
| 106 | ) | ||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 305 | my ( $name, $long_short ) = @{$combo}; | |||||||||||||||||||||||||||
| 109 | 305 | ▶ | $all_options{$name}->[0] eq $name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | or next; # don't generate abbreviations for short | ||||||||||||||||||||||||||||
| 111 | 305 | ▶ | defined $all_options{$long_short} and next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 222 | push @{ $all_options{$long_short} }, $name; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 58 | return \@options, \%has_to_split, \%all_options; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub _options_fix_argv { | ||||||||||||||||||||||||||||
| 119 | 58 | ▶ | my ( $option_data, $has_to_split, $all_options ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_options_fix_argv: called CC=17 SCAR=30.3
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 58 | my @new_argv; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #parse all argv | ||||||||||||||||||||||||||||
| 124 | 58 | ▶ | while ( defined( my $arg = shift @ARGV ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 34 | ▶ | if ( $arg eq '--' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | push @new_argv, $arg, @ARGV; | |||||||||||||||||||||||||||
| 127 | 0 | last; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 34 | ▶ | if ( index( $arg, '-' ) != 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 3 | push @new_argv, $arg; | |||||||||||||||||||||||||||
| 131 | 3 | next; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 31 | my ( $arg_name_with_dash, $arg_values ) = split( /=/x, $arg, 2 ); | |||||||||||||||||||||||||||
| 135 | 31 | ▶ | if ( index( $arg_name_with_dash, '--' ) < 0 && !defined $arg_values ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
index($arg_name_with_dash, "--") == -1 and not defined $arg_values
MC/DC0%0/2
index($arg_name_with_dash, "--") == -1 and not defined $arg_values
index($arg_name_with_dash, "--") == -1
not defined $arg_values
Truth table33%1/3
A and not(B)
A= index($arg_name_with_dash, "--") == -1B= not defined $arg_values
| |||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | $arg_values | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | = length($arg_name_with_dash) > 2 | ||||||||||||||||||||||||||||
| 139 | ? substr( $arg_name_with_dash, 2 ) | ||||||||||||||||||||||||||||
| 140 | : undef; | ||||||||||||||||||||||||||||
| 141 | 0 | $arg_name_with_dash = substr( $arg_name_with_dash, 0, 2 ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 31 | ▶ | unshift @ARGV, $arg_values if defined $arg_values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 31 | my ( $dash, $negative, $arg_name_without_dash ) | |||||||||||||||||||||||||||
| 146 | = $arg_name_with_dash =~ /^(\-+)(no\-)?(.*)$/x; | ||||||||||||||||||||||||||||
| 147 | 31 | $arg_name_without_dash =~ s/\-/_/gx; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 31 | my $original_long_option = $all_options->{$arg_name_without_dash}; | |||||||||||||||||||||||||||
| 150 | 31 | ▶ | if ( defined $original_long_option ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | ## no critic (ErrorHandling::RequireCarping) | ||||||||||||||||||||||||||||
| 152 | # uncoverable branch false | ||||||||||||||||||||||||||||
| 153 | 25 | ▶ | @$original_long_option == 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | or die | ||||||||||||||||||||||||||||
| 155 | "Internal error, duplicate map for abbreviation detected for '$arg_name_without_dash'!"; | ||||||||||||||||||||||||||||
| 156 | 25 | $original_long_option = $original_long_option->[0]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 31 | my $arg_name = $dash; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 31 | ▶ | if ( defined $negative && defined $original_long_option ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $negative and defined $original_long_option
MC/DC50%1/2
defined $negative and defined $original_long_option
defined $negative
defined $original_long_option
Truth table66%2/3
A and B
A= defined $negativeB= defined $original_long_option
| |||||||||||||||||||||||||||||
| 162 | $arg_name .= | ||||||||||||||||||||||||||||
| 163 | $option_data->{$original_long_option}{negatable} | ||||||||||||||||||||||||||||
| 164 | 1 | ▶ | ? 'no-' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | : 'no_'; | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 31 | $arg_name .= $arg_name_without_dash; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 31 | ▶ | if ( defined $original_long_option | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $original_long_option and defined(my $arg_value = shift @ARGV)
MC/DC100%2/2
defined $original_long_option and defined(my $arg_value = shift @ARGV)
defined $original_long_option
defined(my $arg_value = shift @ARGV)
Truth table100%3/3
A and B
A= defined $original_long_optionB= defined(my $arg_value = shift @ARGV)
| |||||||||||||||||||||||||||||
| 171 | && ( defined( my $arg_value = shift @ARGV ) ) ) | ||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 20 | my $autorange = $option_data->{$original_long_option}{autorange}; | |||||||||||||||||||||||||||
| 174 | my $argv_processor = sub { | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | #remove the quoted if exist to chain | ||||||||||||||||||||||||||||
| 177 | 22 | ▶ | $_[0] =~ s/^['"]|['"]$//gx; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||
| 178 | 22 | ▶ | if ($autorange) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | push @new_argv, | ||||||||||||||||||||||||||||
| 180 | 0 | map { $arg_name => $_ } _expand_autorange( $_[0] ); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | else { | ||||||||||||||||||||||||||||
| 183 | 22 | push @new_argv, $arg_name, $_[0]; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 20 | }; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 20 | ▶ | if ( my $rec = $has_to_split->{$original_long_option} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 2 | foreach my $record ( $rec->records($arg_value) ) { | |||||||||||||||||||||||||||
| 190 | 4 | $argv_processor->($record); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | else { | ||||||||||||||||||||||||||||
| 194 | 18 | $argv_processor->($arg_value); | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | else { | ||||||||||||||||||||||||||||
| 198 | 11 | push @new_argv, $arg_name; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 58 | return @new_argv; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | sub _expand_autorange { | ||||||||||||||||||||||||||||
| 206 | 0 | ▶ | my ($arg_value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_expand_autorange: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | my @expanded_arg_value; | |||||||||||||||||||||||||||
| 209 | 0 | my ( $left_figure, $autorange_found, $right_figure ) | |||||||||||||||||||||||||||
| 210 | = $arg_value =~ /^(\d*)(\.\.)(\d*)$/x; | ||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if ($autorange_found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | $left_figure = $right_figure unless length($left_figure); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | $right_figure = $left_figure unless length($right_figure); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | if ( length $left_figure && length $right_figure ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $left_figure and length $right_figure
MC/DC0%0/2
length $left_figure and length $right_figure
length $left_figure
length $right_figure
Truth table0%0/3
A and B
A= length $left_figureB= length $right_figure
| |||||||||||||||||||||||||||||
| 215 | 0 | push @expanded_arg_value, $left_figure .. $right_figure; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 0 | ▶ | return @expanded_arg_value ? @expanded_arg_value : $arg_value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | ### PRIVATE | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 22 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 224 | 22 | with "MooX::Locale::Passthrough"; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 22 | requires qw/_options_data _options_config/; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head1 METHODS | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | These methods will be composed into your class | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head2 new_with_options | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Same as new but parse ARGV with L<Getopt::Long::Descriptive> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Check full doc L<MooX::Options> for more details. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =cut | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | sub new_with_options { | ||||||||||||||||||||||||||||
| 241 | 43 | ▶ | my ( $class, %params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_with_options: called CC=12 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | #save subcommand | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 43 | ▶ | if ( ref( my $command_chain = $params{command_chain} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | $class->can('around')->( | ||||||||||||||||||||||||||||
| 247 | _options_prog_name => sub { | ||||||||||||||||||||||||||||
| 248 | 0 | ▶ | my $prog_name = Getopt::Long::Descriptive::prog_name; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 249 | 0 | for my $cmd (@$command_chain) { | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | next if !blessed $cmd || !$cmd->can('command_name'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($cmd) and $cmd->can("command_name")
MC/DC0%0/2
builtin::blessed($cmd) and $cmd->can("command_name")
builtin::blessed($cmd)
$cmd->can("command_name")
Truth table0%0/3
A and B
A= builtin::blessed($cmd)B= $cmd->can("command_name")
| |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ( defined( my $cmd_name = $cmd->command_name ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | 0 | $prog_name .= ' ' . $cmd_name; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | return $prog_name; | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | 0 | ); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 43 | ▶ | if ( ref( my $command_commands = $params{command_commands} ) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | $class->can('around')->( | ||||||||||||||||||||||||||||
| 263 | _options_sub_commands => sub { | ||||||||||||||||||||||||||||
| 264 | return [ | ||||||||||||||||||||||||||||
| 265 | ## no critic (BuiltinFunctions::RequireBlockMap) | ||||||||||||||||||||||||||||
| 266 | map +{ | ||||||||||||||||||||||||||||
| 267 | name => $_, | ||||||||||||||||||||||||||||
| 268 | 0 | ▶ | command => $command_commands->{$_}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 269 | }, | ||||||||||||||||||||||||||||
| 270 | sort keys %$command_commands | ||||||||||||||||||||||||||||
| 271 | ]; | ||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 0 | ); | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 43 | my %cmdline_params = $class->parse_options(%params); | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 36 | ▶ | if ( $cmdline_params{h} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 279 | 1 | return $class->options_usage( $params{h}, $cmdline_params{h} ); | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | 35 | ▶ | if ( $cmdline_params{help} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 2 | return $class->options_help( $params{help}, $cmdline_params{help} ); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 33 | ▶ | if ( $cmdline_params{man} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | return $class->options_man( $cmdline_params{man} ); | |||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | 33 | ▶ | if ( $cmdline_params{usage} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | return $class->options_short_usage( $params{usage}, | ||||||||||||||||||||||||||||
| 289 | 0 | $cmdline_params{usage} ); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 33 | my $self; | |||||||||||||||||||||||||||
| 293 | return $self | ||||||||||||||||||||||||||||
| 294 | 33 | ▶ | if eval { $self = $class->new(%cmdline_params); 1 }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 1 | ▶ | if ( $@ =~ /^Attribute\s\((.*?)\)\sis\srequired/x ) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 296 | 0 | print STDERR "$1 is missing\n"; | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | elsif ( $@ =~ /^Missing\srequired\sarguments:\s(.*)\sat\s/x ) { | ||||||||||||||||||||||||||||
| 299 | 0 | my @missing_required = split /,\s/x, $1; | |||||||||||||||||||||||||||
| 300 | print STDERR | ||||||||||||||||||||||||||||
| 301 | join( "\n", | ||||||||||||||||||||||||||||
| 302 | 0 | ( map { $_ . " is missing" } @missing_required ), '' ); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | elsif ( $@ =~ /^(.*?)\srequired/x ) { | ||||||||||||||||||||||||||||
| 305 | 0 | print STDERR "$1 is missing\n"; | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | elsif ( $@ =~ /^isa\scheck.*?failed:\s/x ) { | ||||||||||||||||||||||||||||
| 308 | 1 | print STDERR substr( $@, index( $@, ':' ) + 2 ); | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | else { | ||||||||||||||||||||||||||||
| 311 | 0 | print STDERR $@; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | 1 | %cmdline_params = $class->parse_options( h => 1 ); | |||||||||||||||||||||||||||
| 314 | 1 | return $class->options_usage( 1, $cmdline_params{h} ); | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =head2 parse_options | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | Parse your options, call L<Getopt::Long::Descriptive> and convert the result for the "new" method. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | It is use by "new_with_options". | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 22 | my $decode_json; | |||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | sub parse_options { | ||||||||||||||||||||||||||||
| 328 | 60 | ▶ | my ( $class, %params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_options: called CC=23 SCAR=37.8
Pod
documented
| |||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 60 | my %options_data = $class->_options_data; | |||||||||||||||||||||||||||
| 331 | 60 | my %options_config = $class->_options_config; | |||||||||||||||||||||||||||
| 332 | 60 | ▶ | if ( defined $options_config{skip_options} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 57 | delete @options_data{ @{ $options_config{skip_options} } }; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 60 | my ( $options, $has_to_split, $all_options ) | |||||||||||||||||||||||||||
| 337 | = _options_prepare_descriptive( \%options_data ); | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 58 | ▶ | local @ARGV = @ARGV if $options_config{protect_argv}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | 58 | @ARGV = _options_fix_argv( \%options_data, $has_to_split, $all_options ); | |||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 58 | my @flavour; | |||||||||||||||||||||||||||
| 343 | 58 | ▶ | if ( defined $options_config{flavour} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 344 | 55 | push @flavour, { getopt_conf => $options_config{flavour} }; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 58 | my $prog_name = $class->_options_prog_name(); | |||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # create usage str | ||||||||||||||||||||||||||||
| 350 | 58 | my $usage_str = $options_config{usage_string}; | |||||||||||||||||||||||||||
| 351 | 58 | ▶ | $usage_str = sprintf( $class->__("USAGE: %s %s"), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | $prog_name, " [-h] [" . $class->__("long options ...") . "]" ) | ||||||||||||||||||||||||||||
| 353 | if !defined $usage_str; | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 58 | my ( $opt, $usage ) = describe_options( | |||||||||||||||||||||||||||
| 356 | ($usage_str), | ||||||||||||||||||||||||||||
| 357 | @$options, | ||||||||||||||||||||||||||||
| 358 | [], | ||||||||||||||||||||||||||||
| 359 | [ 'usage', $class->__("show a short help message") ], | ||||||||||||||||||||||||||||
| 360 | [ 'h', $class->__("show a compact help message") ], | ||||||||||||||||||||||||||||
| 361 | [ 'help', $class->__("show a long help message") ], | ||||||||||||||||||||||||||||
| 362 | [ 'man', $class->__("show the manual") ], | ||||||||||||||||||||||||||||
| 363 | , | ||||||||||||||||||||||||||||
| 364 | @flavour | ||||||||||||||||||||||||||||
| 365 | ); | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 58 | $usage->{prog_name} = $prog_name; | |||||||||||||||||||||||||||
| 368 | 58 | $usage->{target} = $class; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 58 | ▶ | if ( $usage->{should_die} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 5 | return $class->options_usage( 1, $usage ); | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | 53 | my %cmdline_params = %params; | |||||||||||||||||||||||||||
| 375 | 53 | for my $name ( keys %options_data ) { | |||||||||||||||||||||||||||
| 376 | 81 | my %data = %{ $options_data{$name} }; | |||||||||||||||||||||||||||
| 377 | 81 | ▶ | if ( !defined $cmdline_params{$name} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $cmdline_params{$name} or $options_config{"prefer_commandline"}
MC/DC0%0/2
not defined $cmdline_params{$name} or $options_config{"prefer_commandline"}
not defined $cmdline_params{$name}
$options_config{"prefer_commandline"}
Truth table33%1/3
not(A) or B
A= not defined $cmdline_params{$name}B= $options_config{"prefer_commandline"}
| |||||||||||||||||||||||||||||
| 378 | || $options_config{prefer_commandline} ) | ||||||||||||||||||||||||||||
| 379 | { | ||||||||||||||||||||||||||||
| 380 | 81 | my $val = $opt->$name(); | |||||||||||||||||||||||||||
| 381 | 81 | ▶ | if ( defined $val ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 22 | ▶ | if ( $data{json} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | defined $decode_json | ||||||||||||||||||||||||||||
| 384 | 0 | ▶ | or $decode_json = eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | 0 | use_module("JSON::MaybeXS"); | |||||||||||||||||||||||||||
| 386 | 0 | JSON::MaybeXS->can("decode_json"); | |||||||||||||||||||||||||||
| 387 | }; | ||||||||||||||||||||||||||||
| 388 | defined $decode_json | ||||||||||||||||||||||||||||
| 389 | 0 | ▶ | or $decode_json = eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 0 | use_module("JSON::PP"); | |||||||||||||||||||||||||||
| 391 | 0 | JSON::PP->can("decode_json"); | |||||||||||||||||||||||||||
| 392 | }; | ||||||||||||||||||||||||||||
| 393 | ## no critic (ErrorHandling::RequireCarping) | ||||||||||||||||||||||||||||
| 394 | 0 | ▶ | $@ and die $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 0 | ▶ | if (!eval { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | 0 | $cmdline_params{$name} = $decode_json->($val); | |||||||||||||||||||||||||||
| 397 | 0 | 1; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | ) | ||||||||||||||||||||||||||||
| 400 | { | ||||||||||||||||||||||||||||
| 401 | 0 | print STDERR $@; | |||||||||||||||||||||||||||
| 402 | 0 | return $class->options_usage( 1, $usage ); | |||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | else { | ||||||||||||||||||||||||||||
| 406 | 22 | $cmdline_params{$name} = $val; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 53 | ▶ | if ( $opt->h() || defined $params{h} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opt->h or defined $params{"h"}
MC/DC50%1/2
$opt->h or defined $params{"h"}
$opt->h
defined $params{"h"}
Truth table66%2/3
A or B
A= $opt->hB= defined $params{"h"}
| |||||||||||||||||||||||||||||
| 413 | 2 | $cmdline_params{h} = $usage; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 53 | ▶ | if ( $opt->help() || defined $params{help} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opt->help or defined $params{"help"}
MC/DC100%2/2
$opt->help or defined $params{"help"}
$opt->help
defined $params{"help"}
Truth table100%3/3
A or B
A= $opt->helpB= defined $params{"help"}
| |||||||||||||||||||||||||||||
| 417 | 18 | $cmdline_params{help} = $usage; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 53 | ▶ | if ( $opt->man() || defined $params{man} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opt->man or defined $params{"man"}
MC/DC0%0/2
$opt->man or defined $params{"man"}
$opt->man
defined $params{"man"}
Truth table33%1/3
A or B
A= $opt->manB= defined $params{"man"}
| |||||||||||||||||||||||||||||
| 421 | 0 | $cmdline_params{man} = $usage; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 53 | ▶ | if ( $opt->usage() || defined $params{usage} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opt->usage or defined $params{"usage"}
MC/DC0%0/2
$opt->usage or defined $params{"usage"}
$opt->usage
defined $params{"usage"}
Truth table33%1/3
A or B
A= $opt->usageB= defined $params{"usage"}
| |||||||||||||||||||||||||||||
| 425 | 0 | $cmdline_params{usage} = $usage; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 53 | return %cmdline_params; | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =head2 options_usage | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | Display help message. | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Check full doc L<MooX::Options> for more details. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =cut | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | sub options_usage { | ||||||||||||||||||||||||||||
| 440 | 18 | ▶ | my ( $class, $code, @messages ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
options_usage: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 441 | 18 | my $usage; | |||||||||||||||||||||||||||
| 442 | 18 | ▶ | if ( @messages | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@messages and ref $messages[-1] eq "MooX::Options::Descriptive::Usage"
MC/DC50%1/2
@messages and ref $messages[-1] eq "MooX::Options::Descriptive::Usage"
@messages
ref $messages[-1] eq "MooX::Options::Descriptive::Usage"
Truth table66%2/3
A and B
A= @messagesB= ref $messages[-1] eq "MooX::Options::Descriptive::Usage"
| |||||||||||||||||||||||||||||
| 443 | && ref $messages[-1] eq 'MooX::Options::Descriptive::Usage' ) | ||||||||||||||||||||||||||||
| 444 | { | ||||||||||||||||||||||||||||
| 445 | 7 | $usage = shift @messages; | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | 18 | ▶ | $code = 0 if !defined $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 18 | ▶ | if ( !$usage ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | 11 | local @ARGV = (); | |||||||||||||||||||||||||||
| 450 | 11 | my %cmdline_params = $class->parse_options( help => $code ); | |||||||||||||||||||||||||||
| 451 | 11 | $usage = $cmdline_params{help}; | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | 18 | my $message = ""; | |||||||||||||||||||||||||||
| 454 | 18 | ▶ | $message .= join( "\n", @messages, '' ) if @messages; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | 18 | $message .= $usage . "\n"; | |||||||||||||||||||||||||||
| 456 | 18 | ▶ | if ( $code > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 457 | 6 | CORE::warn $message; | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | else { | ||||||||||||||||||||||||||||
| 460 | 12 | print $message; | |||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||
| 462 | 18 | ▶ | exit($code) if $code >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 463 | 1 | return; | |||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =head2 options_help | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | Display long usage message | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =cut | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | sub options_help { | ||||||||||||||||||||||||||||
| 473 | 7 | ▶ | my ( $class, $code, $usage ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
options_help: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 474 | 7 | ▶ | $code = 0 if !defined $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 7 | ▶ | if ( !defined $usage || !ref $usage ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $usage and ref $usage
MC/DC50%1/2
defined $usage and ref $usage
defined $usage
ref $usage
Truth table66%2/3
A and B
A= defined $usageB= ref $usage
| |||||||||||||||||||||||||||||
| 477 | 5 | local @ARGV = (); | |||||||||||||||||||||||||||
| 478 | 5 | my %cmdline_params = $class->parse_options( help => $code ); | |||||||||||||||||||||||||||
| 479 | 5 | $usage = $cmdline_params{help}; | |||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | 7 | my $message = $usage->option_help . "\n"; | |||||||||||||||||||||||||||
| 482 | 7 | ▶ | if ( $code > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 483 | 0 | CORE::warn $message; | |||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||
| 485 | else { | ||||||||||||||||||||||||||||
| 486 | 7 | print $message; | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | 7 | ▶ | exit($code) if $code >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | 1 | return; | |||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =head2 options_short_usage | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | Display quick usage message, with only the list of options | ||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | =cut | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | sub options_short_usage { | ||||||||||||||||||||||||||||
| 499 | 0 | ▶ | my ( $class, $code, $usage ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
options_short_usage: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 500 | 0 | ▶ | $code = 0 if !defined $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 0 | ▶ | if ( !defined $usage || !ref $usage ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $usage and ref $usage
MC/DC0%0/2
defined $usage and ref $usage
defined $usage
ref $usage
Truth table0%0/3
A and B
A= defined $usageB= ref $usage
| |||||||||||||||||||||||||||||
| 503 | 0 | local @ARGV = (); | |||||||||||||||||||||||||||
| 504 | 0 | my %cmdline_params = $class->parse_options( help => $code ); | |||||||||||||||||||||||||||
| 505 | 0 | $usage = $cmdline_params{help}; | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | 0 | my $message = "USAGE: " . $usage->option_short_usage . "\n"; | |||||||||||||||||||||||||||
| 508 | 0 | ▶ | if ( $code > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 509 | 0 | CORE::warn $message; | |||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | else { | ||||||||||||||||||||||||||||
| 512 | 0 | print $message; | |||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||
| 514 | 0 | ▶ | exit($code) if $code >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 515 | 0 | return; | |||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =head2 options_man | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | Display a pod like a manual | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | =cut | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | sub options_man { | ||||||||||||||||||||||||||||
| 525 | 0 | ▶ | my ( $class, $usage, $output ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
options_man: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 526 | 0 | local @ARGV = (); | |||||||||||||||||||||||||||
| 527 | 0 | ▶ | if ( !$usage ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 528 | 0 | local @ARGV = (); | |||||||||||||||||||||||||||
| 529 | 0 | my %cmdline_params = $class->parse_options( man => 1 ); | |||||||||||||||||||||||||||
| 530 | 0 | $usage = $cmdline_params{man}; | |||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 0 | use_module( "Path::Class", "0.32" ); | |||||||||||||||||||||||||||
| 534 | 0 | my $man_file | |||||||||||||||||||||||||||
| 535 | = Path::Class::file( Path::Class::tempdir( CLEANUP => 1 ), | ||||||||||||||||||||||||||||
| 536 | 'help.pod' ); | ||||||||||||||||||||||||||||
| 537 | 0 | $man_file->spew( iomode => '>:encoding(UTF-8)', $usage->option_pod ); | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 0 | use_module("Pod::Usage"); | |||||||||||||||||||||||||||
| 540 | 0 | Pod::Usage::pod2usage( | |||||||||||||||||||||||||||
| 541 | -verbose => 2, | ||||||||||||||||||||||||||||
| 542 | -input => $man_file->stringify, | ||||||||||||||||||||||||||||
| 543 | -exitval => 'NOEXIT', | ||||||||||||||||||||||||||||
| 544 | -output => $output | ||||||||||||||||||||||||||||
| 545 | ); | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 0 | exit(0); | |||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | ### PRIVATE NEED TO BE EXPORTED | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | sub _options_prog_name { | ||||||||||||||||||||||||||||
| 553 | 58 | ▶ | return Getopt::Long::Descriptive::prog_name; | ||||||||||||||||||||||||||
|
Subroutine
_options_prog_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | sub _options_sub_commands { | ||||||||||||||||||||||||||||
| 557 | 45 | ▶ | return; | ||||||||||||||||||||||||||
|
Subroutine
_options_sub_commands: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | ### PRIVATE NEED TO BE EXPORTED | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | perldoc MooX::ConfigFromFile | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | You can also look for information at: | ||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | =over 4 | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooX-ConfigFromFile> | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | L<http://annocpan.org/dist/MooX-ConfigFromFile> | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | L<http://cpanratings.perl.org/d/MooX-ConfigFromFile> | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | L<http://search.cpan.org/dist/MooX-ConfigFromFile/> | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | =back | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | celogeek <me@celogeek.com> | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | This software is copyright (c) 2013 by celogeek <me@celogeek.com>. | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | This software is copyright (c) 2017 by Jens Rehsack. | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | =cut | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 22 | 1; | |||||||||||||||||||||||||||