Skip to content

Commit

Permalink
added extract_usage method to Mojolicious::Command
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 25, 2014
1 parent 9d0bede commit 18ea4de
Show file tree
Hide file tree
Showing 22 changed files with 187 additions and 217 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@

4.70 2014-01-25
- Added extract_usage method to Mojolicious::Command.
- Updated jQuery to version 2.1.
- Improved all built-in commands to show usage information in their SYNOPSIS
sections.
- Improved tag helpers to make data attributes more convenient. (ravengerUA)
%= tag 'div', data => {my_id => 1, Name => 'test'} => 'some content'
is equivalent to
Expand Down
27 changes: 23 additions & 4 deletions lib/Mojolicious/Command.pm
Expand Up @@ -10,11 +10,12 @@ use Mojo::Loader;
use Mojo::Server;
use Mojo::Template;
use Mojo::Util 'spurt';
use Pod::Usage 'pod2usage';

has app => sub { Mojo::Server->new->build_app('Mojo::HelloWorld') };
has description => 'No description.';
has quiet => 0;
has usage => "usage: $0\n";
has usage => "Usage: APPLICATION\n";

sub chmod_file {
my ($self, $path, $mod) = @_;
Expand Down Expand Up @@ -46,6 +47,18 @@ sub create_rel_dir {
$self->create_dir($self->rel_dir($path));
}

sub extract_usage {
my $self = shift;

open my $fh, '>', \my $output;
pod2usage -exitval => 'noexit', -input => (caller)[1], -output => $fh;
$output =~ s/^.*\n//;
$output =~ s/\n$//;
$output =~ s/^\ {6}//gm;

return $output;
}

sub help {
print shift->usage;
exit 0;
Expand Down Expand Up @@ -105,9 +118,9 @@ Mojolicious::Command - Command base class
# Short usage message
has usage => <<EOF;
usage: $0 mycommand [OPTIONS]
Usage: APPLICATION mycommand [OPTIONS]
These options are available:
Options:
-s, --something Does something.
EOF
Expand Down Expand Up @@ -188,6 +201,13 @@ Create a directory.
Portably create a directory relative to the current working directory.
=head2 extract_usage
my $usage = $command->extract_usage;
Extract usage message from the SYNOPSIS section of the file this method was
called from.
=head2 help
$command->help;
Expand All @@ -210,7 +230,6 @@ directory.
=head2 render_data
my $data = $command->render_data('foo_bar');
my $data = $command->render_data('foo_bar', @args);
Expand Down
13 changes: 4 additions & 9 deletions lib/Mojolicious/Command/cgi.pm
Expand Up @@ -5,12 +5,7 @@ use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Server::CGI;

has description => "Start application with CGI.\n";
has usage => <<EOF;
usage: $0 cgi [OPTIONS]
These options are available:
--nph Enable non-parsed-header mode.
EOF
has usage => sub { shift->extract_usage };

sub run {
my ($self, @args) = @_;
Expand All @@ -29,10 +24,10 @@ Mojolicious::Command::cgi - CGI command
=head1 SYNOPSIS
use Mojolicious::Command::CGI;
Usage: APPLICATION cgi [OPTIONS]
my $cgi = Mojolicious::Command::CGI->new;
$cgi->run(@ARGV);
Options:
--nph Enable non-parsed-header mode.
=head1 DESCRIPTION
Expand Down
19 changes: 7 additions & 12 deletions lib/Mojolicious/Command/cpanify.pm
Expand Up @@ -6,15 +6,7 @@ use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::UserAgent;

has description => "Upload distribution to CPAN.\n";
has usage => <<EOF;
usage: $0 cpanify [OPTIONS] [FILE]
mojo cpanify -u sri -p secr3t Mojolicious-Plugin-MyPlugin-0.01.tar.gz
These options are available:
-p, --password <password> PAUSE password.
-u, --user <name> PAUSE username.
EOF
has usage => sub { shift->extract_usage };

sub run {
my ($self, @args) = @_;
Expand Down Expand Up @@ -56,10 +48,13 @@ Mojolicious::Command::cpanify - Cpanify command
=head1 SYNOPSIS
use Mojolicious::Command::cpanify;
Usage: APPLICATION cpanify [OPTIONS] [FILE]
my $cpanify = Mojolicious::Command::cpanify->new;
$cpanify->run(@ARGV);
mojo cpanify -u sri -p secr3t Mojolicious-Plugin-MyPlugin-0.01.tar.gz
Options:
-p, --password <password> PAUSE password.
-u, --user <name> PAUSE username.
=head1 DESCRIPTION
Expand Down
41 changes: 18 additions & 23 deletions lib/Mojolicious/Command/daemon.pm
Expand Up @@ -5,25 +5,7 @@ use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Server::Daemon;

has description => "Start application with HTTP and WebSocket server.\n";
has usage => <<EOF;
usage: $0 daemon [OPTIONS]
These options are available:
-b, --backlog <size> Listen backlog size, defaults to SOMAXCONN.
-c, --clients <number> Maximum number of concurrent clients, defaults
to 1000.
-g, --group <name> Group name for process.
-i, --inactivity <seconds> Inactivity timeout, defaults to the value of
MOJO_INACTIVITY_TIMEOUT or 15.
-l, --listen <location> One or more locations you want to listen on,
defaults to the value of MOJO_LISTEN or
"http://*:3000".
-p, --proxy Activate reverse proxy support, defaults to
the value of MOJO_REVERSE_PROXY.
-r, --requests <number> Maximum number of requests per keep-alive
connection, defaults to 25.
-u, --user <name> Username for process.
EOF
has usage => sub { shift->extract_usage };

sub run {
my ($self, @args) = @_;
Expand Down Expand Up @@ -53,10 +35,23 @@ Mojolicious::Command::daemon - Daemon command
=head1 SYNOPSIS
use Mojolicious::Command::daemon;
my $daemon = Mojolicious::Command::daemon->new;
$daemon->run(@ARGV);
Usage: APPLICATION daemon [OPTIONS]
Options:
-b, --backlog <size> Listen backlog size, defaults to SOMAXCONN.
-c, --clients <number> Maximum number of concurrent clients,
defaults to 1000.
-g, --group <name> Group name for process.
-i, --inactivity <seconds> Inactivity timeout, defaults to the value of
MOJO_INACTIVITY_TIMEOUT or 15.
-l, --listen <location> One or more locations you want to listen on,
defaults to the value of MOJO_LISTEN or
"http://*:3000".
-p, --proxy Activate reverse proxy support, defaults to
the value of MOJO_REVERSE_PROXY.
-r, --requests <number> Maximum number of requests per keep-alive
connection, defaults to 25.
-u, --user <name> Username for process.
=head1 DESCRIPTION
Expand Down
23 changes: 9 additions & 14 deletions lib/Mojolicious/Command/eval.pm
Expand Up @@ -4,17 +4,7 @@ use Mojo::Base 'Mojolicious::Command';
use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);

has description => "Run code against application.\n";
has usage => <<EOF;
usage: $0 eval [OPTIONS] CODE
mojo eval 'say app->ua->get("/")->res->body'
mojo eval -v 'app->home'
mojo eval -V 'app->renderer->paths'
These options are available:
-v, --verbose Print return value to STDOUT.
-V Print returned data structure to STDOUT.
EOF
has usage => sub { shift->extract_usage };

sub run {
my ($self, @args) = @_;
Expand All @@ -40,10 +30,15 @@ Mojolicious::Command::eval - Eval command
=head1 SYNOPSIS
use Mojolicious::Command::eval;
Usage: APPLICATION eval [OPTIONS] CODE
my $eval = Mojolicious::Command::eval->new;
$eval->run(@ARGV);
mojo eval 'say app->ua->get("/")->res->body'
mojo eval -v 'app->home'
mojo eval -V 'app->renderer->paths'
Options:
-v, --verbose Print return value to STDOUT.
-V Print returned data structure to STDOUT.
=head1 DESCRIPTION
Expand Down
14 changes: 6 additions & 8 deletions lib/Mojolicious/Command/generate.pm
Expand Up @@ -4,15 +4,16 @@ use Mojo::Base 'Mojolicious::Commands';
has description => "Generate files and directories from templates.\n";
has hint => <<EOF;
See '$0 generate help GENERATOR' for more information on a specific generator.
See 'APPLICATION generate help GENERATOR' for more information on a specific
generator.
EOF
has message => <<EOF;
usage: $0 generate GENERATOR [OPTIONS]
Usage: APPLICATION generate GENERATOR [OPTIONS]
These generators are currently available:
Generators:
EOF
has namespaces => sub { ['Mojolicious::Command::generate'] };
has usage => "usage: $0 generate GENERATOR [OPTIONS]\n";
has usage => sub { shift->extract_usage };

sub help { shift->run(@_) }

Expand All @@ -26,10 +27,7 @@ Mojolicious::Command::generate - Generator command
=head1 SYNOPSIS
use Mojolicious::Command::generate;
my $generator = Mojolicious::Command::generate->new;
$generator->run(@ARGV);
Usage: APPLICATION generate GENERATOR [OPTIONS]
=head1 DESCRIPTION
Expand Down
7 changes: 2 additions & 5 deletions lib/Mojolicious/Command/generate/app.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious::Command';
use Mojo::Util qw(class_to_file class_to_path);

has description => "Generate Mojolicious application directory structure.\n";
has usage => "usage: $0 generate app [NAME]\n";
has usage => sub { shift->extract_usage };

sub run {
my ($self, $class) = @_;
Expand Down Expand Up @@ -150,10 +150,7 @@ Mojolicious::Command::generate::app - App generator command
=head1 SYNOPSIS
use Mojolicious::Command::generate::app;
my $app = Mojolicious::Command::generate::app->new;
$app->run(@ARGV);
Usage: APPLICATION generate app [NAME]
=head1 DESCRIPTION
Expand Down
7 changes: 2 additions & 5 deletions lib/Mojolicious/Command/generate/lite_app.pm
Expand Up @@ -2,7 +2,7 @@ package Mojolicious::Command::generate::lite_app;
use Mojo::Base 'Mojolicious::Command';

has description => "Generate Mojolicious::Lite application.\n";
has usage => "usage: $0 generate lite_app [NAME]\n";
has usage => sub { shift->extract_usage };

sub run {
my ($self, $name) = @_;
Expand Down Expand Up @@ -51,10 +51,7 @@ Mojolicious::Command::generate::lite_app - Lite app generator command
=head1 SYNOPSIS
use Mojolicious::Command::generate::lite_app;
my $app = Mojolicious::Command::generate::lite_app->new;
$app->run(@ARGV);
Usage: APPLICATION generate lite_app [NAME]
=head1 DESCRIPTION
Expand Down
7 changes: 2 additions & 5 deletions lib/Mojolicious/Command/generate/makefile.pm
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious::Command';
use Mojolicious;

has description => qq{Generate "Makefile.PL".\n};
has usage => "usage: $0 generate makefile\n";
has usage => sub { shift->extract_usage };

sub run { shift->render_to_rel_file('makefile', 'Makefile.PL') }

Expand Down Expand Up @@ -33,10 +33,7 @@ Mojolicious::Command::generate::makefile - Makefile generator command
=head1 SYNOPSIS
use Mojolicious::Command::generate::makefile;
my $makefile = Mojolicious::Command::generate::makefile->new;
$makefile->run(@ARGV);
Usage: APPLICATION generate makefile
=head1 DESCRIPTION
Expand Down
7 changes: 2 additions & 5 deletions lib/Mojolicious/Command/generate/plugin.pm
Expand Up @@ -5,7 +5,7 @@ use Mojo::Util qw(camelize class_to_path);
use Mojolicious;

has description => "Generate Mojolicious plugin directory structure.\n";
has usage => "usage: $0 generate plugin [NAME]\n";
has usage => sub { shift->extract_usage };

sub run {
my ($self, $name) = @_;
Expand Down Expand Up @@ -121,10 +121,7 @@ Mojolicious::Command::generate::plugin - Plugin generator command
=head1 SYNOPSIS
use Mojolicious::Command::generate::plugin;
my $plugin = Mojolicious::Command::generate::plugin->new;
$plugin->run(@ARGV);
Usage: APPLICATION generate plugin [NAME]
=head1 DESCRIPTION
Expand Down

0 comments on commit 18ea4de

Please sign in to comment.