| 1 | package Getopt::App; | ||||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use feature qw(:5.16); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 7 | our $VERSION = '0.13'; | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 7 | our ($OPT_COMMENT_RE, $OPTIONS, $SUBCOMMAND, $SUBCOMMANDS, %APPS) = (qr{\s+\#\s+}); | |||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | our $call_maybe = sub { | ||||||||||||||||||||||||||||||||
| 16 | 189 | ▶ | my ($app, $m) = (shift, shift); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 17 | 189 | local $Getopt::App::APP_CLASS; | |||||||||||||||||||||||||||||||
| 18 | 189 | ▶ | $m = $app->can($m) || __PACKAGE__->can("_$m"); | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$app->can($m) || __PACKAGE__->can("_$m")
MC/DC100%2/2
$app->can($m) || __PACKAGE__->can("_$m")
$app->can($m)
__PACKAGE__->can("_$m")
Truth table100%3/3
A || B
A= $app->can($m)B= __PACKAGE__->can("_$m")
| |||||||||||||||||||||||||||||||||
| 19 | 189 | ▶ | return $m ? $app->$m(@_) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 20 | 7 | }; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | sub bundle { | ||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | my ($class, $script, $OUT) = (@_, \*STDOUT); | ||||||||||||||||||||||||||||||
|
Subroutine
bundle: called CC=18 SCAR=30.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 24 | 1 | my ($package, @script); | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | open my $SCRIPT, '<', $script or croak "Can't read $script: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | while (my $line = readline $SCRIPT) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if ($line =~ m!^\s*package\s+\S+\s*;!) { # look for app class name | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 29 | 1 | $package .= $line; | |||||||||||||||||||||||||||||||
| 30 | 1 | last; | |||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | elsif ($. == 1) { # look for hashbang | ||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | $line =~ m/^#!/ ? print {$OUT} $line : do { print {$OUT} "#!$^X\n"; push @script, $line }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||||||
| 36 | 0 | push @script, $line; | |||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | last if $line =~ m!^[^#]+;!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 1 | my $out_line = ''; | |||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | open my $SELF, '<', __FILE__ or croak "Can't read Getopt::App: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | while (my $line = readline $SELF) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 44 | 287 | ▶ | next if $line =~ m!(?:\bVERSION\s|^\s*$)!; # TODO: Should version get skipped? | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 45 | 234 | ▶ | next if $line =~ m!^sub bundle\s\{! .. $line =~ m!^}$!; # skip bundle() | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 191 | ▶ | last if $line =~ m!^1;\s*$!; # do not include POD | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 190 | chomp $line; | |||||||||||||||||||||||||||||||
| 49 | 190 | ▶ | if ($line =~ m!^sub\s!) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 50 | 16 | ▶ | print {$OUT} $out_line, "\n" if $out_line; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 51 | 16 | ▶ | $line =~ m!\}$! ? print {$OUT} $line, "\n" : ($out_line = $line); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | elsif ($line =~ m!^}$!) { | ||||||||||||||||||||||||||||||||
| 54 | 14 | print {$OUT} $out_line, $line, "\n"; | |||||||||||||||||||||||||||||||
| 55 | 14 | $out_line = ''; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||||||
| 58 | 160 | $line =~ s!^[ ]{2,}!!; # remove leading white space | |||||||||||||||||||||||||||||||
| 59 | 160 | $line =~ s!\#\s.*!!; # remove comments | |||||||||||||||||||||||||||||||
| 60 | 160 | $out_line .= $line; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 1 | print {$OUT} qq(BEGIN{\$INC{'Getopt/App.pm'}='BUNDLED'}\n); | |||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | print {$OUT} +($package || "package main\n"); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$package || "package main\n"
MC/DC0%0/1
$package || "package main\n"
$package
Truth table50%1/2
A
A= $package
| |||||||||||||||||||||||||||||||||
| 66 | 1 | print {$OUT} @script; | |||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | print {$OUT} $_ while readline $SCRIPT; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | sub capture { | ||||||||||||||||||||||||||||||||
| 71 | 25 | ▶ | my ($app, $argv) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
capture: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 25 | require File::Temp; | |||||||||||||||||||||||||||||||
| 74 | 25 | my ($STDOUT_CAPTURE, $STDERR_CAPTURE) = (File::Temp->new, File::Temp->new); | |||||||||||||||||||||||||||||||
| 75 | 25 | ▶ | open my $STDOUT_ORIG, '>&STDOUT' or die "Can't remember original STDOUT: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 25 | ▶ | open my $STDERR_ORIG, '>&STDERR' or die "Can't remember original STDERR: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | my $restore = sub { | ||||||||||||||||||||||||||||||||
| 79 | 25 | ▶ | open STDERR, '>&', fileno($STDERR_ORIG) or die "Can't restore STDERR: $!"; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=15.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 80 | 25 | ▶ | open STDOUT, '>&', fileno($STDOUT_ORIG) or die "Can't restore STDOUT: $!"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 25 | ▶ | die $_[0] if $_[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | 25 | }; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 25 | ▶ | open STDOUT, '>&', fileno($STDOUT_CAPTURE) or $restore->("Can't capture STDOUT: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | 25 | ▶ | open STDERR, '>&', fileno($STDERR_CAPTURE) or $restore->("Can't capture STDERR: $!"); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 25 | my $exit_value; | |||||||||||||||||||||||||||||||
| 88 | 25 | ▶ | unless (eval { $exit_value = $app->($argv || [@ARGV]); 1; }) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$argv || [@ARGV]
MC/DC100%1/1
$argv || [@ARGV]
$argv
Truth table100%2/2
A
A= $argv
| |||||||||||||||||||||||||||||||||
| 89 | 4 | print STDERR $@; | |||||||||||||||||||||||||||||||
| 90 | 4 | $exit_value = int $!; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 25 | STDERR->flush; | |||||||||||||||||||||||||||||||
| 94 | 25 | STDOUT->flush; | |||||||||||||||||||||||||||||||
| 95 | 25 | $restore->(); | |||||||||||||||||||||||||||||||
| 96 | 25 | seek $STDERR_CAPTURE, 0, 0; | |||||||||||||||||||||||||||||||
| 97 | 25 | seek $STDOUT_CAPTURE, 0, 0; | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 25 | return [join('', <$STDOUT_CAPTURE>), join('', <$STDERR_CAPTURE>), $exit_value]; | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | sub extract_usage { | ||||||||||||||||||||||||||||||||
| 103 | 4 | ▶ | my %pod2usage; | ||||||||||||||||||||||||||||||
|
Subroutine
extract_usage: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||
| 104 | 4 | $pod2usage{'-sections'} = shift; | |||||||||||||||||||||||||||||||
| 105 | 4 | ▶ | $pod2usage{'-input'} = shift || (caller)[1]; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
shift() || (caller)[1]
MC/DC0%0/2
shift() || (caller)[1]
shift()
(caller)[1]
Truth table33%1/3
A || B
A= shift()B= (caller)[1]
| |||||||||||||||||||||||||||||||||
| 106 | 4 | ▶ | $pod2usage{'-verbose'} = 99 if $pod2usage{'-sections'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 4 | require Pod::Usage; | |||||||||||||||||||||||||||||||
| 109 | 4 | open my $USAGE, '>', \my $usage; | |||||||||||||||||||||||||||||||
| 110 | 4 | Pod::Usage::pod2usage(-exitval => 'noexit', -output => $USAGE, %pod2usage); | |||||||||||||||||||||||||||||||
| 111 | 4 | close $USAGE; | |||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 4 | ▶ | $usage //= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$usage //= ""
MC/DC100%1/1
$usage //= ""
$usage
Truth table100%2/2
A
A= $usage
| |||||||||||||||||||||||||||||||||
| 114 | 4 | ▶ | $usage =~ s!^(.*?)\n!!s if $pod2usage{'-sections'}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 115 | 4 | $usage =~ s!^Usage:\n\s+([A-Z])!$1!s; # Remove "Usage" header if SYNOPSIS has a description | |||||||||||||||||||||||||||||||
| 116 | 4 | $usage =~ s!^ !!gm; | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 4 | ▶ | return join '', $usage, _usage_for_subcommands($SUBCOMMANDS || []), _usage_for_options($OPTIONS || []); | ||||||||||||||||||||||||||||||
|
Condition75%3/4
$SUBCOMMANDS || []
$OPTIONS || []
MC/DC100%1/1
$SUBCOMMANDS || []
$SUBCOMMANDS
MC/DC0%0/1
$OPTIONS || []
$OPTIONS
Truth table100%2/2
A
A= $SUBCOMMANDS
Truth table50%1/2
A
A= $OPTIONS
| |||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||
| 121 | sub import { | ||||||||||||||||||||||||||||||||
| 122 | 20 | ▶ | my ($class, @flags) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=12 SCAR=25.2
| |||||||||||||||||||||||||||||||||
| 123 | 20 | my $caller = caller; | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 20 | $_->import for qw(strict warnings utf8); | |||||||||||||||||||||||||||||||
| 126 | 20 | feature->import(':5.16'); | |||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 20 | my $skip_default; | |||||||||||||||||||||||||||||||
| 129 | 7 | ▶ | no strict qw(refs); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 130 | 20 | ▶ | while (my $flag = shift @flags) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 10 | ▶ | if ($flag eq '-capture') { | ||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||
| 132 | 6 | *{"$caller\::capture"} = \&capture; | |||||||||||||||||||||||||||||||
| 133 | 6 | $skip_default = 1; | |||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||
| 135 | elsif ($flag eq '-complete') { | ||||||||||||||||||||||||||||||||
| 136 | 2 | require Getopt::App::Complete; | |||||||||||||||||||||||||||||||
| 137 | 2 | *{"$caller\::generate_completion_script"} = \&Getopt::App::Complete::generate_completion_script; | |||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | elsif ($flag eq '-signatures') { | ||||||||||||||||||||||||||||||||
| 140 | 0 | require experimental; | |||||||||||||||||||||||||||||||
| 141 | 0 | experimental->import(qw(signatures)); | |||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||
| 143 | elsif ($flag !~ /^-/) { | ||||||||||||||||||||||||||||||||
| 144 | 2 | ▶ | croak "package definition required - cannot extend main with $flag!" if $caller eq 'main'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | croak "require $flag FAIL $@" unless eval "require $flag;1"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 146 | 1 | push @{"${caller}::ISA"}, $flag; | |||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 19 | ▶ | unless ($skip_default) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 151 | 13 | ▶ | *{"$caller\::extract_usage"} = \&extract_usage unless $caller->can('extract_usage'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | 13 | ▶ | *{"$caller\::new"} = \&new unless $caller->can('new'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 13 | *{"$caller\::run"} = \&run; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | sub new { | ||||||||||||||||||||||||||||||||
| 158 | 51 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 159 | 51 | ▶ | bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | sub run { | ||||||||||||||||||||||||||||||||
| 163 | 59 | ▶ | my @rules = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 164 | 59 | ▶ | my $class = $Getopt::App::APP_CLASS || caller; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$Getopt::App::APP_CLASS || caller
MC/DC0%0/2
$Getopt::App::APP_CLASS || caller
$Getopt::App::APP_CLASS
caller
Truth table66%2/3
A || B
A= $Getopt::App::APP_CLASSB= caller
| |||||||||||||||||||||||||||||||||
| 165 | 47 | ▶ | return sub { local $Getopt::App::APP_CLASS = $class; run(@_, @rules) } | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 166 | 59 | ▶ | if !$Getopt::App::APP_CLASS and defined wantarray; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $Getopt::App::APP_CLASS and defined wantarray
MC/DC50%1/2
not $Getopt::App::APP_CLASS and defined wantarray
not $Getopt::App::APP_CLASS
defined wantarray
Truth table66%2/3
not(A) and B
A= not $Getopt::App::APP_CLASSB= defined wantarray
| |||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 47 | my $cb = pop @rules; | |||||||||||||||||||||||||||||||
| 169 | 47 | ▶ | my $argv = ref $rules[0] eq 'ARRAY' ? shift @rules : [@ARGV]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 47 | local $OPTIONS = [@rules]; | |||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 47 | my $app = $class->new; | |||||||||||||||||||||||||||||||
| 173 | 47 | ▶ | return $app->$call_maybe('getopt_complete_reply') if defined $ENV{COMP_POINT} and defined $ENV{COMP_LINE}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ENV{"COMP_POINT"} and defined $ENV{"COMP_LINE"}
MC/DC50%1/2
defined $ENV{"COMP_POINT"} and defined $ENV{"COMP_LINE"}
defined $ENV{"COMP_POINT"}
defined $ENV{"COMP_LINE"}
Truth table66%2/3
A and B
A= defined $ENV{"COMP_POINT"}B= defined $ENV{"COMP_LINE"}
| |||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||
| 175 | 37 | $app->$call_maybe(getopt_pre_process_argv => $argv); | |||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 37 | local $SUBCOMMANDS = $app->$call_maybe('getopt_subcommands'); | |||||||||||||||||||||||||||||||
| 178 | 37 | ▶ | my $exit_value = $SUBCOMMANDS ? _subcommand_run_maybe($app, $SUBCOMMANDS, $argv) : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 179 | 33 | ▶ | return _exit($app, $exit_value) if defined $exit_value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | 24 | return _run($app, \@rules, $argv, $cb); | |||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 10 | ▶ | sub _getopt_complete_reply { Getopt::App::Complete::complete_reply(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
_getopt_complete_reply: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||
| 185 | 24 | ▶ | sub _getopt_configure {qw(bundling no_auto_abbrev no_ignore_case pass_through require_order)} | ||||||||||||||||||||||||||||||
|
Subroutine
_getopt_configure: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||
| 187 | sub _getopt_load_subcommand { | ||||||||||||||||||||||||||||||||
| 188 | 6 | ▶ | my ($app, $subcommand, $argv) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_getopt_load_subcommand: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 189 | 6 | ▶ | return $subcommand->[1] if ref $subcommand->[1] eq 'CODE'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | 6 | ($@, $!) = ('', 0); | |||||||||||||||||||||||||||||||
| 192 | 6 | ▶ | croak "Unable to load subcommand $subcommand->[0]: $@ ($!)" unless my $code = do $subcommand->[1]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 193 | 5 | return $code; | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | sub _getopt_post_process_argv { | ||||||||||||||||||||||||||||||||
| 197 | 23 | ▶ | my ($app, $argv, $state) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_getopt_post_process_argv: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 198 | 23 | ▶ | return unless $state->{valid}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 199 | 23 | ▶ | return unless $argv->[0] and $argv->[0] =~ m!^-!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$argv->[0] and $argv->[0] =~ /^-/u
MC/DC100%2/2
$argv->[0] and $argv->[0] =~ /^-/u
$argv->[0]
$argv->[0] =~ /^-/u
Truth table100%3/3
A and B
A= $argv->[0]B= $argv->[0] =~ /^-/u
| |||||||||||||||||||||||||||||||||
| 200 | 1 | $! = 1; | |||||||||||||||||||||||||||||||
| 201 | 1 | die "Invalid argument or argument order: @$argv\n"; | |||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | sub _getopt_unknown_subcommand { | ||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | my ($app, $argv) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_getopt_unknown_subcommand: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 206 | 1 | $! = 2; | |||||||||||||||||||||||||||||||
| 207 | 1 | die "Unknown subcommand: $argv->[0]\n"; | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | sub _exit { | ||||||||||||||||||||||||||||||||
| 211 | 33 | ▶ | my ($app, $exit_value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_exit: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 212 | 33 | ▶ | $exit_value = $app->$call_maybe(getopt_post_process_exit_value => $exit_value) // $exit_value; | ||||||||||||||||||||||||||||||
|
Condition100%3/3
$app->$call_maybe("getopt_post_process_exit_value", $exit_value) // $exit_value
MC/DC100%2/2
$app->$call_maybe("getopt_post_process_exit_value", $exit_value) // $exit_value
$app->$call_maybe("getopt_post_process_exit_value", $exit_value)
$exit_value
Truth table100%3/3
A // B
A= $app->$call_maybe("getopt_post_process_exit_value", $exit_value)B= $exit_value
| |||||||||||||||||||||||||||||||||
| 213 | 33 | ▶ | $exit_value = 0 unless $exit_value and $exit_value =~ m!^\d{1,3}$!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$exit_value and $exit_value =~ /^\d{1,3}$/u
MC/DC100%2/2
$exit_value and $exit_value =~ /^\d{1,3}$/u
$exit_value
$exit_value =~ /^\d{1,3}$/u
Truth table100%3/3
A and B
A= $exit_valueB= $exit_value =~ /^\d{1,3}$/u
| |||||||||||||||||||||||||||||||||
| 214 | 33 | ▶ | $exit_value = 255 unless $exit_value < 255; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 33 | ▶ | exit $exit_value unless $Getopt::App::APP_CLASS; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 216 | 33 | return $exit_value; | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | sub _run { | ||||||||||||||||||||||||||||||||
| 220 | 26 | ▶ | my ($app, $rules, $argv, $cb) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_run: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 221 | 26 | s!$OPT_COMMENT_RE.*$!! for @$rules; | |||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | 26 | my @configure = $app->$call_maybe('getopt_configure'); | |||||||||||||||||||||||||||||||
| 224 | 26 | my $prev = Getopt::Long::Configure(@configure); | |||||||||||||||||||||||||||||||
| 225 | 26 | ▶ | my $valid = Getopt::Long::GetOptionsFromArray($argv, $app, @$rules) ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 226 | 26 | Getopt::Long::Configure($prev); | |||||||||||||||||||||||||||||||
| 227 | 26 | $app->$call_maybe(getopt_post_process_argv => $argv, {valid => $valid}); | |||||||||||||||||||||||||||||||
| 228 | 24 | ▶ | return _exit($app, $valid ? $app->$cb(@$argv) : 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | sub _subcommand_run { | ||||||||||||||||||||||||||||||||
| 232 | 13 | ▶ | my ($app, $subcommand, $argv) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_subcommand_run: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 233 | 13 | local $Getopt::App::SUBCOMMAND = $subcommand; | |||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | 13 | my $method = $app->can($subcommand->[1]); | |||||||||||||||||||||||||||||||
| 236 | 13 | ▶ | return _run($app, [@$OPTIONS], [@$argv[1 .. $#$argv]], $method) if $method; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||
| 238 | 11 | ▶ | unless ($APPS{$subcommand->[1]}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 239 | 6 | $APPS{$subcommand->[1]} = $app->$call_maybe(getopt_load_subcommand => $subcommand, $argv); | |||||||||||||||||||||||||||||||
| 240 | 5 | ▶ | croak "$subcommand->[0] did not return a code ref" unless ref $APPS{$subcommand->[1]} eq 'CODE'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||
| 243 | 10 | return $APPS{$subcommand->[1]}->([@$argv[1 .. $#$argv]]); | |||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||
| 246 | sub _subcommand_run_maybe { | ||||||||||||||||||||||||||||||||
| 247 | 17 | ▶ | my ($app, $subcommands, $argv) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_subcommand_run_maybe: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 248 | 17 | ▶ | return undef unless $argv->[0] and $argv->[0] =~ m!^\w!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$argv->[0] and $argv->[0] =~ /^\w/u
MC/DC100%2/2
$argv->[0] and $argv->[0] =~ /^\w/u
$argv->[0]
$argv->[0] =~ /^\w/u
Truth table100%3/3
A and B
A= $argv->[0]B= $argv->[0] =~ /^\w/u
| |||||||||||||||||||||||||||||||||
| 249 | return $app->$call_maybe(getopt_unknown_subcommand => $argv) | ||||||||||||||||||||||||||||||||
| 250 | 14 | ▶ | unless my $subcommand = first { $_->[0] eq $argv->[0] } @$subcommands; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 251 | 10 | return _subcommand_run($app, $subcommand, $argv); | |||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||
| 254 | sub _usage_for_options { | ||||||||||||||||||||||||||||||||
| 255 | 4 | ▶ | my ($rules) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_usage_for_options: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 256 | 4 | ▶ | return '' unless @$rules; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||
| 258 | 3 | my ($len, @options) = (0); | |||||||||||||||||||||||||||||||
| 259 | 3 | for (@$rules) { | |||||||||||||||||||||||||||||||
| 260 | 9 | my @o = split $OPT_COMMENT_RE, $_, 2; | |||||||||||||||||||||||||||||||
| 261 | 9 | $o[0] =~ s/(=[si][@%]?|\!|\+)$//; | |||||||||||||||||||||||||||||||
| 262 | 9 | ▶ | $o[0] = join ', ', map { length($_) == 1 ? "-$_" : "--$_" } sort { length($b) <=> length($a) } split /\|/, $o[0]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 263 | 9 | ▶ | $o[1] //= ''; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$o[1] //= ""
MC/DC100%1/1
$o[1] //= ""
$o[1]
Truth table100%2/2
A
A= $o[1]
| |||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||
| 265 | 9 | my $l = length $o[0]; | |||||||||||||||||||||||||||||||
| 266 | 9 | ▶ | $len = $l if $l > $len; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 267 | 9 | push @options, \@o; | |||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | 3 | return "Options:\n" . join('', map { sprintf " %-${len}s %s\n", @$_ } @options) . "\n"; | |||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||
| 273 | sub _usage_for_subcommands { | ||||||||||||||||||||||||||||||||
| 274 | 4 | ▶ | my ($subcommands) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_usage_for_subcommands: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||
| 275 | 4 | ▶ | return '' unless @$subcommands; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||
| 277 | 1 | my ($len, @cmds) = (0); | |||||||||||||||||||||||||||||||
| 278 | 1 | for my $s (@$subcommands) { | |||||||||||||||||||||||||||||||
| 279 | 6 | my $l = length $s->[0]; | |||||||||||||||||||||||||||||||
| 280 | 6 | ▶ | $len = $l if $l > $len; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 281 | 6 | ▶ | push @cmds, [$s->[0], $s->[2] // '']; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$s->[2] // ""
MC/DC0%0/1
$s->[2] // ""
$s->[2]
Truth table50%1/2
A
A= $s->[2]
| |||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 1 | return "Subcommands:\n" . join('', map { sprintf " %-${len}s %s\n", @$_ } @cmds) . "\n"; | |||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | 7 | 1; | |||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||
| 289 | =encoding utf8 | ||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | Getopt::App - Write and test your script with ease | ||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||
| 295 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | =head2 The script file | ||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||
| 299 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||
| 300 | package My::Script; | ||||||||||||||||||||||||||||||||
| 301 | use Getopt::App -complete, -signatures; | ||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||
| 303 | # See "APPLICATION METHODS" | ||||||||||||||||||||||||||||||||
| 304 | sub getopt_post_process_argv ($app, $argv, $state) { ... } | ||||||||||||||||||||||||||||||||
| 305 | sub getopt_configure ($app) { ... } | ||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||
| 307 | # run() must be the last statement in the script | ||||||||||||||||||||||||||||||||
| 308 | run( | ||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||
| 310 | # Specify your Getopt::Long options and optionally a help text | ||||||||||||||||||||||||||||||||
| 311 | 'h|help # Output help', | ||||||||||||||||||||||||||||||||
| 312 | 'v+ # Verbose output', | ||||||||||||||||||||||||||||||||
| 313 | 'name=s # Specify a name', | ||||||||||||||||||||||||||||||||
| 314 | 'completion-script # Print autocomplete script', | ||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | # Here is the main sub that will run the script | ||||||||||||||||||||||||||||||||
| 317 | sub ($app, @extra) { | ||||||||||||||||||||||||||||||||
| 318 | return print generate_completion_script() if $app->{'completion-script'}; | ||||||||||||||||||||||||||||||||
| 319 | return print extract_usage() if $app->{h}; | ||||||||||||||||||||||||||||||||
| 320 | say $app->{name} // 'no name'; # Access command line options | ||||||||||||||||||||||||||||||||
| 321 | return 42; # Reture value is used as exit code | ||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | ); | ||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||
| 325 | =head2 Running the script | ||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||
| 327 | The example script above can be run like any other script: | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | $ my-script --name superwoman; # prints "superwoman" | ||||||||||||||||||||||||||||||||
| 330 | $ echo $? # 42 | ||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | =head2 Testing | ||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||
| 334 | use Test::More; | ||||||||||||||||||||||||||||||||
| 335 | use Cwd qw(abs_path); | ||||||||||||||||||||||||||||||||
| 336 | use Getopt::App -capture; | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | # Sourcing the script returns a callback | ||||||||||||||||||||||||||||||||
| 339 | my $app = do(abs_path('./bin/myapp')); | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | # The callback can be called with any @ARGV | ||||||||||||||||||||||||||||||||
| 342 | subtest name => sub { | ||||||||||||||||||||||||||||||||
| 343 | my $got = capture($app, [qw(--name superwoman)]); | ||||||||||||||||||||||||||||||||
| 344 | is $got->[0], "superwoman\n", 'stdout'; | ||||||||||||||||||||||||||||||||
| 345 | is $got->[1], '', 'stderr'; | ||||||||||||||||||||||||||||||||
| 346 | is $got->[2], 42, 'exit value'; | ||||||||||||||||||||||||||||||||
| 347 | }; | ||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||
| 349 | done_testing; | ||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||
| 351 | =head2 Subcommands | ||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||
| 353 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||
| 354 | # Define a package to avoid mixing methods after loading the subcommand script | ||||||||||||||||||||||||||||||||
| 355 | package My::App::main; | ||||||||||||||||||||||||||||||||
| 356 | use Getopt::App -complete; | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | # getopt_subcommands() is called by Getopt::App | ||||||||||||||||||||||||||||||||
| 359 | sub getopt_subcommands { | ||||||||||||||||||||||||||||||||
| 360 | my $app = shift; | ||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||
| 362 | return [ | ||||||||||||||||||||||||||||||||
| 363 | ['find', '/path/to/subcommand/find.pl', 'Find things'], | ||||||||||||||||||||||||||||||||
| 364 | ['update', '/path/to/subcommand/update.pl', 'Update things'], | ||||||||||||||||||||||||||||||||
| 365 | ]; | ||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||
| 368 | # run() is only called if there are no matching sub commands | ||||||||||||||||||||||||||||||||
| 369 | run( | ||||||||||||||||||||||||||||||||
| 370 | 'h # Print help', | ||||||||||||||||||||||||||||||||
| 371 | 'completion-script # Print autocomplete script', | ||||||||||||||||||||||||||||||||
| 372 | sub { | ||||||||||||||||||||||||||||||||
| 373 | my ($app, @args) = @_; | ||||||||||||||||||||||||||||||||
| 374 | return print generate_completion_script() if $app->{'completion-script'}; | ||||||||||||||||||||||||||||||||
| 375 | return print extract_usage(); | ||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||
| 377 | ); | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | See L</getopt_subcommands> and L<https://github.com/jhthorsen/getopt-app/tree/main/example> | ||||||||||||||||||||||||||||||||
| 380 | for more details. | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | L<Getopt::App> is a module that helps you structure your scripts and integrates | ||||||||||||||||||||||||||||||||
| 385 | L<Getopt::Long> with a very simple API. In addition it makes it very easy to | ||||||||||||||||||||||||||||||||
| 386 | test your script, since the script file can be sourced without actually being | ||||||||||||||||||||||||||||||||
| 387 | run. | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | L<Getopt::App> also supports infinite nested L<subcommands|/getopt_subcommands> | ||||||||||||||||||||||||||||||||
| 390 | and a method for L<bundling|/bundle> this module with your script to prevent | ||||||||||||||||||||||||||||||||
| 391 | depending on a module from CPAN. | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | =head1 APPLICATION METHODS | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | These methods are optional, but can be defined in your script to override the | ||||||||||||||||||||||||||||||||
| 396 | default behavior. | ||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||
| 398 | =head2 getopt_complete_reply | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | $app->getopt_complete_reply; | ||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | This method will be called instead of the L</run> callback when the | ||||||||||||||||||||||||||||||||
| 403 | C<COMP_LINE> and C<COMP_POINT> environment variables are set. The default | ||||||||||||||||||||||||||||||||
| 404 | implementation will call L<Getopt::App::Complete/complete_reply>. | ||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||
| 406 | See also "Completion" under L</import>. | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | =head2 getopt_configure | ||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||
| 410 | @configure = $app->getopt_configure; | ||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||
| 412 | This method can be defined if you want L<Getopt::Long/Configure> to be set up | ||||||||||||||||||||||||||||||||
| 413 | differently. The default return value is: | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | qw(bundling no_auto_abbrev no_ignore_case pass_through require_order) | ||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||
| 417 | Note that the default "pass_through" item is to enable the default | ||||||||||||||||||||||||||||||||
| 418 | L</getopt_post_process_argv> to croak on invalid arguments, since | ||||||||||||||||||||||||||||||||
| 419 | L<Getopt::Long> will by default just warn to STDERR about unknown arguments. | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | =head2 getopt_load_subcommand | ||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | $code = $app->getopt_load_subcommand($subcommand, [@ARGV]); | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | Takes the subcommand found in the L</getopt_subcommands> list and the command | ||||||||||||||||||||||||||||||||
| 426 | line arguments and must return a CODE block. The default implementation is | ||||||||||||||||||||||||||||||||
| 427 | simply: | ||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | $code = do($subcommand->[1]); | ||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||
| 431 | =head2 getopt_post_process_argv | ||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||
| 433 | $bool = $app->getopt_post_process_argv([@ARGV], {%state}); | ||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||
| 435 | This method can be used to post process the options. C<%state> contains a key | ||||||||||||||||||||||||||||||||
| 436 | "valid" which is true or false, depending on the return value from | ||||||||||||||||||||||||||||||||
| 437 | L<Getopt::Long/GetOptionsFromArray>. | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | This method can C<die> and optionally set C<$!> to avoid calling the function | ||||||||||||||||||||||||||||||||
| 440 | passed to L</run>. | ||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||
| 442 | The default behavior is to check if the first item in C<$argv> starts with a | ||||||||||||||||||||||||||||||||
| 443 | hyphen, and C<die> with an error message if so: | ||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||
| 445 | Invalid argument or argument order: @$argv\n | ||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||
| 447 | =head2 getopt_post_process_exit_value | ||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||
| 449 | $exit_value = $app->getopt_post_process_exit_value($exit_value); | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | A method to be called after the L</run> function has been called. | ||||||||||||||||||||||||||||||||
| 452 | C<$exit_value> holds the return value from L</run> which could be any value, | ||||||||||||||||||||||||||||||||
| 453 | not just 0-255. This value can then be changed to change the exit value from | ||||||||||||||||||||||||||||||||
| 454 | the program. | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | sub getopt_post_process_exit_value ($app, $exit_value) { | ||||||||||||||||||||||||||||||||
| 457 | return int(1 + rand 10); | ||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||
| 460 | =head2 getopt_pre_process_argv | ||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||
| 462 | $app->getopt_pre_process_argv($argv); | ||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||
| 464 | This method can be defined to pre-process C<$argv> before it is passed on to | ||||||||||||||||||||||||||||||||
| 465 | L<Getopt::Long/GetOptionsFromArray>. Example: | ||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||
| 467 | sub getopt_pre_process_argv ($app, $argv) { | ||||||||||||||||||||||||||||||||
| 468 | $app->{first_non_option} = shift @$argv if @$argv and $argv->[0] =~ m!^[a-z]!; | ||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | This method can C<die> and optionally set C<$!> to avoid calling the actual | ||||||||||||||||||||||||||||||||
| 472 | L</run> function. | ||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||
| 474 | =head2 getopt_subcommands | ||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||
| 476 | $subcommands = $app->getopt_subcommands; | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | This method must be defined in the script to enable sub commands. The return | ||||||||||||||||||||||||||||||||
| 479 | value must be either C<undef> to disable subcommands or an array-ref of | ||||||||||||||||||||||||||||||||
| 480 | array-refs like this: | ||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||
| 482 | [["subname", "/abs/path/to/sub-command-script", "help text"], ...] | ||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||
| 484 | The first element in each array-ref "subname" will be matched against the first | ||||||||||||||||||||||||||||||||
| 485 | argument passed to the script, and when matched the "sub-command-script" will | ||||||||||||||||||||||||||||||||
| 486 | be sourced and run inside the same perl process. The sub command script must | ||||||||||||||||||||||||||||||||
| 487 | also use L<Getopt::App> for this to work properly. | ||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||
| 489 | The sub-command will have C<$Getopt::App::SUBCOMMAND> set to the item found in | ||||||||||||||||||||||||||||||||
| 490 | the list. | ||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||
| 492 | Instead of specifying a path, it is also possible to specify a method name, in | ||||||||||||||||||||||||||||||||
| 493 | case you want to include the sub commands inside the current script. Example: | ||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||
| 495 | [["foo", "command_foo", "help text"], ...] | ||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||
| 497 | See L<https://github.com/jhthorsen/getopt-app/tree/main/example> for a working | ||||||||||||||||||||||||||||||||
| 498 | example. | ||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||
| 500 | =head2 getopt_unknown_subcommand | ||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||
| 502 | $exit_value = $app->getopt_unknown_subcommand($argv); | ||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||
| 504 | Will be called when L</getopt_subcommands> is defined but C<$argv> does not | ||||||||||||||||||||||||||||||||
| 505 | match an item in the list. Default behavior is to C<die> with an error message: | ||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||
| 507 | Unknown subcommand: $argv->[0]\n | ||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||
| 509 | Returning C<undef> instead of dying or a number (0-255) will cause the L</run> | ||||||||||||||||||||||||||||||||
| 510 | callback to be called. | ||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||
| 512 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||
| 514 | =head2 capture | ||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||
| 516 | use Getopt::App -capture; | ||||||||||||||||||||||||||||||||
| 517 | my $app = do '/path/to/bin/myapp'; | ||||||||||||||||||||||||||||||||
| 518 | my $array_ref = capture($app, [@ARGV]); # [$stdout, $stderr, $exit_value] | ||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||
| 520 | Used to run an C<$app> and capture STDOUT, STDERR and the exit value in that | ||||||||||||||||||||||||||||||||
| 521 | order in C<$array_ref>. This function will also capture C<die>. C<$@> will be | ||||||||||||||||||||||||||||||||
| 522 | set and captured in the second C<$array_ref> element, and C<$exit_value> will | ||||||||||||||||||||||||||||||||
| 523 | be set to C<$!>. | ||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||
| 525 | This function is a very slimmed down alternative to L<Capture::Tiny/capture>. | ||||||||||||||||||||||||||||||||
| 526 | The main reason why L</capture> exists in this package is that if something | ||||||||||||||||||||||||||||||||
| 527 | inside the C<$app> throws an exception, then it will be part of the captured | ||||||||||||||||||||||||||||||||
| 528 | C<$stderr> instead of making C<capture()> throw an exception. | ||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||
| 530 | L<Capture::Tiny/capture> is however more robust than this function, so please | ||||||||||||||||||||||||||||||||
| 531 | try L<Capture::Tiny> out in case you find an edge case. | ||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||
| 533 | =head2 extract_usage | ||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | # Default to "SYNOPSIS" from current file | ||||||||||||||||||||||||||||||||
| 536 | my $str = extract_usage($section, $file); | ||||||||||||||||||||||||||||||||
| 537 | my $str = extract_usage($section); | ||||||||||||||||||||||||||||||||
| 538 | my $str = extract_usage(); | ||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||
| 540 | Will extract a C<$section> from POD C<$file> and append command line option | ||||||||||||||||||||||||||||||||
| 541 | descriptions when called from inside of L</run>. Command line options can | ||||||||||||||||||||||||||||||||
| 542 | optionally have a description with "spaces-hash-spaces-description", like this: | ||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||
| 544 | run( | ||||||||||||||||||||||||||||||||
| 545 | 'o|option # Some description', | ||||||||||||||||||||||||||||||||
| 546 | 'v|verbose # Enable verbose output', | ||||||||||||||||||||||||||||||||
| 547 | sub { | ||||||||||||||||||||||||||||||||
| 548 | ... | ||||||||||||||||||||||||||||||||
| 549 | }, | ||||||||||||||||||||||||||||||||
| 550 | ); | ||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||
| 552 | This function will I<not> be exported if a function with the same name already | ||||||||||||||||||||||||||||||||
| 553 | exists in the script. | ||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||
| 555 | =head2 new | ||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||
| 557 | my $obj = new($class, %args); | ||||||||||||||||||||||||||||||||
| 558 | my $obj = new($class, \%args); | ||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||
| 560 | This function is exported into the caller package so we can construct a new | ||||||||||||||||||||||||||||||||
| 561 | object: | ||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||
| 563 | my $app = Application::Class->new(\%args); | ||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||
| 565 | This function will I<not> be exported if a function with the same name already | ||||||||||||||||||||||||||||||||
| 566 | exists in the script. | ||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||
| 568 | =head2 run | ||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||
| 570 | # Run a code block on valid @ARGV | ||||||||||||||||||||||||||||||||
| 571 | run(@rules, sub ($app, @extra) { ... }); | ||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||
| 573 | # For testing | ||||||||||||||||||||||||||||||||
| 574 | my $cb = run(@rules, sub ($app, @extra) { ... }); | ||||||||||||||||||||||||||||||||
| 575 | my $exit_value = $cb->([@ARGV]); | ||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||
| 577 | L</run> can be used to call a callback when valid command line options is | ||||||||||||||||||||||||||||||||
| 578 | provided. On invalid arguments, warnings will be issued and the program exit | ||||||||||||||||||||||||||||||||
| 579 | with C<$?> set to 1. | ||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||
| 581 | C<$app> inside the callback is a hash blessed to the caller package. The keys | ||||||||||||||||||||||||||||||||
| 582 | in the hash are the parsed command line options, while C<@extra> is the extra | ||||||||||||||||||||||||||||||||
| 583 | unparsed command line options. | ||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||
| 585 | C<@rules> are the same options as L<Getopt::Long> can take. Example: | ||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||
| 587 | # app.pl -vv --name superwoman -o OptX cool beans | ||||||||||||||||||||||||||||||||
| 588 | run(qw(h|help v+ name=s o=s@), sub ($app, @extra) { | ||||||||||||||||||||||||||||||||
| 589 | die "No help here" if $app->{h}; | ||||||||||||||||||||||||||||||||
| 590 | warn $app->{v}; # 2 | ||||||||||||||||||||||||||||||||
| 591 | warn $app->{name}; # "superwoman" | ||||||||||||||||||||||||||||||||
| 592 | warn @{$app->{o}}; # "OptX" | ||||||||||||||||||||||||||||||||
| 593 | warn @extra; # "cool beans" | ||||||||||||||||||||||||||||||||
| 594 | return 0; # Used as exit code | ||||||||||||||||||||||||||||||||
| 595 | }); | ||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||
| 597 | In the example above, C<@extra> gets populated, since there is a non-flag value | ||||||||||||||||||||||||||||||||
| 598 | "cool" after a list of valid command line options. | ||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||
| 602 | =head2 bundle | ||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||
| 604 | Getopt::App->bundle($path_to_script); | ||||||||||||||||||||||||||||||||
| 605 | Getopt::App->bundle($path_to_script, $fh); | ||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | This method can be used to combine L<Getopt::App> and C<$path_to_script> into a | ||||||||||||||||||||||||||||||||
| 608 | a single script that does not need to have L<Getopt::App> installed from CPAN. | ||||||||||||||||||||||||||||||||
| 609 | This is for example useful for sysadmin scripts that otherwise only depends on | ||||||||||||||||||||||||||||||||
| 610 | core Perl modules. | ||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||
| 612 | The script will be printed to C<$fh>, which defaults to C<STDOUT>. | ||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||
| 614 | Example usage: | ||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||
| 616 | perl -MGetopt::App -e'Getopt::App->bundle(shift)' ./src/my-script.pl > ./bin/my-script; | ||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||
| 618 | =head2 import | ||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||
| 620 | use Getopt::App; | ||||||||||||||||||||||||||||||||
| 621 | use Getopt::App 'My::Script::Base', -signatures; | ||||||||||||||||||||||||||||||||
| 622 | use Getopt::App -capture; | ||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||
| 624 | =over 2 | ||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||
| 626 | =item * Default | ||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||
| 628 | use Getopt::App; | ||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||
| 630 | Passing in no flags will export the default functions L</extract_usage>, | ||||||||||||||||||||||||||||||||
| 631 | L</new> and L</run>. In addition it will save you from a lot of typing, since | ||||||||||||||||||||||||||||||||
| 632 | it will also import the following: | ||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||
| 634 | use strict; | ||||||||||||||||||||||||||||||||
| 635 | use warnings; | ||||||||||||||||||||||||||||||||
| 636 | use utf8; | ||||||||||||||||||||||||||||||||
| 637 | use feature ':5.16'; | ||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||
| 639 | =item * Completion | ||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||
| 641 | use Getopt::App -complete; | ||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||
| 643 | Same as L</Default>, but will also load L<Getopt::App::Complete> and import | ||||||||||||||||||||||||||||||||
| 644 | L<generate_completion_script()|Getopt::App::Complete/generate_completion_script>. | ||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||
| 646 | =item * Signatures | ||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||
| 648 | use Getopt::App -signatures; | ||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||
| 650 | Same as L</Default>, but will also import L<experimental/signatures>. This | ||||||||||||||||||||||||||||||||
| 651 | requires Perl 5.20+. | ||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||
| 653 | =item * Class name | ||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||
| 655 | package My::Script::Foo; | ||||||||||||||||||||||||||||||||
| 656 | use Getopt::App 'My::Script'; | ||||||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||||||
| 658 | Same as L</Default> but will also make C<My::Script::Foo> inherit from | ||||||||||||||||||||||||||||||||
| 659 | L<My::Script>. Note that a package definition is required. | ||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||
| 661 | =item * Capture | ||||||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||||||
| 663 | use Getopt::App -capture; | ||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||
| 665 | This will only export L</capture>. | ||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||
| 667 | =back | ||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||
| 669 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||
| 671 | This library is free software. You can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 672 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||
| 674 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||
| 676 | Jan Henning Thorsen - C<jhthorsen@cpan.org> | ||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||
| 678 | =cut | ||||||||||||||||||||||||||||||||