/usr/share/perl5/App/Cmd/Simple.pm is in libapp-cmd-perl 0.331-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 332 333 334 335 336 337 338 339 340 341 342 343 | use strict;
use warnings;
package App::Cmd::Simple;
$App::Cmd::Simple::VERSION = '0.331';
use App::Cmd::Command;
BEGIN { our @ISA = 'App::Cmd::Command' }
# ABSTRACT: a helper for building one-command App::Cmd applications
use App::Cmd;
use Sub::Install;
#pod =head1 SYNOPSIS
#pod
#pod in F<simplecmd>:
#pod
#pod use YourApp::Cmd;
#pod Your::Cmd->run;
#pod
#pod in F<YourApp/Cmd.pm>:
#pod
#pod package YourApp::Cmd;
#pod use base qw(App::Cmd::Simple);
#pod
#pod sub opt_spec {
#pod return (
#pod [ "blortex|X", "use the blortex algorithm" ],
#pod [ "recheck|r", "recheck all results" ],
#pod );
#pod }
#pod
#pod sub validate_args {
#pod my ($self, $opt, $args) = @_;
#pod
#pod # no args allowed but options!
#pod $self->usage_error("No args allowed") if @$args;
#pod }
#pod
#pod sub execute {
#pod my ($self, $opt, $args) = @_;
#pod
#pod my $result = $opt->{blortex} ? blortex() : blort();
#pod
#pod recheck($result) if $opt->{recheck};
#pod
#pod print $result;
#pod }
#pod
#pod and, finally, at the command line:
#pod
#pod knight!rjbs$ simplecmd --recheck
#pod
#pod All blorts successful.
#pod
#pod =head1 SUBCLASSING
#pod
#pod When writing a subclass of App::Cmd:Simple, there are only a few methods that
#pod you might want to implement. They behave just like the same-named methods in
#pod App::Cmd.
#pod
#pod =head2 opt_spec
#pod
#pod This method should be overridden to provide option specifications. (This is
#pod list of arguments passed to C<describe_options> from Getopt::Long::Descriptive,
#pod after the first.)
#pod
#pod If not overridden, it returns an empty list.
#pod
#pod =head2 usage_desc
#pod
#pod This method should be overridden to provide the top level usage line.
#pod It's a one-line summary of how the command is to be invoked, and
#pod should be given in the format used for the C<$usage_desc> parameter to
#pod C<describe_options> in Getopt::Long::Descriptive.
#pod
#pod If not overridden, it returns something that prints out like:
#pod
#pod yourapp [-?h] [long options...]
#pod
#pod =head2 validate_args
#pod
#pod $cmd->validate_args(\%opt, \@args);
#pod
#pod This method is passed a hashref of command line options (as processed by
#pod Getopt::Long::Descriptive) and an arrayref of leftover arguments. It may throw
#pod an exception (preferably by calling C<usage_error>) if they are invalid, or it
#pod may do nothing to allow processing to continue.
#pod
#pod =head2 execute
#pod
#pod Your::App::Cmd::Simple->execute(\%opt, \@args);
#pod
#pod This method does whatever it is the command should do! It is passed a hash
#pod reference of the parsed command-line options and an array reference of left
#pod over arguments.
#pod
#pod =cut
# The idea here is that the user will someday replace "Simple" in his ISA with
# "Command" and then write a standard App::Cmd package. To make that possible,
# we produce a behind-the-scenes App::Cmd object when the user says 'use
# MyApp::Simple' and redirect MyApp::Simple->run to that.
my $i;
BEGIN { $i = 0 }
sub import {
my ($class) = @_;
return if $class eq __PACKAGE__;
# This signals that something has already set the target up.
return $class if $class->_cmd_pkg;
my $core_execute = App::Cmd::Command->can('execute');
my $our_execute = $class->can('execute');
Carp::confess(
"App::Cmd::Simple subclasses must implement ->execute, not ->run"
) unless $our_execute and $our_execute != $core_execute;
# I doubt the $i will ever be needed, but let's start paranoid.
my $generated_name = join('::', $class, '_App_Cmd', $i++);
{
no strict 'refs';
*{$generated_name . '::ISA'} = [ 'App::Cmd' ];
}
Sub::Install::install_sub({
into => $class,
as => '_cmd_pkg',
code => sub { $generated_name },
});
Sub::Install::install_sub({
into => $class,
as => 'command_names',
code => sub { 'only' },
});
Sub::Install::install_sub({
into => $generated_name,
as => '_plugins',
code => sub { $class },
});
Sub::Install::install_sub({
into => $generated_name,
as => 'default_command',
code => sub { 'only' },
});
Sub::Install::install_sub({
into => $generated_name,
as => '_cmd_from_args',
code => sub {
my ($self, $args) = @_;
if (defined(my $command = $args->[0])) {
my $plugin = $self->plugin_for($command);
# If help was requested, show the help for the command, not the
# main help. Because the main help would talk about subcommands,
# and a "Simple" app has no subcommands.
if ($plugin and $plugin eq $self->plugin_for("help")) {
return ($command, [ $self->default_command ]);
}
# Any other value for "command" isn't really a command at all --
# it's the first argument. So call the default command instead.
}
return ($self->default_command, $args);
},
});
Sub::Install::install_sub({
into => $class,
as => 'run',
code => sub {
$generated_name->new({
no_help_plugin => 0,
no_commands_plugin => 1,
})->run(@_);
}
});
return $class;
}
sub usage_desc {
return "%c %o"
}
sub _cmd_pkg { }
#pod =head1 WARNINGS
#pod
#pod B<This should be considered experimental!> Although it is probably not going
#pod to change much, don't build your business model around it yet, okay?
#pod
#pod App::Cmd::Simple is not rich in black magic, but it does do some somewhat
#pod gnarly things to make an App::Cmd::Simple look as much like an
#pod App::Cmd::Command as possible. This means that you can't deviate too much from
#pod the sort of thing shown in the synopsis as you might like. If you're doing
#pod something other than writing a fairly simple command, and you want to screw
#pod around with the App::Cmd-iness of your program, Simple might not be the best
#pod choice.
#pod
#pod B<One specific warning...> if you are writing a program with the
#pod App::Cmd::Simple class embedded in it, you B<must> call import on the class.
#pod That's how things work. You can just do this:
#pod
#pod YourApp::Cmd->import->run;
#pod
#pod =cut
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
App::Cmd::Simple - a helper for building one-command App::Cmd applications
=head1 VERSION
version 0.331
=head1 SYNOPSIS
in F<simplecmd>:
use YourApp::Cmd;
Your::Cmd->run;
in F<YourApp/Cmd.pm>:
package YourApp::Cmd;
use base qw(App::Cmd::Simple);
sub opt_spec {
return (
[ "blortex|X", "use the blortex algorithm" ],
[ "recheck|r", "recheck all results" ],
);
}
sub validate_args {
my ($self, $opt, $args) = @_;
# no args allowed but options!
$self->usage_error("No args allowed") if @$args;
}
sub execute {
my ($self, $opt, $args) = @_;
my $result = $opt->{blortex} ? blortex() : blort();
recheck($result) if $opt->{recheck};
print $result;
}
and, finally, at the command line:
knight!rjbs$ simplecmd --recheck
All blorts successful.
=head1 SUBCLASSING
When writing a subclass of App::Cmd:Simple, there are only a few methods that
you might want to implement. They behave just like the same-named methods in
App::Cmd.
=head2 opt_spec
This method should be overridden to provide option specifications. (This is
list of arguments passed to C<describe_options> from Getopt::Long::Descriptive,
after the first.)
If not overridden, it returns an empty list.
=head2 usage_desc
This method should be overridden to provide the top level usage line.
It's a one-line summary of how the command is to be invoked, and
should be given in the format used for the C<$usage_desc> parameter to
C<describe_options> in Getopt::Long::Descriptive.
If not overridden, it returns something that prints out like:
yourapp [-?h] [long options...]
=head2 validate_args
$cmd->validate_args(\%opt, \@args);
This method is passed a hashref of command line options (as processed by
Getopt::Long::Descriptive) and an arrayref of leftover arguments. It may throw
an exception (preferably by calling C<usage_error>) if they are invalid, or it
may do nothing to allow processing to continue.
=head2 execute
Your::App::Cmd::Simple->execute(\%opt, \@args);
This method does whatever it is the command should do! It is passed a hash
reference of the parsed command-line options and an array reference of left
over arguments.
=head1 WARNINGS
B<This should be considered experimental!> Although it is probably not going
to change much, don't build your business model around it yet, okay?
App::Cmd::Simple is not rich in black magic, but it does do some somewhat
gnarly things to make an App::Cmd::Simple look as much like an
App::Cmd::Command as possible. This means that you can't deviate too much from
the sort of thing shown in the synopsis as you might like. If you're doing
something other than writing a fairly simple command, and you want to screw
around with the App::Cmd-iness of your program, Simple might not be the best
choice.
B<One specific warning...> if you are writing a program with the
App::Cmd::Simple class embedded in it, you B<must> call import on the class.
That's how things work. You can just do this:
YourApp::Cmd->import->run;
=head1 AUTHOR
Ricardo Signes <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 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
|