| 1 | package CLI::Framework::Command::Alias; | ||
| 2 | use base qw( CLI::Framework::Command::Meta ); | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = 0.01; | ||
| 8 | |||
| 9 | #------- | ||
| 10 | |||
| 11 | sub usage_text { | ||
| 12 | q{ | ||
| 13 | alias [<cmd-name>]: show command aliases | ||
| 14 | [and subcommand aliases for <cmd-name>, if given] | ||
| 15 | |||
| 16 | ARGUMENTS | ||
| 17 | <cmd-name>: if specified, show aliases for this command only and show | ||
| 18 | its subcommand aliases | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | sub run { | ||
| 23 | my ($self, $opts, @args) = @_; | ||
| 24 | |||
| 25 | my $app = $self->get_app(); | ||
| 26 | my %cmd_alias_to_name = $app->command_alias(); | ||
| 27 | my $cmd = shift @args; | ||
| 28 | |||
| 29 | # Ignore non-interactive commands while in interactive mode... | ||
| 30 | if( $app->get_interactivity_mode() ) { | ||
| 31 | while( my ($k,$v) = each %cmd_alias_to_name ) { | ||
| 32 | if( ! $app->is_interactive_command( $v ) ) { | ||
| 33 | delete $cmd_alias_to_name{ $k }; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | } | ||
| 37 | # Alias command only recognizes one argument: a top-level command... | ||
| 38 | if( $cmd ) { | ||
| 39 | # Recognize alias requests by alias... | ||
| 40 | $cmd = $cmd_alias_to_name{$cmd} if exists $cmd_alias_to_name{$cmd}; | ||
| 41 | |||
| 42 | # Silently pass if invalid command... | ||
| 43 | return unless $app->is_valid_command_name( $cmd ); | ||
| 44 | |||
| 45 | # Formatted display of aliases to specific command... | ||
| 46 | my $summary = $self->_cmd_alias_hash_to_summary( | ||
| 47 | \%cmd_alias_to_name, | ||
| 48 | target => $cmd | ||
| 49 | ); | ||
| 50 | # Formatted display of aliases to subcommand... | ||
| 51 | my $cmd_object = $app->registered_command_object( $cmd ) | ||
| 52 | || $app->register_command( $cmd ); | ||
| 53 | my %subcommand_alias = $cmd_object->subcommand_alias(); | ||
| 54 | my $subcommand_summary = $self->_cmd_alias_hash_to_summary( | ||
| 55 | \%subcommand_alias, | ||
| 56 | ); | ||
| 57 | if( $subcommand_summary ) { | ||
| 58 | $summary .= sprintf( "\n%15s '%s':\n", 'SUBCOMMANDS of command', $cmd ); | ||
| 59 | $summary .= sprintf( "\n%s", $subcommand_summary ); | ||
| 60 | } | ||
| 61 | return $summary; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | # Formatted display of all aliases... | ||
| 65 | my $summary = $self->_cmd_alias_hash_to_summary( | ||
| 66 | \%cmd_alias_to_name, | ||
| 67 | ); | ||
| 68 | return $summary; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _cmd_alias_hash_to_summary { | ||
| 73 | my ($self, $aliases, %param) = @_; | ||
| 74 | |||
| 75 | my $target = $param{target}; | ||
| 76 | |||
| 77 | my %name_to_alias_set; | ||
| 78 | while( my ($alias, $name) = each %$aliases ) { | ||
| 79 | next if $alias =~ /^\d+$/; # ignore numerical aliases | ||
| 80 | next if $target && $name ne $target; | ||
| 81 | push @{ $name_to_alias_set{$name} }, $alias; | ||
| 82 | } | ||
| 83 | return $self->format_name_to_aliases_hash( \%name_to_alias_set ); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub format_name_to_aliases_hash { | ||
| 87 | my ($self, $h, $indent) = @_; | ||
| 88 | |||
| 89 | $indent ||= 10; | ||
| 90 | my $format = '%'.$indent."s: %s\n"; | ||
| 91 | |||
| 92 | my @output; | ||
| 93 | for my $command (keys %$h) { | ||
| 94 | push @output, sprintf | ||
| 95 | $format, $command, join( ', ', @{$h->{$command}} ); | ||
| 96 | } | ||
| 97 | my @output_sorted = sort { | ||
| 98 | my $name_a = substr( $a, index($a, ':') ); | ||
| 99 | my $name_b = substr( $b, index($b, ':') ); | ||
| 100 | $name_a cmp $name_b; | ||
| 101 | } @output; | ||
| 102 | return join( '', @output ); | ||
| 103 | } | ||
| 104 | |||
| 105 | __END__ |