1Mojolicious::Command(3)User Contributed Perl DocumentatioMnojolicious::Command(3)
2
3
4
6 Mojolicious::Command - Command base class
7
9 # Lowercase command name
10 package Mojolicious::Command::mycommand;
11 use Mojo::Base 'Mojolicious::Command';
12
13 # Short description
14 has description => 'My first Mojo command';
15
16 # Usage message from SYNOPSIS
17 has usage => sub { shift->extract_usage };
18
19 sub run {
20 my ($self, @args) = @_;
21
22 # Magic here! :)
23 }
24
25 1;
26
27 =head1 SYNOPSIS
28
29 Usage: APPLICATION mycommand [OPTIONS]
30
31 Options:
32 -s, --something Does something
33
34 =cut
35
37 Mojolicious::Command is an abstract base class for Mojolicious
38 commands.
39
40 See "COMMANDS" in Mojolicious::Commands for a list of commands that are
41 available by default.
42
44 Mojolicious::Command implements the following attributes.
45
46 app
47 my $app = $command->app;
48 $command = $command->app(Mojolicious->new);
49
50 Application for command, defaults to a Mojo::HelloWorld object. Note
51 that this attribute is weakened.
52
53 # Introspect
54 say "Template path: $_" for @{$command->app->renderer->paths};
55
56 description
57 my $description = $command->description;
58 $command = $command->description('Foo');
59
60 Short description of command, used for the command list.
61
62 quiet
63 my $bool = $command->quiet;
64 $command = $command->quiet($bool);
65
66 Limited command output.
67
68 template
69 my $template = $command->template;
70 $command = $command->template({vars => 1});
71
72 Attribute values passed to Mojo::Template objects used to render
73 templates with "render_data", defaults to activating "vars".
74
75 usage
76 my $usage = $command->usage;
77 $command = $command->usage('Foo');
78
79 Usage information for command, used for the help screen.
80
82 Mojolicious::Command inherits all methods from Mojo::Base and
83 implements the following new ones.
84
85 chmod_file
86 $command = $command->chmod_file('/home/sri/foo.txt', 0644);
87
88 Change mode of a file.
89
90 chmod_rel_file
91 $command = $command->chmod_rel_file('foo/foo.txt', 0644);
92
93 Portably change mode of a file relative to the current working
94 directory.
95
96 create_dir
97 $command = $command->create_dir('/home/sri/foo/bar');
98
99 Create a directory if it does not exist already.
100
101 create_rel_dir
102 $command = $command->create_rel_dir('foo/bar/baz');
103
104 Portably create a directory relative to the current working directory
105 if it does not exist already.
106
107 extract_usage
108 my $usage = $command->extract_usage;
109
110 Extract usage message from the SYNOPSIS section of the file this method
111 was called from with "extract_usage" in Mojo::Util.
112
113 help
114 $command->help;
115
116 Print usage information for command.
117
118 rel_file
119 my $path = $command->rel_file('foo/bar.txt');
120
121 Return a Mojo::File object relative to the current working directory.
122
123 render_data
124 my $data = $command->render_data('foo_bar');
125 my $data = $command->render_data('foo_bar', @args);
126 my $data = $command->render_data('foo_bar', {foo => 'bar'});
127
128 Render a template from the "DATA" section of the command class with
129 Mojo::Loader and Mojo::Template. The template can be configured with
130 "template".
131
132 render_to_file
133 $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt');
134 $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt', @args);
135 $command = $command->render_to_file(
136 'foo_bar', '/home/sri/foo.txt', {foo => 'bar'});
137
138 Render a template with "render_data" to a file if it does not exist
139 already, and create the directory if necessary.
140
141 render_to_rel_file
142 $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt');
143 $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt', @args);
144 $command = $command->render_to_rel_file(
145 'foo_bar', 'foo/bar.txt', {foo => 'bar'});
146
147 Portably render a template with "render_data" to a file relative to the
148 current working directory if it does not exist already, and create the
149 directory if necessary.
150
151 run
152 $command->run;
153 $command->run(@ARGV);
154
155 Run command. Meant to be overloaded in a subclass.
156
157 write_file
158 $command = $command->write_file('/home/sri/foo.txt', 'Hello World!');
159
160 Write text to a file if it does not exist already, and create the
161 directory if necessary.
162
163 write_rel_file
164 $command = $command->write_rel_file('foo/bar.txt', 'Hello World!');
165
166 Portably write text to a file relative to the current working directory
167 if it does not exist already, and create the directory if necessary.
168
170 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
171
172
173
174perl v5.30.1 2020-01-30 Mojolicious::Command(3)