| 1 | package CLI::Osprey::Role; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use CLI::Osprey::Descriptive; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # ABSTRACT: Role for CLI::Osprey applications | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '0.08'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | sub _osprey_option_to_getopt { | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | my ($name, %attributes) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_osprey_option_to_getopt: called CC=7 SCAR=22.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | my $getopt = join('|', grep defined, ($name, $attributes{short})); | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | $getopt .= '+' if $attributes{repeatable} && !defined $attributes{format}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$attributes{"repeatable"} and not defined $attributes{"format"}
MC/DC0%0/2
$attributes{"repeatable"} and not defined $attributes{"format"}
$attributes{"repeatable"}
not defined $attributes{"format"}
Truth table33%1/3
A and not(B)
A= $attributes{"repeatable"}B= not defined $attributes{"format"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | $getopt .= '!' if $attributes{negatable}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | $getopt .= '=' . $attributes{format} if defined $attributes{format}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | ▶ | $getopt .= '@' if $attributes{repeatable} && defined $attributes{format}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$attributes{"repeatable"} and defined $attributes{"format"}
MC/DC0%0/2
$attributes{"repeatable"} and defined $attributes{"format"}
$attributes{"repeatable"}
defined $attributes{"format"}
Truth table33%1/3
A and B
A= $attributes{"repeatable"}B= defined $attributes{"format"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 7 | return $getopt; | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | sub _osprey_prepare_options { | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 16 | ▶ | my ($options, $config) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_osprey_prepare_options: called CC=17 SCAR=34.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 16 | my @getopt; | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | my %abbreviations; | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 16 | my %fullnames; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | my @order = sort { | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 16 | ($options->{$a}{order} || 9999) <=> ($options->{$b}{order} || 9999) | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | || ($config->{added_order} ? ($options->{$a}{added_order} <=> $options->{$b}{added_order}) : 0) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/7
($options->{$a}{"order"} || 9999) <=> ($options->{$b}{"order"} || 9999) or $config->{"added_order"} ? $options->{$a}{"added_order"} <=> $options->{$b}{"added_order"} : 0
$options->{$a}{'order'} || 9999
$options->{$b}{'order'} || 9999
MC/DC0%0/2
($options->{$a}{"order"} || 9999) <=> ($options->{$b}{"order"} || 9999) or $config->{"added_order"} ? $options->{$a}{"added_order"} <=> $options->{$b}{"added_order"} : 0
$options->{$a}{'order'}
$config->{"added_order"} ? $options->{$a}{"added_order"} <=> $options->{$b}{"added_order"} : 0
MC/DC0%0/1
$options->{$b}{'order'} || 9999
$options->{$b}{'order'}
Truth table0%0/3
A or B
A= $options->{$a}{'order'}B= $config->{"added_order"} ? $options->{$a}{"added_order"} <=> $options->{$b}{"added_order"} : 0
Truth table0%0/2
A
A= $options->{$b}{'order'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | || $a cmp $b | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } keys %$options; | ||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 16 | for my $option (@order) { | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | my %attributes = %{ $options->{$option} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | push @{ $fullnames{ $attributes{option} } }, $option; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 16 | for my $name (keys %fullnames) { | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 7 | ▶ | if (@{ $fullnames{$name} } > 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | croak "Multiple option attributes named $name: [@{ $fullnames{$name} }]"; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 16 | for my $option (@order) { | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | my %attributes = %{ $options->{$option} }; | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | my $name = $attributes{option}; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | 7 | my $doc = $attributes{doc}; | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 7 | ▶ | $doc = "no documentation for $name" unless defined $doc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 7 | ▶ | push @getopt, [] if $attributes{spacer_before}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 7 | ▶ | push @getopt, [ _osprey_option_to_getopt($option, %attributes), $doc, ($attributes{hidden} ? { hidden => 1} : ()) ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 7 | ▶ | push @getopt, [] if $attributes{spacer_after}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | push @{ $abbreviations{$name} }, $option; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # If we allow abbreviating long option names, an option can be called by any prefix of its name, | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # unless that prefix is an option name itself. Ambiguous cases (an abbreviation is a prefix of | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # multiple option names) are handled later in _osprey_fix_argv. | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | ▶ | if ($config->{abbreviate}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 7 | for my $len (1 .. length($name) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | 60 | my $abbreviated = substr $name, 0, $len; | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 60 | ▶ | push @{ $abbreviations{$abbreviated} }, $name unless exists $fullnames{$abbreviated}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 16 | return \@getopt, \%abbreviations; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | sub _osprey_fix_argv { | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 16 | ▶ | my ($options, $abbreviations) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_osprey_fix_argv: called CC=14 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 16 | my @new_argv; | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 16 | ▶ | while (defined( my $arg = shift @ARGV )) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # As soon as we find a -- or a non-option word, stop processing and leave everything | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # from there onwards in ARGV as either positional args or a subcommand. | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | ▶ | if ($arg eq '--' or $arg eq '-' or $arg !~ /^-/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$arg eq "--" or $arg eq "-" or not $arg =~ /^-/
$arg eq "--" or $arg eq "-"
MC/DC33%1/3
$arg eq "--" or $arg eq "-" or not $arg =~ /^-/
$arg eq "--"
$arg eq "-"
not $arg =~ /^-/
Truth table50%2/4
A or B or not(C)
A= $arg eq "--"B= $arg eq "-"C= not $arg =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 5 | push @new_argv, $arg, @ARGV; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | my ($arg_name_with_dash, $arg_value) = split /=/, $arg, 2; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | unshift @ARGV, $arg_value if defined $arg_value; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | my ($dash, $negative, $arg_name_without_dash) | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | = $arg_name_with_dash =~ /^(-+)(no\-)?(.+)$/; | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | my $option_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 2 | ▶ | if ($dash eq '--') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 2 | my $option_names = $abbreviations->{$arg_name_without_dash}; | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 2 | ▶ | if (defined $option_names) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | if (@$option_names == 1) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 2 | $option_name = $option_names->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # TODO: can't we produce a warning saying that it's ambiguous and which options conflict? | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $option_name = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | ▶ | my $arg_name = ($dash || '') . ($negative || ''); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$dash || ''
$negative || ''
MC/DC0%0/1
$dash || ''
$dash
MC/DC0%0/1
$negative || ''
$negative
Truth table50%1/2
A
A= $dash
Truth table50%1/2
A
A= $negative
| |||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if (defined $option_name) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | $arg_name .= $option_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $arg_name .= $arg_name_without_dash; | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | push @new_argv, $arg_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | if (defined $option_name && $options->{$option_name}{format}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $option_name and $options->{$option_name}{"format"}
MC/DC0%0/2
defined $option_name and $options->{$option_name}{"format"}
defined $option_name
$options->{$option_name}{"format"}
Truth table33%1/3
A and B
A= defined $option_nameB= $options->{$option_name}{"format"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | push @new_argv, shift @ARGV; | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 16 | return @new_argv; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 4 | requires qw(_osprey_config _osprey_options _osprey_subcommands); | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 4 | has 'parent_command' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 4 | has 'invoked_as' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | sub new_with_options { | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 15 | ▶ | my ($class, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_with_options: called CC=20 SCAR=40.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 15 | my %config = $class->_osprey_config; | |||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 15 | ▶ | local @ARGV = @ARGV if $config{protect_argv}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 15 | ▶ | if (!defined $params{invoked_as}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 11 | $params{invoked_as} = Getopt::Long::Descriptive::prog_name(); | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 15 | my ($parsed_params, $usage) = $class->parse_options(%params); | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 15 | ▶ | if ($parsed_params->{h}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | return $class->osprey_usage(1, $usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 152 | } elsif ($parsed_params->{help}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | return $class->osprey_help(1, $usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 154 | } elsif ($parsed_params->{man}) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | return $class->osprey_man($usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 15 | my %merged_params; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | 15 | ▶ | if ($config{prefer_commandline}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 15 | %merged_params = (%params, %$parsed_params); | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | %merged_params = (%$parsed_params, %params); | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 15 | my %subcommands = $class->_osprey_subcommands; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 15 | my ($subcommand_name, $subcommand_class); | |||||||||||||||||||||||||||||||||||||||||||||
| 167 | 15 | ▶ | if (@ARGV && $ARGV[0] ne '--') { # Check what to do with remaining options | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@ARGV and $ARGV[0] ne "--"
MC/DC50%1/2
@ARGV and $ARGV[0] ne "--"
@ARGV
$ARGV[0] ne "--"
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] ne "--"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 5 | ▶ | if ($ARGV[0] =~ /^--/) { # Getopt stopped at an unrecognized option, error. | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | print STDERR "Unknown option '$ARGV[0]'.\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | return $class->osprey_usage(1, $usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | } elsif (%subcommands) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 5 | $subcommand_name = shift @ARGV; # Remove it so the subcommand sees only options | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 5 | $subcommand_class = $subcommands{$subcommand_name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 5 | ▶ | if (!defined $subcommand_class) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | print STDERR "Unknown subcommand '$subcommand_name'.\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | return $class->osprey_usage(1, $usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # If we're not expecting a subcommand, and getopt didn't stop at an option, consider the remainder | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # as positional args and leave them in ARGV. | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 15 | my $self; | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 15 | ▶ | unless (eval { $self = $class->new(%merged_params); 1 }) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($@ =~ /^Attribute \((.*?)\) is required/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | print STDERR "$1 is missing\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | } elsif ($@ =~ /^Missing required arguments: (.*) at /) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my @missing_required = split /,\s/, $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | print STDERR "$_ is missing\n" for @missing_required; | |||||||||||||||||||||||||||||||||||||||||||||
| 190 | } elsif ($@ =~ /^(.*?) required/) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | print STDERR "$1 is missing\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | } elsif ($@ =~ /^isa check .*?failed: /) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | print STDERR substr($@, index($@, ':') + 2); | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | print STDERR $@; | |||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | return $class->osprey_usage(1, $usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 15 | ▶ | return $self unless $subcommand_class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 5 | ▶ | use_module($subcommand_class) unless ref $subcommand_class; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | return $subcommand_class->new_with_options( | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | %params, | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | parent_command => $self, | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | invoked_as => "$params{invoked_as} $subcommand_name" | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||
| 211 | sub parse_options { | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 16 | ▶ | my ($class, %params) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_options: called CC=9 SCAR=22.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 16 | my %options = $class->_osprey_options; | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | 16 | my %config = $class->_osprey_config; | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | 16 | my %subcommands = $class->_osprey_subcommands; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 16 | my ($options, $abbreviations) = _osprey_prepare_options(\%options, \%config); | |||||||||||||||||||||||||||||||||||||||||||||
| 219 | 16 | @ARGV = _osprey_fix_argv(\%options, $abbreviations); | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 16 | ▶ | my @getopt_options = %subcommands ? qw(require_order) : (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 16 | ▶ | push @getopt_options, @{$config{getopt_options}} if defined $config{getopt_options}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 16 | my $prog_name = $params{invoked_as}; | |||||||||||||||||||||||||||||||||||||||||||||
| 226 | 16 | ▶ | $prog_name = Getopt::Long::Descriptive::prog_name() if !defined $prog_name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 16 | my $usage_str = $config{usage_string}; | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | 16 | ▶ | unless (defined $usage_str) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 16 | ▶ | if (%subcommands) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 11 | $usage_str = "Usage: $prog_name %o [subcommand]"; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 5 | $usage_str = "Usage: $prog_name %o"; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 16 | my ($opt, $usage) = describe_options( | |||||||||||||||||||||||||||||||||||||||||||||
| 238 | $usage_str, | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | @$options, | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | [], | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | [ 'h', "show a short help message" ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 242 | [ 'help', "show a long help message" ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | [ 'man', "show the manual" ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | { getopt_conf => \@getopt_options }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 16 | $usage->{prog_name} = $prog_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 248 | 16 | $usage->{target} = $class; | |||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 16 | ▶ | if ($usage->{should_die}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | return $class->osprey_usage(1, $usage); | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 16 | my %parsed_params; | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 16 | for my $name (keys %options, qw(h help man)) { | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | 55 | my $val = $opt->$name(); | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | 55 | ▶ | $parsed_params{$name} = $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 16 | return \%parsed_params, $usage; | |||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||
| 265 | sub osprey_usage { | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | my ($class, $code, @messages) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
osprey_usage: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | my $usage; | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | if (@messages && blessed($messages[0]) && $messages[0]->isa('CLI::Osprey::Descriptive::Usage')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@messages and builtin::blessed($messages[0]) and $messages[0]->isa("CLI::Osprey::Descriptive::Usage")
@messages and builtin::blessed($messages[0])
MC/DC0%0/3
@messages and builtin::blessed($messages[0]) and $messages[0]->isa("CLI::Osprey::Descriptive::Usage")
@messages
builtin::blessed($messages[0])
$messages[0]->isa("CLI::Osprey::Descriptive::Usage")
Truth table0%0/4
A and B and C
A= @messagesB= builtin::blessed($messages[0])C= $messages[0]->isa("CLI::Osprey::Descriptive::Usage")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $usage = shift @messages; | |||||||||||||||||||||||||||||||||||||||||||||
| 272 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | local @ARGV = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | (undef, $usage) = $class->parse_options(help => 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | my $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | $message = join("\n", @messages, '') if @messages; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $message .= $usage . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ($code) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | CORE::warn $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 283 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | print $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | ▶ | exit $code if defined $code; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | sub osprey_help { | ||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | my ($class, $code, $usage) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
osprey_help: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | unless (defined $usage && blessed($usage) && $usage->isa('CLI::Osprey::Descriptive::Usage')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $usage and builtin::blessed($usage) and $usage->isa("CLI::Osprey::Descriptive::Usage")
defined $usage and builtin::blessed($usage)
MC/DC0%0/3
defined $usage and builtin::blessed($usage) and $usage->isa("CLI::Osprey::Descriptive::Usage")
defined $usage
builtin::blessed($usage)
$usage->isa("CLI::Osprey::Descriptive::Usage")
Truth table0%0/4
A and B and C
A= defined $usageB= builtin::blessed($usage)C= $usage->isa("CLI::Osprey::Descriptive::Usage")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | local @ARGV = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | (undef, $usage) = $class->parse_options(help => 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | my $message = $usage->option_help . "\n"; | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | if ($code) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | CORE::warn $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 302 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | print $message; | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | exit $code if defined $code; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||
| 309 | sub osprey_man { | ||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | my ($class, $usage, $output) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
osprey_man: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | unless (defined $usage && blessed($usage) && $usage->isa('CLI::Osprey::Descriptive::Usage')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $usage and builtin::blessed($usage) and $usage->isa("CLI::Osprey::Descriptive::Usage")
defined $usage and builtin::blessed($usage)
MC/DC0%0/3
defined $usage and builtin::blessed($usage) and $usage->isa("CLI::Osprey::Descriptive::Usage")
defined $usage
builtin::blessed($usage)
$usage->isa("CLI::Osprey::Descriptive::Usage")
Truth table0%0/4
A and B and C
A= defined $usageB= builtin::blessed($usage)C= $usage->isa("CLI::Osprey::Descriptive::Usage")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | local @ARGV = (); | |||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | (undef, $usage) = $class->parse_options(man => 1); | |||||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | my $tmpdir = Path::Tiny->tempdir; | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my $podfile = $tmpdir->child("help.pod"); | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | $podfile->spew_utf8($usage->option_pod); | |||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | require Pod::Usage; | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | Pod::Usage::pod2usage( | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | -verbose => 2, | ||||||||||||||||||||||||||||||||||||||||||||||
| 324 | -input => "$podfile", | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | -exitval => 'NOEXIT', | ||||||||||||||||||||||||||||||||||||||||||||||
| 326 | -output => $output, | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | exit(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | sub _osprey_subcommand_desc { | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_osprey_subcommand_desc: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my %config = $class->_osprey_config; | |||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | return $config{desc}; | |||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||
| 340 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||