1App::CLI(3) User Contributed Perl Documentation App::CLI(3)
2
3
4
6 App::CLI - Dispatcher module for command line interface programs
7
9 package MyApp;
10 use base 'App::CLI'; # the DISPATCHER of your App
11 # it's not necessary to put the dispatcher
12 # on the top level of your App
13
14 package main;
15
16 MyApp->dispatch; # call the dispatcher where you want
17
18
19 package MyApp::List;
20 use base qw(App::CLI::Command); # any (SUB)COMMAND of your App
21
22 use constant options => (
23 "h|help" => "help",
24 "verbose" => "verbose",
25 'n|name=s' => 'name',
26 );
27
28 use constant subcommands => qw(User Nickname type); # if you want subcommands
29 # automatically dispatch to subcommands
30 # when invoke $ myapp list [user|nickname|--type]
31 # note 'type' is not capitalized
32 # it is a deprecated subcommand
33
34 sub run {
35 my ($self, @args) = @_;
36
37 print "verbose" if $self->{verbose};
38 my $name = $self->{name}; # get arg following long option --name
39
40 if ($self->{help}) {
41 # if $ myapp list --help or $ myapp list -h
42 # only output PODs
43 } else {
44 # do something when invoking $ myapp list
45 # without subcommand and --help
46 }
47 }
48
49
50 package MyApp::List::User;
51 use base qw(App::CLI::Command);
52 use constant options => (
53 "h|help" => "help",
54 );
55
56 sub run {
57 my ($self,@args) = @_;
58 # code for listing user
59 }
60
61
62 pakcage MyApp::List::Nickname;
63 use base qw(App::CLI::Command);
64 use constant options => (
65 "sort=s" => "sort",
66 );
67
68 sub run {
69 my ($self,@args) = @_;
70 # code for listing nickname
71 }
72
73
74 package MyApp::List::type; # old genre of subcommand could not cascade infinitely
75 use base qw(MyApp::List); # should inherit its parent's command
76
77 sub run {
78 my ($self, @args);
79 # run to here when invoking $ myapp list --type
80 }
81
82
83 package MyApp::Help;
84 use base 'App::CLI::Command::Help';
85
86 use constant options => (
87 'verbose' => 'verbose',
88 );
89
90 sub run {
91 my ($self, @arg) = @_;
92 # do something
93 $self->SUPER(@_); # App::CLI::Command::Help would output POD of each command
94 }
95
97 "App::CLI" dispatches CLI (command line interface) based commands into
98 command classes. It also supports subcommand and per-command options.
99
100 Methods
101 get_opt([@config], %opt_map)
102
103 Give options map, processed by Getopt::Long::Parser.
104
105 dispatch(@args)
106
107 Interface of dispatcher
108
109 cmd_map($cmd)
110
111 Find the name of the package implementing the requested command.
112
113 The command is first searched for in "alias". If the alias exists and
114 points to a package name starting with the "+" sign, then that package
115 name (minus the "+" sign) is returned. This makes it possible to map
116 commands to arbitrary packages.
117
118 Otherwise, the package is searched for in the result of calling
119 "commands", and a package name is constructed by upper-casing the first
120 character of the command name, and appending it to the package name of
121 the app itself.
122
123 If both of these fail, and the command does not map to any package
124 name, "undef" is returned instead.
125
126 get_cmd($cmd, @arg)
127
128 Return subcommand of first level via $ARGV[0].
129
131 • App::CLI::Command
132
133 • Getopt::Long
134
136 • Chia-liang Kao <clkao@clkao.org>
137
138 • Alex Vandiver <alexmv@bestpractical.com>
139
140 • Yo-An Lin <cornelius.howl@gmail.com>
141
142 • Shelling <navyblueshellingford@gmail.com>
143
144 • Paul Cochrane <paul@liekut.de> (current maintainer)
145
147 The following people have contributed patches to the project:
148
149 • José Joaquín Atria <jjatria@gmail.com>
150
151 • sunnavy <sunnavy@gmail.com>
152
153 • Ildar Shaimordanov <ildar.shaimordanov@gmail.com>
154
156 Copyright 2005-2010 by Chia-liang Kao <clkao@clkao.org>. Copyright
157 2010 by Yo-An Lin <cornelius.howl@gmail.com> and Shelling
158 <navyblueshellingford@gmail.com>. Copyright 2017-2018 by Paul Cochrane
159 <paul@liekut.de>
160
161 This program is free software; you can redistribute it and/or modify it
162 under the same terms as Perl itself.
163
164 See <http://www.perl.com/perl/misc/Artistic.html>
165
166
167
168perl v5.36.0 2022-07-22 App::CLI(3)