1App::Cmd::Simple(3) User Contributed Perl Documentation App::Cmd::Simple(3)
2
3
4
6 App::Cmd::Simple - a helper for building one-command App::Cmd
7 applications
8
10 version 0.331
11
13 in simplecmd:
14
15 use YourApp::Cmd;
16 Your::Cmd->run;
17
18 in YourApp/Cmd.pm:
19
20 package YourApp::Cmd;
21 use base qw(App::Cmd::Simple);
22
23 sub opt_spec {
24 return (
25 [ "blortex|X", "use the blortex algorithm" ],
26 [ "recheck|r", "recheck all results" ],
27 );
28 }
29
30 sub validate_args {
31 my ($self, $opt, $args) = @_;
32
33 # no args allowed but options!
34 $self->usage_error("No args allowed") if @$args;
35 }
36
37 sub execute {
38 my ($self, $opt, $args) = @_;
39
40 my $result = $opt->{blortex} ? blortex() : blort();
41
42 recheck($result) if $opt->{recheck};
43
44 print $result;
45 }
46
47 and, finally, at the command line:
48
49 knight!rjbs$ simplecmd --recheck
50
51 All blorts successful.
52
54 When writing a subclass of App::Cmd:Simple, there are only a few
55 methods that you might want to implement. They behave just like the
56 same-named methods in App::Cmd.
57
58 opt_spec
59 This method should be overridden to provide option specifications.
60 (This is list of arguments passed to "describe_options" from
61 Getopt::Long::Descriptive, after the first.)
62
63 If not overridden, it returns an empty list.
64
65 usage_desc
66 This method should be overridden to provide the top level usage line.
67 It's a one-line summary of how the command is to be invoked, and should
68 be given in the format used for the $usage_desc parameter to
69 "describe_options" in Getopt::Long::Descriptive.
70
71 If not overriden, it returns something that prints out like:
72
73 yourapp [-?h] [long options...]
74
75 validate_args
76 $cmd->validate_args(\%opt, \@args);
77
78 This method is passed a hashref of command line options (as processed
79 by Getopt::Long::Descriptive) and an arrayref of leftover arguments.
80 It may throw an exception (preferably by calling "usage_error") if they
81 are invalid, or it may do nothing to allow processing to continue.
82
83 execute
84 Your::App::Cmd::Simple->execute(\%opt, \@args);
85
86 This method does whatever it is the command should do! It is passed a
87 hash reference of the parsed command-line options and an array
88 reference of left over arguments.
89
91 This should be considered experimental! Although it is probably not
92 going to change much, don't build your business model around it yet,
93 okay?
94
95 App::Cmd::Simple is not rich in black magic, but it does do some
96 somewhat gnarly things to make an App::Cmd::Simple look as much like an
97 App::Cmd::Command as possible. This means that you can't deviate too
98 much from the sort of thing shown in the synopsis as you might like.
99 If you're doing something other than writing a fairly simple command,
100 and you want to screw around with the App::Cmd-iness of your program,
101 Simple might not be the best choice.
102
103 One specific warning... if you are writing a program with the
104 App::Cmd::Simple class embedded in it, you must call import on the
105 class. That's how things work. You can just do this:
106
107 YourApp::Cmd->import->run;
108
110 Ricardo Signes <rjbs@cpan.org>
111
113 This software is copyright (c) 2016 by Ricardo Signes.
114
115 This is free software; you can redistribute it and/or modify it under
116 the same terms as the Perl 5 programming language system itself.
117
118
119
120perl v5.30.0 2019-07-26 App::Cmd::Simple(3)