App-CLI-Extension
view release on metacpan or search on metacpan
lib/App/CLI/Extension.pm view on Meta::CPAN
name => "kurt",
favorite_group => "nirvana",
favorite_song => ["Lounge Act", "Negative Creep", "Radio Friendly Unit Shifter", "You Know You're Right"]
);
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
print "My name is " . $self->config->{name} . "\n";
print "My favorite group is " . $self->config->{favorite_group} . "\n";
print "My favorite song is " . join(",", @{$self->config->{favorite_song}});
print " and Smells Like Teen Spirit\n"
}
# myapp
#!/usr/bin/perl
use strict;
use MyApp;
MyApp->dispatch;
# execute
[kurt@localhost ~] myapp hello
My name is kurt
My favorite group is nirvana
My favorite song is Lounge Act,Negative Creep,Radio Friendly Unit Shifter,You Know You're Right and Smells Like Teen Spirit
=cut
sub config {
my($class, %config) = @_;
$class->_config(\%config);
return $class->_config;
}
=head1 COMPONENT METHOD
=head2 argv0
my script name
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use feature ":5.10.0";
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
say "my script name is " . $self->argv0;
}
1;
# execute
[kurt@localhost ~] myapp hello
my script name is myapp
=head2 full_argv0
my script fullname
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use feature ":5.10.0";
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
say "my script full name is " . $self->full_argv0;
}
1;
# execute
[kurt@localhost ~] myapp hello
my script name is /home/kurt/myapp
=head2 cmdline
my execute cmdline string
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use feature ":5.10.0";
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
say "my script cmdline is [" . $self->cmdline . "]";
}
1;
# execute
[kurt@localhost ~] myapp hello --verbose --num=10
my script cmdline is [/home/kurt/myapp hello --verbose --num=10]
=head2 orig_argv
my execute script original argv
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use feature ":5.10.0";
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
say "my script original argv is [" join(", ", @{$self->orig_argv}) . "]";
}
1;
# execute
[kurt@localhost ~] myapp hello --verbose --num=10
my script original argv is [hello,--verbose, --num=10]
=head2 stash
like global variable in Command package
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use feature ":5.10.0";
use base qw(App::CLI::Command);
sub run {
my($self, @args) = @_;
$self->stash->{name} = "kurt";
say "stash value: " . $self->stash->{name};
}
1;
=head2 new_callback
install new callback phase
Example:
$self->new_callback("some_phase");
# registered callback argument pattern
$self->new_callback("some_phase", sub { $self = shift; "anything to do..." });
=head2 add_callback
install callback
Example:
$self->add_callback("some_phase", sub { my $self = shift; say "some_phase method No.1" });
$self->add_callback("some_phase", sub { my $self = shift; say "some_phase method No.1" });
$self->add_callback("any_phase", sub {
my($self, @args) = @_;
say "any_phase args: @args";
});
=cut
=head2 exec_callback
execute callback
Example:
$self->execute_callback("some_phase");
# some_phase method method No.1
# some_phase method method No.2
$self->execute_callback("any_phase", qw(one two three));
# any_phase args: one two three
=head2 exists_callback
exists callback check
Example:
if ($self->exists_callback("some_phase")) {
$self->exec_callback("some_phase");
} else {
die "some_phase is not exists callback phase";
}
=head2 exit_value
set exit value
Example:
# program exit value is 1(ex. echo $?)
$self->exit_value(1);
=head2 finished
setup or prepare phase and 1 set, run and postrun phase will not run. default 0
Example:
# MyApp/Hello.pm
package MyApp::Hello;
use strict;
use base qw(App::CLI::Command);
sub prerun {
my($self, @args) = @_;
$self->finished(1);
}
# non execute
sub run {
my($self, @args) = @_;
( run in 0.316 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )