Skip to content

Instantly share code, notes, and snippets.

/getopt.diff Secret

Created November 1, 2016 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1c6e1b3af61009c15a1b4205cb90b1dc to your computer and use it in GitHub Desktop.
Save anonymous/1c6e1b3af61009c15a1b4205cb90b1dc to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Util.pm b/lib/Mojo/Util.pm
index 4295e96..261444e 100644
--- a/lib/Mojo/Util.pm
+++ b/lib/Mojo/Util.pm
@@ -8,6 +8,7 @@ use Digest::SHA qw(hmac_sha1_hex sha1 sha1_hex);
use Encode 'find_encoding';
use Exporter 'import';
use File::Find 'find';
+use Getopt::Long 'GetOptionsFromArray';
use IO::Poll qw(POLLIN POLLPRI);
use List::Util 'min';
use MIME::Base64 qw(decode_base64 encode_base64);
@@ -55,7 +56,7 @@ my %CACHE;
our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
- qw(decode deprecated dumper encode files hmac_sha1_sum html_unescape),
+ qw(decode deprecated dumper encode files getopt hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt steady_time tablify term_escape trim unindent unquote),
@@ -138,6 +139,13 @@ sub files {
return sort keys %files;
}
+sub getopt {
+ my @extra = ref $_[1] eq 'ARRAY' ? @{splice @_, 1, 1} : ();
+ my $save = Getopt::Long::Configure(qw(no_auto_abbrev no_ignore_case), @extra);
+ GetOptionsFromArray @_;
+ Getopt::Long::Configure($save);
+}
+
sub html_unescape {
my $str = shift;
$str
@@ -592,6 +600,20 @@ Include hidden files and directories.
=back
+=head getopt
+
+ getopt $arrayref,
+ 'H|headers=s' => \my @headers,
+ 't|timeout=i' => \my $timeout,
+ 'v|verbose' => \my $verbose;
+ getopt $arrayref, ['pass_through'],
+ 'H|headers=s' => \my @headers,
+ 't|timeout=i' => \my $timeout,
+ 'v|verbose' => \my $verbose;
+
+Extract options from an array reference with L<Getopt::Long>, but without
+changing its global configuration.
+
=head2 hmac_sha1_sum
my $checksum = hmac_sha1_sum $bytes, 'passw0rd';
diff --git a/lib/Mojolicious/Command/cgi.pm b/lib/Mojolicious/Command/cgi.pm
index 2ded0d1..8832f2d 100644
--- a/lib/Mojolicious/Command/cgi.pm
+++ b/lib/Mojolicious/Command/cgi.pm
@@ -1,15 +1,15 @@
package Mojolicious::Command::cgi;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Server::CGI;
+use Mojo::Util 'getopt';
has description => 'Start application with CGI';
has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
- GetOptionsFromArray \@args, nph => \(my $nph = 0);
+ getopt \@args, nph => \(my $nph = 0);
Mojo::Server::CGI->new(app => $self->app, nph => $nph)->run;
}
diff --git a/lib/Mojolicious/Command/cpanify.pm b/lib/Mojolicious/Command/cpanify.pm
index 6a25654..a0d6cc4 100644
--- a/lib/Mojolicious/Command/cpanify.pm
+++ b/lib/Mojolicious/Command/cpanify.pm
@@ -2,7 +2,7 @@ package Mojolicious::Command::cpanify;
use Mojo::Base 'Mojolicious::Command';
use File::Basename 'basename';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
+use Mojo::Util 'getopt';
has description => 'Upload distribution to CPAN';
has usage => sub { shift->extract_usage };
@@ -10,7 +10,7 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
- GetOptionsFromArray \@args,
+ getopt \@args,
'p|password=s' => \(my $password = ''),
'u|user=s' => \(my $user = '');
die $self->usage unless my $file = shift @args;
diff --git a/lib/Mojolicious/Command/daemon.pm b/lib/Mojolicious/Command/daemon.pm
index ae185bb..c8afb4d 100644
--- a/lib/Mojolicious/Command/daemon.pm
+++ b/lib/Mojolicious/Command/daemon.pm
@@ -1,8 +1,8 @@
package Mojolicious::Command::daemon;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Server::Daemon;
+use Mojo::Util 'getopt';
has description => 'Start application with HTTP and WebSocket server';
has usage => sub { shift->extract_usage };
@@ -11,7 +11,7 @@ sub run {
my ($self, @args) = @_;
my $daemon = Mojo::Server::Daemon->new(app => $self->app);
- GetOptionsFromArray \@args,
+ getopt \@args,
'b|backlog=i' => sub { $daemon->backlog($_[1]) },
'c|clients=i' => sub { $daemon->max_clients($_[1]) },
'i|inactivity-timeout=i' => sub { $daemon->inactivity_timeout($_[1]) },
diff --git a/lib/Mojolicious/Command/eval.pm b/lib/Mojolicious/Command/eval.pm
index 3986c61..e344c73 100644
--- a/lib/Mojolicious/Command/eval.pm
+++ b/lib/Mojolicious/Command/eval.pm
@@ -1,7 +1,7 @@
package Mojolicious::Command::eval;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
+use Mojo::Util 'getopt';
has description => 'Run code against application';
has usage => sub { shift->extract_usage };
@@ -9,7 +9,7 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
- GetOptionsFromArray \@args, 'v|verbose' => \my $v1, 'V' => \my $v2;
+ getopt \@args, 'v|verbose' => \my $v1, 'V' => \my $v2;
my $code = shift @args || '';
# Run code against application
diff --git a/lib/Mojolicious/Command/get.pm b/lib/Mojolicious/Command/get.pm
index 85e964f..b047a67 100644
--- a/lib/Mojolicious/Command/get.pm
+++ b/lib/Mojolicious/Command/get.pm
@@ -1,13 +1,12 @@
package Mojolicious::Command::get;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::DOM;
use Mojo::IOLoop;
use Mojo::JSON qw(encode_json j);
use Mojo::JSON::Pointer;
use Mojo::UserAgent;
-use Mojo::Util qw(decode encode);
+use Mojo::Util qw(getopt decode encode);
use Scalar::Util 'weaken';
has description => 'Perform HTTP request';
@@ -16,7 +15,7 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
- GetOptionsFromArray \@args,
+ getopt \@args,
'C|charset=s' => \my $charset,
'c|content=s' => \(my $content = ''),
'H|header=s' => \my @headers,
diff --git a/lib/Mojolicious/Command/prefork.pm b/lib/Mojolicious/Command/prefork.pm
index bb77be9..b293b2e 100644
--- a/lib/Mojolicious/Command/prefork.pm
+++ b/lib/Mojolicious/Command/prefork.pm
@@ -1,8 +1,8 @@
package Mojolicious::Command::prefork;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
use Mojo::Server::Prefork;
+use Mojo::Util 'getopt';
has description =>
'Start application with pre-forking HTTP and WebSocket server';
@@ -12,7 +12,7 @@ sub run {
my ($self, @args) = @_;
my $prefork = Mojo::Server::Prefork->new(app => $self->app);
- GetOptionsFromArray \@args,
+ getopt \@args,
'a|accepts=i' => sub { $prefork->accepts($_[1]) },
'b|backlog=i' => sub { $prefork->backlog($_[1]) },
'c|clients=i' => sub { $prefork->max_clients($_[1]) },
diff --git a/lib/Mojolicious/Command/routes.pm b/lib/Mojolicious/Command/routes.pm
index c09b23b..f7b731d 100644
--- a/lib/Mojolicious/Command/routes.pm
+++ b/lib/Mojolicious/Command/routes.pm
@@ -2,8 +2,7 @@ package Mojolicious::Command::routes;
use Mojo::Base 'Mojolicious::Command';
use re 'regexp_pattern';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
-use Mojo::Util qw(encode tablify);
+use Mojo::Util qw(encode getopt tablify);
has description => 'Show available routes';
has usage => sub { shift->extract_usage };
@@ -11,7 +10,7 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
- GetOptionsFromArray \@args, 'v|verbose' => \my $verbose;
+ getopt \@args, 'v|verbose' => \my $verbose;
my $rows = [];
_walk($_, 0, $rows, $verbose) for @{$self->app->routes->children};
diff --git a/lib/Mojolicious/Command/test.pm b/lib/Mojolicious/Command/test.pm
index 96e3f95..c4519b0 100644
--- a/lib/Mojolicious/Command/test.pm
+++ b/lib/Mojolicious/Command/test.pm
@@ -1,7 +1,7 @@
package Mojolicious::Command::test;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
+use Mojo::Util 'getopt';
has description => 'Run tests';
has usage => sub { shift->extract_usage };
@@ -9,7 +9,7 @@ has usage => sub { shift->extract_usage };
sub run {
my ($self, @args) = @_;
- GetOptionsFromArray \@args, 'v|verbose' => \$ENV{HARNESS_VERBOSE};
+ getopt \@args, 'v|verbose' => \$ENV{HARNESS_VERBOSE};
if (!@args && (my $home = $self->app->home)) {
die "Can't find test directory.\n" unless -d $home->rel_dir('t');
diff --git a/lib/Mojolicious/Commands.pm b/lib/Mojolicious/Commands.pm
index ae66b04..4eb818e 100644
--- a/lib/Mojolicious/Commands.pm
+++ b/lib/Mojolicious/Commands.pm
@@ -1,10 +1,9 @@
package Mojolicious::Commands;
use Mojo::Base 'Mojolicious::Command';
-use Getopt::Long 'GetOptionsFromArray';
use Mojo::Loader qw(find_modules find_packages load_class);
use Mojo::Server;
-use Mojo::Util 'tablify';
+use Mojo::Util qw(getopt tablify);
has hint => <<EOF;
@@ -73,15 +72,11 @@ sub start_app { shift; Mojo::Server->new->build_app(shift)->start(@_) }
# Command line options for MOJO_HELP, MOJO_HOME and MOJO_MODE
sub _args {
- return if __PACKAGE__->detect;
-
- my $save
- = Getopt::Long::Configure(qw(no_auto_abbrev no_ignore_case pass_through));
- GetOptionsFromArray shift,
+ getopt shift, ['pass_through'],
'h|help' => \$ENV{MOJO_HELP},
'home=s' => \$ENV{MOJO_HOME},
- 'm|mode=s' => \$ENV{MOJO_MODE};
- Getopt::Long::Configure($save);
+ 'm|mode=s' => \$ENV{MOJO_MODE}
+ unless __PACKAGE__->detect;
}
# Do not remove options from @ARGV
diff --git a/script/hypnotoad b/script/hypnotoad
index 363bac1..8728bd2 100755
--- a/script/hypnotoad
+++ b/script/hypnotoad
@@ -3,9 +3,9 @@
use strict;
use warnings;
-use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
+use Mojo::Util 'getopt';
-GetOptions
+getopt
'f|foreground' => \$ENV{HYPNOTOAD_FOREGROUND},
'h|help' => \my $help,
's|stop' => \$ENV{HYPNOTOAD_STOP},
diff --git a/script/morbo b/script/morbo
index 2db5973..2bac834 100755
--- a/script/morbo
+++ b/script/morbo
@@ -3,9 +3,9 @@
use strict;
use warnings;
-use Getopt::Long qw(GetOptions :config no_auto_abbrev no_ignore_case);
+use Mojo::Util 'getopt';
-GetOptions
+getopt
'h|help' => \my $help,
'l|listen=s' => \my @listen,
'm|mode=s' => \$ENV{MOJO_MODE},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment