File: commands.pm

package info (click to toggle)
libapp-cmd-perl 0.339-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 556 kB
  • sloc: perl: 1,919; makefile: 8; sh: 3
file content (331 lines) | stat: -rw-r--r-- 8,400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use strict;
use warnings;

package App::Cmd::Command::commands 0.339;

use App::Cmd::Command;
BEGIN { our @ISA = 'App::Cmd::Command' };

# ABSTRACT: list the application's commands

#pod =head1 DESCRIPTION
#pod
#pod This command will list all of the application commands available and their
#pod abstracts.
#pod
#pod =method execute
#pod
#pod This is the command's primary method and raison d'etre.  It prints the
#pod application's usage text (if any) followed by a sorted listing of the
#pod application's commands and their abstracts.
#pod
#pod The commands are printed in sorted groups (created by C<sort_commands>); each
#pod group is set off by blank lines.
#pod
#pod =cut

sub opt_spec {
  return (
    [ 'stderr' => 'hidden' ],
    [ 'for-completion',   'one per line, for use in tab completion scripts' ],
    [ 'bash-completion',  'output a bash completion script for this application' ],
    [ 'zsh-completion',   'output a zsh completion script for this application' ],
  );
}

sub execute {
  my ($self, $opt, $args) = @_;

  my $target = $opt->stderr ? *STDERR : *STDOUT;

  my @cmd_groups = $self->app->command_groups;
  my @primary_commands = map { @$_ if ref $_ } @cmd_groups;

  if (!@cmd_groups) {
    @primary_commands =
      grep { $_ ne 'version' or $self->app->{show_version} }
      map { ($_->command_names)[0] }
      $self->app->command_plugins;

    @cmd_groups = $self->sort_commands(@primary_commands);
  }

  if ($opt->for_completion) {
    print "$_\n" for map {; @$_ } @cmd_groups;
    return;
  }

  if ($opt->bash_completion) {
    $self->_print_bash_completion(\@cmd_groups);
    return;
  }

  if ($opt->zsh_completion) {
    $self->_print_zsh_completion(\@cmd_groups);
    return;
  }

  my $fmt_width = 0;
  for (@primary_commands) { $fmt_width = length if length > $fmt_width }
  $fmt_width += 2; # pretty

  foreach my $cmd_set (@cmd_groups) {
    if (!ref $cmd_set) {
      print { $target } "$cmd_set:\n";
      next;
    }
    for my $command (@$cmd_set) {
      my $abstract = $self->app->plugin_for($command)->abstract;
      printf { $target } "%${fmt_width}s: %s\n", $command, $abstract;
    }
    print { $target } "\n";
  }
}

#pod =method C<sort_commands>
#pod
#pod   my @sorted = $cmd->sort_commands(@unsorted);
#pod
#pod This method orders the list of commands into groups which it returns as a list of
#pod arrayrefs, and optional group header strings.
#pod
#pod By default, the first group is for the "help" and "commands" commands, and all
#pod other commands are in the second group.
#pod
#pod This method can be overridden by implementing the C<commands_groups> method in
#pod your application base clase.
#pod
#pod =cut

sub _print_bash_completion {
  my ($self, $cmd_groups) = @_;

  die "--bash-completion requires a version of Getopt::Long::Descriptive "
    . "that supports shell completion generation\n"
    unless Getopt::Long::Descriptive->can('_completion_for_bash');

  my $app  = $self->app;
  my $prog = $app->arg0;
  (my $func = "_${prog}_complete") =~ s/\W/_/g;

  my @all_cmds = map {; @$_ } @$cmd_groups;

  my %cmd_completion;
  for my $cmd (@all_cmds) {
    my $plugin = $app->plugin_for($cmd) or next;
    $cmd_completion{$cmd} =
      Getopt::Long::Descriptive::_completion_for_bash($plugin->opt_spec);
  }

  my $cmds_str = join q{ }, @all_cmds;

  print <<"END_HEADER";
$func() {
    local cur prev words cword
    _init_completion 2>/dev/null || {
        cur="\${COMP_WORDS[COMP_CWORD]}"
        prev="\${COMP_WORDS[COMP_CWORD-1]}"
    }
    words=("\${COMP_WORDS[\@]}")
    cword=\$COMP_CWORD

    local cmd=""
    local i
    for ((i=1; i < cword; i++)); do
        if [[ "\${words[i]}" != -* ]]; then
            cmd="\${words[i]}"
            break
        fi
    done

    if [[ -z "\$cmd" ]]; then
        COMPREPLY=(\$(compgen -W "$cmds_str" -- "\$cur"))
        return
    fi

    case "\$cmd" in
END_HEADER

  for my $cmd (sort keys %cmd_completion) {
    my $completion = $cmd_completion{$cmd};
    my $flags_str = $completion->{flags};

    next unless $flags_str || @{ $completion->{prev_cases} };

    print "        $cmd)\n";

    if (@{ $completion->{prev_cases} }) {
      print "            case \"\$prev\" in\n";
      for my $case (@{ $completion->{prev_cases} }) {
        print "                $case->{pattern})\n";
        print "                    $case->{action}\n";
        print "                    return ;;\n";
      }
      print "            esac\n";
    }

    print "            COMPREPLY=(\$(compgen -W \"$flags_str\" -- \"\$cur\"))\n";
    print "            ;;\n";
  }

  print <<"END_FOOTER";
        *)
            COMPREPLY=()
            ;;
    esac
}
complete -F $func $prog
END_FOOTER
}

sub _print_zsh_completion {
  my ($self, $cmd_groups) = @_;

  die "--zsh-completion requires a version of Getopt::Long::Descriptive "
    . "that supports shell completion generation\n"
    unless Getopt::Long::Descriptive->can('_completion_for_zsh');

  my $app  = $self->app;
  my $prog = $app->arg0;
  (my $func = "_${prog}_complete") =~ s/\W/_/g;

  my @all_cmds = map {; @$_ } @$cmd_groups;

  my @cmd_descs;
  my %cmd_zsh_args;
  for my $cmd (@all_cmds) {
    my $plugin = $app->plugin_for($cmd) or next;
    (my $abstract = $plugin->abstract) =~ s/'/'\\''/g;
    push @cmd_descs, "        '$cmd:$abstract'";
    $cmd_zsh_args{$cmd} = [ Getopt::Long::Descriptive::_completion_for_zsh($plugin->opt_spec) ];
  }

  my $cmd_list = join "\n", @cmd_descs;

  print <<"END_HEADER";
#compdef $prog

$func() {
    local curcontext="\$curcontext" state line
    typeset -A opt_args

    _arguments -C \\
        '1: :->command' \\
        '*:: :->args'

    case \$state in
        command)
            local -a _cmds
            _cmds=(
$cmd_list
            )
            _describe 'command' _cmds
            ;;
        args)
            case \$line[1] in
END_HEADER

  for my $cmd (sort keys %cmd_zsh_args) {
    my @args = @{ $cmd_zsh_args{$cmd} };
    print "                $cmd)\n";
    if (@args) {
      print "                    _arguments \\\n";
      for my $i (0 .. $#args) {
        my $cont = $i < $#args ? ' \\' : '';
        print "                        $args[$i]$cont\n";
      }
    } else {
      print "                    _arguments\n";
    }
    print "                    ;;\n";
  }

  print <<"END_FOOTER";
                *)
                    ;;
            esac
            ;;
    esac
}
$func "\$\@"
END_FOOTER
}

sub sort_commands {
  my ($self, @commands) = @_;

  my $float = qr/^(?:help|commands)$/;

  my @head = sort grep { $_ =~ $float } @commands;
  my @tail = sort grep { $_ !~ $float } @commands;

  return (\@head, \@tail);
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

App::Cmd::Command::commands - list the application's commands

=head1 VERSION

version 0.339

=head1 DESCRIPTION

This command will list all of the application commands available and their
abstracts.

=head1 PERL VERSION

This library should run on perls released even a long time ago.  It should
work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to
lower the minimum required perl.

=head1 METHODS

=head2 execute

This is the command's primary method and raison d'etre.  It prints the
application's usage text (if any) followed by a sorted listing of the
application's commands and their abstracts.

The commands are printed in sorted groups (created by C<sort_commands>); each
group is set off by blank lines.

=head2 C<sort_commands>

  my @sorted = $cmd->sort_commands(@unsorted);

This method orders the list of commands into groups which it returns as a list of
arrayrefs, and optional group header strings.

By default, the first group is for the "help" and "commands" commands, and all
other commands are in the second group.

This method can be overridden by implementing the C<commands_groups> method in
your application base clase.

=head1 AUTHOR

Ricardo Signes <cpan@semiotic.systems>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Ricardo Signes.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut