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.311
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 validate_args
66 $cmd->validate_args(\%opt, \@args);
67
68 This method is passed a hashref of command line options (as processed
69 by Getopt::Long::Descriptive) and an arrayref of leftover arguments.
70 It may throw an exception (preferably by calling "usage_error") if they
71 are invalid, or it may do nothing to allow processing to continue.
72
73 execute
74 Your::App::Cmd::Simple->execute(\%opt, \@args);
75
76 This method does whatever it is the command should do! It is passed a
77 hash reference of the parsed command-line options and an array
78 reference of left over arguments.
79
81 This should be considered experimental! Although it is probably not
82 going to change much, don't build your business model around it yet,
83 okay?
84
85 App::Cmd::Simple is not rich in black magic, but it does do some
86 somewhat gnarly things to make an App::Cmd::Simple look as much like an
87 App::Cmd::Command as possible. This means that you can't deviate too
88 much from the sort of thing shown in the synopsis as you might like.
89 If you're doing something other than writing a fairly simple command,
90 and you want to screw around with the App::Cmd-iness of your program,
91 Simple might not be the best choice.
92
93 One specific warning... if you are writing a program with the
94 App::Cmd::Simple class embedded in it, you must call import on the
95 class. That's how things work. You can just do this:
96
97 YourApp::Cmd->import->run;
98
100 Ricardo Signes <rjbs@cpan.org>
101
103 This software is copyright (c) 2011 by Ricardo Signes.
104
105 This is free software; you can redistribute it and/or modify it under
106 the same terms as the Perl 5 programming language system itself.
107
108
109
110perl v5.12.3 2011-03-18 App::Cmd::Simple(3)