Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
simplify option handling for built-in commands
  • Loading branch information
kraih committed Jul 30, 2012
1 parent ab6921f commit e904f08
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 67 deletions.
8 changes: 8 additions & 0 deletions lib/Mojolicious/Command.pm
Expand Up @@ -6,6 +6,7 @@ use Cwd 'getcwd';
use File::Basename 'dirname';
use File::Path 'mkpath';
use File::Spec::Functions qw(catdir catfile);
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use Mojo::Loader;
use Mojo::Server;
use Mojo::Template;
Expand Down Expand Up @@ -91,6 +92,13 @@ sub write_rel_file {
$self->write_file($self->rel_file($path), $data);
}

sub _options {
my ($self, $args) = (shift, shift);
local @ARGV = @$args;
GetOptions @_;
@$args = @ARGV;
}

1;

=head1 NAME
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious/Command/cgi.pm
@@ -1,7 +1,6 @@
package Mojolicious::Command::cgi;
use Mojo::Base 'Mojolicious::Command';

use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use Mojo::Server::CGI;

has description => "Start application with CGI.\n";
Expand All @@ -14,10 +13,9 @@ EOF

# "Fire all weapons and open a hailing frequency for my victory yodel."
sub run {
my $self = shift;
my $cgi = Mojo::Server::CGI->new;
local @ARGV = @_;
GetOptions(nph => sub { $cgi->nph(1) });
my ($self, @args) = @_;
my $cgi = Mojo::Server::CGI->new;
$self->_options(\@args, nph => sub { $cgi->nph(1) });
$cgi->run;
}

Expand Down
14 changes: 6 additions & 8 deletions lib/Mojolicious/Command/cpanify.pm
Expand Up @@ -2,7 +2,6 @@ package Mojolicious::Command::cpanify;
use Mojo::Base 'Mojolicious::Command';

use File::Basename 'basename';
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use Mojo::UserAgent;

has description => "Upload distribution to CPAN.\n";
Expand All @@ -18,16 +17,15 @@ EOF

# "Hooray! A happy ending for the rich people!"
sub run {
my $self = shift;
my ($self, @args) = @_;

# Options
local @ARGV = @_;
my $password = my $user = '';
GetOptions(
'p|password=s' => sub { $password = $_[1] },
'u|user=s' => sub { $user = $_[1] }
$self->_options(
\@args,
'p|password=s' => \(my $password = ''),
'u|user=s' => \(my $user = '')
);
die $self->usage unless my $file = shift @ARGV;
die $self->usage unless my $file = shift @args;

# Upload
my $tx = Mojo::UserAgent->new->detect_proxy->post_form(
Expand Down
12 changes: 5 additions & 7 deletions lib/Mojolicious/Command/daemon.pm
@@ -1,7 +1,6 @@
package Mojolicious::Command::daemon;
use Mojo::Base 'Mojolicious::Command';

use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use Mojo::Server::Daemon;

has description => "Start application with HTTP and WebSocket server.\n";
Expand Down Expand Up @@ -30,18 +29,17 @@ EOF
# Why do they call it that?
# Cause it has no pigment."
sub run {
my $self = shift;
my $daemon = Mojo::Server::Daemon->new;
my ($self, @args) = @_;

# Options
local @ARGV = @_;
my @listen;
GetOptions(
my $daemon = Mojo::Server::Daemon->new;
$self->_options(
\@args,
'b|backlog=i' => sub { $daemon->backlog($_[1]) },
'c|clients=i' => sub { $daemon->max_clients($_[1]) },
'g|group=s' => sub { $daemon->group($_[1]) },
'i|inactivity=i' => sub { $daemon->inactivity_timeout($_[1]) },
'l|listen=s' => \@listen,
'l|listen=s' => \my @listen,
'p|proxy' => sub { $ENV{MOJO_REVERSE_PROXY} = 1 },
'r|requests=i' => sub { $daemon->max_requests($_[1]) },
'u|user=s' => sub { $daemon->user($_[1]) }
Expand Down
14 changes: 5 additions & 9 deletions lib/Mojolicious/Command/eval.pm
@@ -1,8 +1,6 @@
package Mojolicious::Command::eval;
use Mojo::Base 'Mojolicious::Command';

use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);

has description => "Run code against application.\n";
has usage => <<"EOF";
usage: $0 eval [OPTIONS] CODE
Expand All @@ -19,13 +17,11 @@ EOF
# Air pressure returning.
# Terror replaced by cautious optimism."
sub run {
my $self = shift;
my ($self, @args) = @_;

# Options
local @ARGV = @_;
my $verbose;
GetOptions('v|verbose' => sub { $verbose = 1 });
my $code = shift @ARGV || '';
$self->_options(\@args, 'v|verbose' => \my $verbose);
my $code = shift @args || '';

# Run code against application
my $app = $self->app;
Expand All @@ -46,7 +42,7 @@ Mojolicious::Command::eval - Eval command
use Mojolicious::Command::eval;
my $eval = Mojolicious::Command::eval->new;
$eval->run;
$eval->run(@ARGV);
=head1 DESCRIPTION
Expand Down Expand Up @@ -78,7 +74,7 @@ L<Mojolicious::Command> and implements the following new ones.
=head2 C<run>
$eval->run;
$eval->run(@ARGV);
Run this command.
Expand Down
33 changes: 15 additions & 18 deletions lib/Mojolicious/Command/get.pm
@@ -1,7 +1,6 @@
package Mojolicious::Command::get;
use Mojo::Base 'Mojolicious::Command';

use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use Mojo::DOM;
use Mojo::IOLoop;
use Mojo::JSON;
Expand Down Expand Up @@ -39,19 +38,17 @@ EOF
# In the absence of pants, defense's suspenders serve no purpose.
# I'm going to allow them... for now."
sub run {
my $self = shift;
my ($self, @args) = @_;

# Options
local @ARGV = @_;
my ($method, $content, @headers) = ('GET', '');
my ($charset, $redirect, $verbose);
GetOptions(
'C|charset=s' => sub { $charset = $_[1] },
'c|content=s' => sub { $content = $_[1] },
'H|header=s' => \@headers,
'M|method=s' => sub { $method = $_[1] },
'r|redirect' => sub { $redirect = 1 },
'v|verbose' => sub { $verbose = 1 }
$self->_options(
\@args,
'C|charset=s' => \my $charset,
'c|content=s' => \(my $content = ''),
'H|header=s' => \my @headers,
'M|method=s' => \(my $method = 'GET'),
'r|redirect' => \my $redirect,
'v|verbose' => \my $verbose
);
$verbose = 1 if $method eq 'HEAD';

Expand All @@ -60,8 +57,8 @@ sub run {
/^\s*([^:]+)\s*:\s*([^:]+)\s*$/ and $headers{$1} = $2 for @headers;

# URL and selector
die $self->usage unless my $url = decode 'UTF-8', shift @ARGV // '';
my $selector = shift @ARGV;
die $self->usage unless my $url = decode 'UTF-8', shift @args // '';
my $selector = shift @args;

# Fresh user agent
my $ua = Mojo::UserAgent->new(ioloop => Mojo::IOLoop->singleton);
Expand Down Expand Up @@ -139,7 +136,7 @@ sub run {
return _json($buffer, $selector) if $type =~ /json/i;

# Selector
_select($buffer, $selector, $charset // $tx->res->content->charset);
_select($buffer, $selector, $charset // $tx->res->content->charset, @args);
}

sub _json {
Expand All @@ -155,15 +152,15 @@ sub _say {
}

sub _select {
my ($buffer, $selector, $charset) = @_;
my ($buffer, $selector, $charset, @args) = @_;

# Find
my $dom = Mojo::DOM->new->charset($charset)->parse($buffer);
my $results = $dom->find($selector);

# Commands
my $finished;
while (defined(my $command = shift @ARGV)) {
while (defined(my $command = shift @args)) {

# Number
if ($command =~ /^\d+$/) {
Expand All @@ -179,7 +176,7 @@ sub _select {

# Attribute
elsif ($command eq 'attr') {
next unless my $name = shift @ARGV;
next unless my $name = shift @args;
_say($_->attrs->{$name}) for @$results;
}

Expand Down
7 changes: 2 additions & 5 deletions lib/Mojolicious/Command/routes.pm
Expand Up @@ -2,7 +2,6 @@ package Mojolicious::Command::routes;
use Mojo::Base 'Mojolicious::Command';

use re 'regexp_pattern';
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);

has description => "Show available routes.\n";
has usage => <<"EOF";
Expand All @@ -14,12 +13,10 @@ EOF

# "I'm finally richer than those snooty ATM machines."
sub run {
my $self = shift;
my ($self, @args) = @_;

# Options
local @ARGV = @_;
my $verbose;
GetOptions('v|verbose' => sub { $verbose = 1 });
$self->_options(\@args, 'v|verbose' => \my $verbose);

# Walk and draw
my $routes = [];
Expand Down
8 changes: 3 additions & 5 deletions lib/Mojolicious/Command/test.pm
Expand Up @@ -4,7 +4,6 @@ use Mojo::Base 'Mojolicious::Command';
use Cwd 'realpath';
use FindBin;
use File::Spec::Functions qw(abs2rel catdir splitdir);
use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
use Mojo::Home;

has description => "Run unit tests.\n";
Expand All @@ -18,12 +17,11 @@ EOF
# "Why, the secret ingredient was...water!
# Yes, ordinary water, laced with nothing more than a few spoonfuls of LSD."
sub run {
my $self = shift;
my ($self, @args) = @_;

# Options
local @ARGV = @_;
GetOptions('v|verbose' => sub { $ENV{HARNESS_VERBOSE} = 1 });
my @tests = @ARGV;
$self->_options(\@args, 'v|verbose' => sub { $ENV{HARNESS_VERBOSE} = 1 });
my @tests = @args;

# Search tests
unless (@tests) {
Expand Down
6 changes: 2 additions & 4 deletions script/hypnotoad
Expand Up @@ -13,17 +13,15 @@ my $lib = join('/', @base, 'lib');
-e catdir(@base, 't') ? unshift(@INC, $lib) : push(@INC, $lib);

# "Hey sexy mama, wanna kill all humans?"
my $help;
GetOptions(
'f|foreground' => sub { $ENV{HYPNOTOAD_FOREGROUND} = 1 },
'h|help' => sub { $help = 1 },
'h|help' => \my $help,
's|stop' => sub { $ENV{HYPNOTOAD_STOP} = 1 },
't|test' => sub { $ENV{HYPNOTOAD_TEST} = 1 }
);
$help = 1 unless my $app = shift || $ENV{HYPNOTOAD_APP};

# Usage
die <<"EOF" if $help;
die <<"EOF" if $help || !(my $app = shift || $ENV{HYPNOTOAD_APP});
usage: $0 [OPTIONS] [APPLICATION]
hypnotoad script/myapp
Expand Down
10 changes: 4 additions & 6 deletions script/morbo
Expand Up @@ -16,17 +16,15 @@ my $lib = join('/', @base, 'lib');
# Across the galaxy my people are completing the mighty space fleet that
# will exterminate the human race!
# But first, this news from Tinseltown."
my ($help, @listen, @watch);
GetOptions(
'h|help' => sub { $help = 1 },
'l|listen=s' => \@listen,
'h|help' => \my $help,
'l|listen=s' => \my @listen,
'v|verbose' => sub { $ENV{MORBO_VERBOSE} = 1 },
'w|watch=s' => \@watch
'w|watch=s' => \my @watch
);
$help = 1 unless my $app = shift;

# Usage
die <<"EOF" if $help;
die <<"EOF" if $help || !(my $app = shift);
usage: $0 [OPTIONS] [APPLICATION]
morbo script/myapp
Expand Down

0 comments on commit e904f08

Please sign in to comment.