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', -signatures;
12
13         # Short description
14         has description => 'My first Mojo command';
15
16         # Usage message from SYNOPSIS
17         has usage => sub ($self) { $self->extract_usage };
18
19         sub run ($self, @args) {
20
21           # Magic here! :)
22         }
23
24         =head1 SYNOPSIS
25
26           Usage: APPLICATION mycommand [OPTIONS]
27
28           Options:
29             -s, --something   Does something
30
31         =cut
32

DESCRIPTION

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

ATTRIBUTES

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

METHODS

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

SEE ALSO

167       Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
168
169
170
171perl v5.32.1                      2021-02-07           Mojolicious::Command(3)
Impressum