| 1 | package App::Sandy::CLI::App; | ||
| 2 | # ABSTRACT: App::Sandy::CLI subclass for command line application interface. | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use Path::Class 'file'; | ||
| 6 | use Pod::Usage; | ||
| 7 | use Try::Tiny; | ||
| 8 | |||
| 9 | extends 'App::Sandy::CLI'; | ||
| 10 | |||
| 11 | with 'App::Sandy::Role::ParseArgv'; | ||
| 12 | |||
| 13 | our $VERSION = '0.25'; # VERSION | ||
| 14 | |||
| 15 | has 'command_stack' => ( | ||
| 16 | traits => ['Array'], | ||
| 17 | is => 'ro', | ||
| 18 | isa => 'ArrayRef[HashRef]', | ||
| 19 | default => sub { [] }, | ||
| 20 | handles => { | ||
| 21 | add_command => 'push', | ||
| 22 | get_command => 'get', | ||
| 23 | map_command => 'map', | ||
| 24 | has_no_command => 'is_empty' | ||
| 25 | } | ||
| 26 | ); | ||
| 27 | |||
| 28 | has 'app_path' => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Str', | ||
| 31 | builder => '_build_app_path' | ||
| 32 | ); | ||
| 33 | |||
| 34 | sub _build_app_path { | ||
| 35 | # Determine dynamic the app path that inherit from this class | ||
| 36 | # If no one is inheriting, return this class path | ||
| 37 | my $class = (caller(1))[3]; | ||
| 38 | $class =~ s/::new//; | ||
| 39 | my $command_pm = file(split /::/ => "$class.pm"); | ||
| 40 | return $INC{$command_pm}; | ||
| 41 | } | ||
| 42 | |||
| 43 | override 'opt_spec' => sub { | ||
| 44 | super | ||
| 45 | }; | ||
| 46 | |||
| 47 | sub command_map_bultin { | ||
| 48 | help => \&help_command, | ||
| 49 | man => \&man_command | ||
| 50 | } | ||
| 51 | |||
| 52 | sub command_map { | ||
| 53 | # It needs to be override | ||
| 54 | } | ||
| 55 | |||
| 56 | sub error { | ||
| 57 | my ($self, $error_msg) = @_; | ||
| 58 | my $sender = $self->_whois; | ||
| 59 | chomp $error_msg; | ||
| 60 | die "$sender: $error_msg\n"; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _whois { | ||
| 64 | my $self = shift; | ||
| 65 | my $sender = $self->progname; | ||
| 66 | my @commands = $self->map_command(sub { $_->{name} }); | ||
| 67 | $sender .= " @commands" unless $self->has_no_command; | ||
| 68 | return $sender; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub _try_msg { | ||
| 72 | my $self = shift; | ||
| 73 | return sprintf "Try '%s --help' for more information" => $self->_whois; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _help_text { | ||
| 77 | my ($self, $path) = @_; | ||
| 78 | $path ||= $self->app_path; | ||
| 79 | pod2usage(-input => $path, -verbose => 99, -sections => ['SYNOPSIS'], -exitval => 0); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _man_text { | ||
| 83 | my ($self, $path) = @_; | ||
| 84 | $path ||= $self->app_path; | ||
| 85 | pod2usage(-input => $path, -verbose => 2, -exitval => 0); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub help_command { | ||
| 89 | my ($self, $argv) = @_; | ||
| 90 | my %command_map = $self->command_map; | ||
| 91 | $self->_dispatcher(\%command_map, $argv); | ||
| 92 | $self->error("Too many arguments: '@$argv'\n" . $self->_try_msg) if @$argv; | ||
| 93 | my $path; | ||
| 94 | $path = $self->get_command(-1)->{path} unless $self->has_no_command; | ||
| 95 | return $self->_help_text($path); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub man_command { | ||
| 99 | my ($self, $argv) = @_; | ||
| 100 | my %command_map = $self->command_map; | ||
| 101 | $self->_dispatcher(\%command_map, $argv); | ||
| 102 | $self->error("Too many arguments: '@$argv'\n" . $self->_try_msg) if @$argv; | ||
| 103 | my $path; | ||
| 104 | $path = $self->get_command(-1)->{path} unless $self->has_no_command; | ||
| 105 | return $self->_man_text($path); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub run_no_command { | ||
| 109 | my ($self, $argv) = @_; | ||
| 110 | my ($opts, $args); | ||
| 111 | |||
| 112 | try { | ||
| 113 | ($opts, $args) = $self->with_parser($argv, $self->opt_spec); | ||
| 114 | } catch { | ||
| 115 | $self->error("$_" . $self->_try_msg); | ||
| 116 | }; | ||
| 117 | |||
| 118 | $self->_help_text if $opts->{help}; | ||
| 119 | $self->_man_text if $opts->{man}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub run_command { | ||
| 123 | my ($self, $argv) = @_; | ||
| 124 | my %command_map = $self->command_map; | ||
| 125 | $self->_dispatcher(\%command_map, $argv); | ||
| 126 | |||
| 127 | my $command = $self->get_command(-1); | ||
| 128 | my $o = $command->{class}->new; | ||
| 129 | |||
| 130 | # $args has at least $argv if no opt has been passed | ||
| 131 | my ($opts, $args) = (undef, $argv); | ||
| 132 | |||
| 133 | if ($o->can('opt_spec')) { | ||
| 134 | try { | ||
| 135 | ($opts, $args) = $self->with_parser($argv, $o->opt_spec); | ||
| 136 | } catch { | ||
| 137 | $self->error("$_" . $self->_try_msg); | ||
| 138 | }; | ||
| 139 | } | ||
| 140 | |||
| 141 | $self->_help_text($command->{path}) if $opts->{help}; | ||
| 142 | $self->_man_text($command->{path}) if $opts->{man}; | ||
| 143 | |||
| 144 | # Deep copy the arguments, just in case the user | ||
| 145 | # manages to mess with | ||
| 146 | my %opts_copy = %$opts; | ||
| 147 | my @args_copy = @$args; | ||
| 148 | |||
| 149 | if ($o->can('validate_args')) { | ||
| 150 | try { | ||
| 151 | $o->validate_args(\@args_copy); | ||
| 152 | } catch { | ||
| 153 | $self->error("$_" . $self->_try_msg); | ||
| 154 | }; | ||
| 155 | } | ||
| 156 | |||
| 157 | if ($o->can('validate_opts')) { | ||
| 158 | try { | ||
| 159 | $o->validate_opts(\%opts_copy); | ||
| 160 | } catch { | ||
| 161 | $self->error("$_" . $self->_try_msg); | ||
| 162 | }; | ||
| 163 | } | ||
| 164 | |||
| 165 | try { | ||
| 166 | $o->execute($opts, $args); | ||
| 167 | } catch { | ||
| 168 | $self->error($_); | ||
| 169 | }; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub _command_loading { | ||
| 173 | my ($self, $command_class) = @_; | ||
| 174 | my $command_pm = file(split /::/ => "$command_class.pm"); | ||
| 175 | |||
| 176 | eval { require $command_pm }; | ||
| 177 | die $@ if $@; | ||
| 178 | |||
| 179 | my $command_class_path = $INC{ $command_pm } | ||
| 180 | or die "$command_class not found in \%INC"; | ||
| 181 | |||
| 182 | return $command_class_path; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub _dispatcher { | ||
| 186 | my ($self, $command_map, $argv) = @_; | ||
| 187 | |||
| 188 | if (@$argv && exists $command_map->{$argv->[0]}) { | ||
| 189 | my $command_name = shift @$argv; | ||
| 190 | my $command_class = $command_map->{$command_name}; | ||
| 191 | my $command_class_path = $self->_command_loading($command_class); | ||
| 192 | |||
| 193 | $self->add_command({ | ||
| 194 | 'name' => $command_name, | ||
| 195 | 'class' => $command_class, | ||
| 196 | 'path' => $command_class_path | ||
| 197 | }); | ||
| 198 | |||
| 199 | unless ($command_class->can('execute')) { | ||
| 200 | die "Not defined method 'execute' for $command_class"; | ||
| 201 | } | ||
| 202 | |||
| 203 | if ($command_class->can('subcommand_map')) { | ||
| 204 | my %command_map = $command_class->subcommand_map; | ||
| 205 | return $self->_dispatcher(\%command_map, $argv); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | sub run { | ||
| 211 | my $self = shift; | ||
| 212 | my @argv = @{ $self->argv }; | ||
| 213 | my %command_map = $self->command_map; | ||
| 214 | my %command_map_bultin = $self->command_map_bultin; | ||
| 215 | |||
| 216 | $self->_help_text unless scalar @argv; | ||
| 217 | |||
| 218 | if ($command_map_bultin{$argv[0]}) { | ||
| 219 | my $command_name = shift @argv; | ||
| 220 | my $command_method = $command_map_bultin{$command_name}; | ||
| 221 | $self->$command_method(\@argv); | ||
| 222 | } elsif ($command_map{$argv[0]}) { | ||
| 223 | $self->run_command(\@argv); | ||
| 224 | } elsif ($argv[0] =~ /^-/) { | ||
| 225 | $self->run_no_command(\@argv); | ||
| 226 | } else { | ||
| 227 | $self->error("Unknown command '$argv[0]'\n" . $self->_try_msg); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | __END__ | ||