| 1 | package MooX::Cmd; | ||||||||||
| 2 | # ABSTRACT: Giving an easy Moo style way to make command organized CLI apps | ||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 8 | our $VERSION = '1.001'; | |||||||||
| 6 | |||||||||||
| 7 | 8 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 8 | ▶ | use Package::Stash; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | sub import | ||||||||||
| 11 | 26 | ▶ | { | ||||||||
|
Subroutine
import: called CC=8 SCAR=20.8
| |||||||||||
| 12 | 26 | my (undef, %import_options) = @_; | |||||||||
| 13 | 26 | my $caller = caller; | |||||||||
| 14 | 26 | my @caller_isa; | |||||||||
| 15 | ## no critic qw(ProhibitNoStrict) | ||||||||||
| 16 | 8 | ▶ | { no strict 'refs'; @caller_isa = @{"${caller}::ISA"} }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | #don't add this to a role | ||||||||||
| 19 | #ISA of a role is always empty ! | ||||||||||
| 20 | 26 | ▶ | @caller_isa or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | |||||||||||
| 22 | 26 | my $execute_return_method_name = $import_options{execute_return_method_name}; | |||||||||
| 23 | |||||||||||
| 24 | 26 | ▶ | exists $import_options{execute_from_new} or $import_options{execute_from_new} = 1; # set default until we want other way | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | |||||||||||
| 26 | 26 | my $stash = Package::Stash->new($caller); | |||||||||
| 27 | defined $import_options{execute_return_method_name} | ||||||||||
| 28 | and $stash->add_symbol('&' . $import_options{execute_return_method_name}, | ||||||||||
| 29 | 26 | ▶ | sub { shift->{$import_options{execute_return_method_name}} }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||
| 30 | 26 | ▶ | defined $import_options{creation_method_name} or $import_options{creation_method_name} = "new_with_cmd"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 26 | ▶ | $stash->add_symbol('&' . $import_options{creation_method_name}, sub { shift->_initialize_from_cmd(@_); }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 26 | ▶ | my $apply_modifiers = sub { | ||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||
| 34 | 26 | ▶ | $caller->can('_initialize_from_cmd') and return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 25 | my $with = $caller->can('with'); | |||||||||
| 36 | 25 | $with->('MooX::Cmd::Role'); | |||||||||
| 37 | # XXX prove whether it can chained ... | ||||||||||
| 38 | 25 | ▶ | if ($import_options{with_config_from_file}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | { | ||||||||||
| 40 | 6 | ▶ | eval { require MooX::ConfigFromFile::Role; 1 } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | or croak "MooX::Cmd option with_config_from_file requires MooX::ConfigFromFile, please install it: $@"; | ||||||||||
| 42 | 5 | $with->('MooX::ConfigFromFile::Role'); | |||||||||
| 43 | } | ||||||||||
| 44 | 24 | ▶ | $import_options{with_config_from_file} and $with->('MooX::Cmd::Role::ConfigFromFile'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 24 | ▶ | $import_options{with_abbrev_cmds} and $with->('MooX::Cmd::Role::AbbrevCmds'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 26 | }; | |||||||||
| 47 | 26 | $apply_modifiers->(); | |||||||||
| 48 | |||||||||||
| 49 | 25 | my %default_modifiers = ( | |||||||||
| 50 | base => '_build_command_base', | ||||||||||
| 51 | execute_method_name => '_build_command_execute_method_name', | ||||||||||
| 52 | execute_return_method_name => '_build_command_execute_return_method_name', | ||||||||||
| 53 | creation_chain_methods => '_build_command_creation_chain_methods', | ||||||||||
| 54 | creation_method_name => '_build_command_creation_method_name', | ||||||||||
| 55 | execute_from_new => '_build_command_execute_from_new', | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | 25 | my $around; | |||||||||
| 59 | 25 | foreach my $opt_key (keys %default_modifiers) | |||||||||
| 60 | { | ||||||||||
| 61 | 150 | ▶ | exists $import_options{$opt_key} or next; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 51 | ▶ | $around or $around = $caller->can('around'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 51 | ▶ | $around->($default_modifiers{$opt_key} => sub { $import_options{$opt_key} }); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 25 | return; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 8 | 1; | |||||||||
| 70 | |||||||||||
| 71 | __END__ | ||||||||||