Skip to content

Commit

Permalink
just default to @argv
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 19, 2017
1 parent 0f67dbd commit b2cfdc9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

7.21 2017-01-19
- Added extract_usage function to Mojo::Util.
- Improve getopt function in Mojo::Util to use @ARGV by default.

7.20 2017-01-18
- Fixed a bug in Mojo::File where the make_path method would die even if no
Expand Down
10 changes: 7 additions & 3 deletions lib/Mojo/Util.pm
Expand Up @@ -151,7 +151,7 @@ sub getopt {
my $opts = ref $_[1] eq 'ARRAY' ? splice @_, 1, 1 : [];
my $save = Getopt::Long::Configure(qw(default no_auto_abbrev no_ignore_case),
@$opts);
GetOptionsFromArray @_;
GetOptionsFromArray ref $_[0] eq 'ARRAY' ? @_ : (\@ARGV, @_);
Getopt::Long::Configure($save);
}

Expand Down Expand Up @@ -601,6 +601,10 @@ documentation, defaults to using the file this function was called from.
=head2 getopt
getopt
'H|headers=s' => \my @headers,
't|timeout=i' => \my $timeout,
'v|verbose' => \my $verbose;
getopt $array,
'H|headers=s' => \my @headers,
't|timeout=i' => \my $timeout,
Expand All @@ -611,8 +615,8 @@ documentation, defaults to using the file this function was called from.
'v|verbose' => \my $verbose;
Extract options from an array reference with L<Getopt::Long>, but without
changing its global configuration. The configuration options C<no_auto_abbrev>
and C<no_ignore_case> are enabled by default.
changing its global configuration, defaults to using C<@ARGV>. The configuration
options C<no_auto_abbrev> and C<no_ignore_case> are enabled by default.
# Extract "charset" option
getopt ['--charset', 'UTF-8'], 'charset=s' => \my $charset;
Expand Down
2 changes: 1 addition & 1 deletion script/hypnotoad
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base -strict;
use Mojo::Server::Hypnotoad;
use Mojo::Util qw(extract_usage getopt);

getopt \@ARGV,
getopt
'f|foreground' => \$ENV{HYPNOTOAD_FOREGROUND},
'h|help' => \my $help,
's|stop' => \$ENV{HYPNOTOAD_STOP},
Expand Down
2 changes: 1 addition & 1 deletion script/morbo
Expand Up @@ -4,7 +4,7 @@ use Mojo::Base -strict;
use Mojo::Server::Morbo;
use Mojo::Util qw(extract_usage getopt);

getopt \@ARGV,
getopt
'h|help' => \my $help,
'l|listen=s' => \my @listen,
'm|mode=s' => \$ENV{MOJO_MODE},
Expand Down
6 changes: 6 additions & 0 deletions t/mojo/util.t
Expand Up @@ -135,6 +135,12 @@ getopt $array, 'h' => \my $flag, 'w|whatever=s' => \my $whatever;
ok $flag, 'flag has been set';
is $whatever, 'Whatever!', 'right string';
is_deeply $array, ['stuff'], 'right structure';
{
local @ARGV = ('--charset', 'UTF-16', 'test');
getopt 'c|charset=s' => \my $charset;
is $charset, 'UTF-16', 'right string';
is_deeply \@ARGV, ['test'], 'right structure';
}

# unindent
is unindent(" test\n 123\n 456\n"), "test\n 123\n456\n",
Expand Down

0 comments on commit b2cfdc9

Please sign in to comment.