| 1 | #============================================================================= | ||
| 2 | # | ||
| 3 | # Module: Term::CLI::CommandSet | ||
| 4 | # | ||
| 5 | # Description: Class for sets of (sub-)commands in Term::CLI | ||
| 6 | # | ||
| 7 | # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org> | ||
| 8 | # Created: 05/02/18 | ||
| 9 | # | ||
| 10 | # Copyright (c) 2018 Steven Bakker | ||
| 11 | # | ||
| 12 | # This module is free software; you can redistribute it and/or modify | ||
| 13 | # it under the same terms as Perl itself. See "perldoc perlartistic." | ||
| 14 | # | ||
| 15 | # This software is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| 18 | # | ||
| 19 | #============================================================================= | ||
| 20 | |||
| 21 | use 5.014_001; | ||
| 22 | |||
| 23 | package Term::CLI::Role::CommandSet 0.051003 { | ||
| 24 | |||
| 25 | use Modern::Perl 1.20140107; | ||
| 26 | use Term::CLI::L10N; | ||
| 27 | |||
| 28 | use Types::Standard 1.000005 qw( | ||
| 29 | ArrayRef | ||
| 30 | CodeRef | ||
| 31 | InstanceOf | ||
| 32 | ConsumerOf | ||
| 33 | Maybe | ||
| 34 | ); | ||
| 35 | |||
| 36 | use Moo::Role; | ||
| 37 | use namespace::clean 0.25; | ||
| 38 | |||
| 39 | has parent => ( | ||
| 40 | is => 'rwp', | ||
| 41 | weak_ref => 1, | ||
| 42 | isa => ConsumerOf['Term::CLI::Role::CommandSet'], | ||
| 43 | ); | ||
| 44 | |||
| 45 | has _commands => ( | ||
| 46 | is => 'rw', | ||
| 47 | writer => '_set_commands', | ||
| 48 | init_arg => 'commands', | ||
| 49 | isa => Maybe[ArrayRef[InstanceOf['Term::CLI::Command']]], | ||
| 50 | trigger => 1, | ||
| 51 | coerce => sub { | ||
| 52 | # Copy the array, so the reference we store becomes | ||
| 53 | # "internal", preventing accidental modification | ||
| 54 | # from the outside. | ||
| 55 | return [@{$_[0]}] | ||
| 56 | }, | ||
| 57 | ); | ||
| 58 | |||
| 59 | has callback => ( | ||
| 60 | is => 'rw', | ||
| 61 | isa => Maybe[CodeRef], | ||
| 62 | predicate => 1 | ||
| 63 | ); | ||
| 64 | |||
| 65 | # $self->_set_commands($ref) => $self->_trigger__commands($ref); | ||
| 66 | # | ||
| 67 | # Trigger to run whenever the object's _commands array ref is set. | ||
| 68 | # | ||
| 69 | sub _trigger__commands { | ||
| 70 | my ($self, $arg) = @_; | ||
| 71 | # No need to check for defined-ness of $arg. | ||
| 72 | # The writer method already checks & croaks. | ||
| 73 | for my $cmd (@$arg) { | ||
| 74 | $cmd->_set_parent($self); | ||
| 75 | } | ||
| 76 | }; | ||
| 77 | |||
| 78 | |||
| 79 | sub commands { | ||
| 80 | my $self = shift; | ||
| 81 | my @l = sort { $a->name cmp $b->name } @{$self->_commands // []}; | ||
| 82 | return @l; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | sub has_commands { | ||
| 87 | my $self = shift; | ||
| 88 | return ($self->_commands and scalar @{$self->_commands} > 0); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub add_command { | ||
| 93 | my ($self, @commands) = @_; | ||
| 94 | |||
| 95 | if (!$self->_commands) { | ||
| 96 | $self->_set_commands([]); | ||
| 97 | } | ||
| 98 | |||
| 99 | for my $cmd (@commands) { | ||
| 100 | push @{$self->_commands}, $cmd; | ||
| 101 | $cmd->_set_parent($self); | ||
| 102 | } | ||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub command_names { | ||
| 108 | my $self = shift; | ||
| 109 | return map { $_->name } $self->commands; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub find_matches { | ||
| 114 | my ($self, $partial) = @_; | ||
| 115 | return () if !$self->has_commands; | ||
| 116 | my @found = grep { rindex($_->name, $partial, 0) == 0 } $self->commands; | ||
| 117 | return @found; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | sub root_node { | ||
| 122 | my $curr_node = shift; | ||
| 123 | |||
| 124 | while (my $parent = $curr_node->parent) { | ||
| 125 | $curr_node = $parent; | ||
| 126 | } | ||
| 127 | return $curr_node; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | sub find_command { | ||
| 132 | my ($self, $partial) = @_; | ||
| 133 | my @matches = $self->find_matches($partial); | ||
| 134 | |||
| 135 | if (@matches == 1) { | ||
| 136 | return $matches[0]; | ||
| 137 | } | ||
| 138 | elsif (@matches == 0) { | ||
| 139 | return $self->set_error(loc("unknown command '[_1]'", $partial)); | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | return $self->set_error( | ||
| 143 | loc("ambiguous command '[_1]' (matches: [_2])", | ||
| 144 | $partial, | ||
| 145 | join(', ', sort map {$_->name} @matches) | ||
| 146 | ) | ||
| 147 | ); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub try_callback { | ||
| 153 | my ($self, %args) = @_; | ||
| 154 | |||
| 155 | if ($self->has_callback && defined $self->callback) { | ||
| 156 | return $self->callback->($self, %args); | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | return %args; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | |||
| 167 | __END__ |