1Mojolicious::Command(3)User Contributed Perl DocumentatioMnojolicious::Command(3)
2
3
4

NAME

6       Mojolicious::Command - Command base class
7

SYNOPSIS

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         =head1 SYNOPSIS
26
27           Usage: APPLICATION mycommand [OPTIONS]
28
29           Options:
30             -s, --something   Does something
31
32         =cut
33

DESCRIPTION

35       Mojolicious::Command is an abstract base class for Mojolicious
36       commands.
37
38       See "COMMANDS" in Mojolicious::Commands for a list of commands that are
39       available by default.
40

ATTRIBUTES

42       Mojolicious::Command implements the following attributes.
43
44   app
45         my $app  = $command->app;
46         $command = $command->app(Mojolicious->new);
47
48       Application for command, defaults to a Mojo::HelloWorld object. Note
49       that this attribute is weakened.
50
51         # Introspect
52         say "Template path: $_" for @{$command->app->renderer->paths};
53
54   description
55         my $description = $command->description;
56         $command        = $command->description('Foo');
57
58       Short description of command, used for the command list.
59
60   quiet
61         my $bool = $command->quiet;
62         $command = $command->quiet($bool);
63
64       Limited command output.
65
66   template
67         my $template = $command->template;
68         $command     = $command->template({vars => 1});
69
70       Attribute values passed to Mojo::Template objects used to render
71       templates with "render_data", defaults to activating "vars".
72
73   usage
74         my $usage = $command->usage;
75         $command  = $command->usage('Foo');
76
77       Usage information for command, used for the help screen.
78

METHODS

80       Mojolicious::Command inherits all methods from Mojo::Base and
81       implements the following new ones.
82
83   chmod_file
84         $command = $command->chmod_file('/home/sri/foo.txt', 0644);
85
86       Change mode of a file.
87
88   chmod_rel_file
89         $command = $command->chmod_rel_file('foo/foo.txt', 0644);
90
91       Portably change mode of a file relative to the current working
92       directory.
93
94   create_dir
95         $command = $command->create_dir('/home/sri/foo/bar');
96
97       Create a directory if it does not exist already.
98
99   create_rel_dir
100         $command = $command->create_rel_dir('foo/bar/baz');
101
102       Portably create a directory relative to the current working directory
103       if it does not exist already.
104
105   extract_usage
106         my $usage = $command->extract_usage;
107
108       Extract usage message from the SYNOPSIS section of the file this method
109       was called from with "extract_usage" in Mojo::Util.
110
111   help
112         $command->help;
113
114       Print usage information for command.
115
116   rel_file
117         my $path = $command->rel_file('foo/bar.txt');
118
119       Return a Mojo::File object relative to the current working directory.
120
121   render_data
122         my $data = $command->render_data('foo_bar');
123         my $data = $command->render_data('foo_bar', @args);
124         my $data = $command->render_data('foo_bar', {foo => 'bar'});
125
126       Render a template from the "DATA" section of the command class with
127       Mojo::Loader and Mojo::Template. The template can be configured with
128       "template".
129
130   render_to_file
131         $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt');
132         $command = $command->render_to_file('foo_bar', '/home/sri/foo.txt', @args);
133         $command = $command->render_to_file(
134           'foo_bar', '/home/sri/foo.txt', {foo => 'bar'});
135
136       Render a template with "render_data" to a file if it does not exist
137       already, and create the directory if necessary.
138
139   render_to_rel_file
140         $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt');
141         $command = $command->render_to_rel_file('foo_bar', 'foo/bar.txt', @args);
142         $command = $command->render_to_rel_file(
143           'foo_bar', 'foo/bar.txt', {foo => 'bar'});
144
145       Portably render a template with "render_data" to a file relative to the
146       current working directory if it does not exist already, and create the
147       directory if necessary.
148
149   run
150         $command->run;
151         $command->run(@ARGV);
152
153       Run command. Meant to be overloaded in a subclass.
154
155   write_file
156         $command = $command->write_file('/home/sri/foo.txt', 'Hello World!');
157
158       Write text to a file if it does not exist already, and create the
159       directory if necessary.
160
161   write_rel_file
162         $command = $command->write_rel_file('foo/bar.txt', 'Hello World!');
163
164       Portably write text to a file relative to the current working directory
165       if it does not exist already, and create the directory if necessary.
166

SEE ALSO

168       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
169
170
171
172perl v5.32.0                      2020-07-28           Mojolicious::Command(3)
Impressum