| 1 | # ABSTRACT: Base class for pinto commands | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package App::Pinto::Command; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 36 | ▶ | use IO::String; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 36 | ▶ | use Pod::Usage qw(pod2usage); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 36 | ▶ | use App::Cmd::Setup -command; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 36 | our $VERSION = '0.14'; # VERSION | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | ▶ | sub usage_desc { | ||||||||||||||||||||||||||
|
Subroutine
usage_desc: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 0 | my ( $class_or_self, @args ) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | my $class = ref $class_or_self || $class_or_self; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $class_or_self || $class_or_self
MC/DC0%0/2
ref $class_or_self || $class_or_self
ref $class_or_self
$class_or_self
Truth table0%0/3
A || B
A= ref $class_or_selfB= $class_or_self
| |||||||||||||||||||||||||||||
| 25 | 0 | my $file = $class . '.pm'; | |||||||||||||||||||||||||||
| 26 | 0 | $file =~ s{::}{/}xg; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $path = $INC{$file} or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | my $handle = IO::String->new; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | pod2usage( -output => $handle, -input => $path, -exitval => 'NOEXIT' ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | return ${ $handle->string_ref }; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | sub pinto { | ||||||||||||||||||||||||||
|
Subroutine
pinto: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 38 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 39 | 0 | return $self->app->pinto; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub validate_args { | ||||||||||||||||||||||||||
|
Subroutine
validate_args: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | my ( $self, $opts, $args ) = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | $self->usage_error("Arguments are not allowed") | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if @{$args} and not $self->args_attribute; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@{$args;} and not $self->args_attribute
MC/DC0%0/2
@{$args;} and not $self->args_attribute
@{$args;}
not $self->args_attribute
Truth table0%0/3
A and not(B)
A= @{$args;}B= not $self->args_attribute
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | return 1; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ( $self, $opts, $args ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my %args = $self->process_args($opts, $args); | |||||||||||||||||||||||||||
| 59 | 0 | my $result = $self->pinto->run( $self->action_name, %{$opts}, %args ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | return $result->exit_status; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub process_args { | ||||||||||||||||||||||||||
|
Subroutine
process_args: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my ( $self, $opts, $args ) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $attr_name = $self->args_attribute or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ( !@{$args} && $self->args_from_stdin($opts) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @{$args;} and $self->args_from_stdin($opts)
MC/DC0%0/2
not @{$args;} and $self->args_from_stdin($opts)
not @{$args;}
$self->args_from_stdin($opts)
Truth table0%0/3
not(A) and B
A= not @{$args;}B= $self->args_from_stdin($opts)
| |||||||||||||||||||||||||||||
| 72 | 0 | return ( $attr_name => [ _args_from_fh( \*STDIN ) ] ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | return ( $attr_name => $args ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub action_name { | ||||||||||||||||||||||||||
|
Subroutine
action_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $class = ref $self; | |||||||||||||||||||||||||||
| 84 | 0 | my $prefix = $self->command_namespace_prefix(); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $class =~ m/ ^ ${prefix}:: (.+) /mx | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or die "Unable to parse Action name from $class\n"; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Convert foo::bar::baz -> Foo::Bar:Baz | ||||||||||||||||||||||||||||
| 90 | # TODO: consider using a regex to do the conversion | ||||||||||||||||||||||||||||
| 91 | 0 | my $action_name = join '::', map {ucfirst} split '::', $1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return $action_name; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub _args_from_fh { | ||||||||||||||||||||||||||
|
Subroutine
_args_from_fh: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 99 | 0 | my ($fh) = @_; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my @args; | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | chomp $line; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | next if not length $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | next if $line =~ m/^ \s* [;#]/x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | next if $line !~ m/\S/x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | push @args, $line; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | return @args; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub args_attribute { return '' } | ||||||||||||||||||||||||||
|
Subroutine
args_attribute: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub args_from_stdin { return 0 } | ||||||||||||||||||||||||||
|
Subroutine
args_from_stdin: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub command_namespace_prefix { return __PACKAGE__ } | ||||||||||||||||||||||||||
|
Subroutine
command_namespace_prefix: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 126 | 36 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||