Skip to content

Commit

Permalink
Always use dash instead of underscore for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 29, 2020
1 parent 47ea614 commit 863f97b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/Author/generate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Mojolicious::Command::Author::generate - Generator command
Usage: APPLICATION generate GENERATOR [OPTIONS]
mojo generate app
mojo generate lite_app
mojo generate lite-app
=head1 DESCRIPTION
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Command/Author/generate/lite_app.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Mojolicious::Command::Author::generate::lite_app - Lite app generator command
=head1 SYNOPSIS
Usage: APPLICATION generate lite_app [OPTIONS] [NAME]
Usage: APPLICATION generate lite-app [OPTIONS] [NAME]
mojo generate lite_app
mojo generate lite_app foo.pl
mojo generate lite-app
mojo generate lite-app foo.pl
Options:
-h, --help Show this summary of available options
Expand Down
16 changes: 11 additions & 5 deletions lib/Mojolicious/Commands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ sub run {
if (!$ENV{MOJO_NO_DETECT} && (my $env = $self->detect)) { $name = $env }

# Run command
if ($name && $name =~ /^\w+$/ && ($name ne 'help' || $args[0])) {
if ($name && $name =~ /^\w[\w-]+$/ && ($name ne 'help' || $args[0])) {
$name =~ s/-/_/g;

# Help
$name = shift @args if my $help = $name eq 'help';
Expand Down Expand Up @@ -68,7 +69,12 @@ sub run {
for grep { _command($_) } find_modules($ns), find_packages($ns);
}

my @rows = map { [" $_", $all{$_}] } sort keys %all;
my @rows;
for my $class (sort keys %all) {
my $command = $class;
$command =~ s/(?<!^)_/-/g;
push @rows, [" $command", $all{$class}];
}
return print $self->message, tablify(\@rows), $self->hint;
}

Expand Down Expand Up @@ -106,7 +112,7 @@ Mojolicious::Commands - Command line interface
Usage: APPLICATION COMMAND [OPTIONS]
mojo version
mojo generate lite_app
mojo generate lite-app
./myapp.pl daemon -m production -l http://*:8080
./myapp.pl get /foo
./myapp.pl routes -v
Expand Down Expand Up @@ -177,9 +183,9 @@ List available options for generator command with short descriptions.
Use L<Mojolicious::Command::Author::generate::app> to generate application
directory structure for a fully functional L<Mojolicious> application.
=head2 generate lite_app
=head2 generate lite-app
$ mojo generate lite_app
$ mojo generate lite-app
Use L<Mojolicious::Command::Author::generate::lite_app> to generate a fully
functional L<Mojolicious::Lite> application.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,7 @@ application to test something? Thanks to the command
L<Mojolicious::Command::eval> you can do just that, the application object
itself can be accessed via C<app>.

$ mojo generate lite_app myapp.pl
$ mojo generate lite-app myapp.pl
$ ./myapp.pl eval 'say for @{app->static->paths}'
$ ./myapp.pl eval 'say for sort keys %{app->renderer->helpers}'

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Growing.pod
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Both application skeletons can be automatically generated with the commands
L<Mojolicious::Command::Author::generate::lite_app> and
L<Mojolicious::Command::Author::generate::app>.

$ mojo generate lite_app myapp.pl
$ mojo generate lite-app myapp.pl
$ mojo generate app MyApp

Feature-wise both are almost equal, the only real differences are
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Tutorial.pod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ L<Mojolicious::Lite>, turning your script into a full featured web application.
With L<Mojolicious::Command::Author::generate::lite_app> there is also a helper
command to generate a small example application.

$ mojo generate lite_app myapp.pl
$ mojo generate lite-app myapp.pl

=head2 Commands

Expand Down
52 changes: 41 additions & 11 deletions t/mojolicious/commands.t
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,48 @@ is ref Mojolicious::Commands->new->run('psgi'), 'CODE', 'right reference';
'right reference';
}

# Start application with application specific command
# Start application with application specific commands
my $app;
{
local $ENV{MOJO_APP_LOADER} = 1;
$app = Mojolicious::Commands->start_app('MojoliciousTest');
}
is $app->start('test_command'), 'works!', 'right result';
is $app->start('test_command'), 'works!', 'right result';
is $app->start('test-command'), 'works!', 'right result';
is $app->start('_test2-command'), 'works 2!', 'right result';
{
is(Mojolicious::Commands->start_app(MojoliciousTest => 'test_command'),
'works!', 'right result');
is(Mojolicious::Commands->start_app(MojoliciousTest => 'test-command'),
'works!', 'right result');
is(Mojolicious::Commands->start_app(MojoliciousTest => '_test2-command'),
'works 2!', 'right result');
}

# Application specific help
my $buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
local $ENV{HARNESS_ACTIVE} = 0;
$app->start;
}
like $buffer,
qr/Usage: APPLICATION COMMAND \[OPTIONS\].*_test2-command.*cgi.*test-comm/s,
'right output';

# Commands starting with a dash are not allowed
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
local $ENV{HARNESS_ACTIVE} = 0;
$app->start('-test2-command');
}
like $buffer,
qr/Usage: APPLICATION COMMAND \[OPTIONS\].*_test2-command.*cgi.*test-comm/s,
'right output';

# Do not pick up options for detected environments
{
local $ENV{MOJO_MODE};
Expand All @@ -99,27 +129,27 @@ is_deeply $commands->namespaces,
ok $commands->description, 'has a description';
like $commands->message, qr/COMMAND/, 'has a message';
like $commands->hint, qr/help/, 'has a hint';
my $buffer = '';
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
local $ENV{HARNESS_ACTIVE} = 0;
$commands->run;
}
like $buffer,
qr/Usage: APPLICATION COMMAND \[OPTIONS\].*daemon.*my_test_command.*version/s,
qr/Usage: APPLICATION COMMAND \[OPTIONS\].*daemon.*my-test-command.*version/s,
'right output';
like $buffer, qr/See, it works/, 'description has been picked up';
unlike $buffer, qr/my_fake_test_command/, 'fake command has been ignored';
unlike $buffer, qr/my-fake-test-command/, 'fake command has been ignored';

# help
$buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$commands->run('help', 'generate', 'lite_app');
$commands->run('help', 'generate', 'lite-app');
}
like $buffer, qr/Usage: APPLICATION generate lite_app \[OPTIONS\] \[NAME\]/,
like $buffer, qr/Usage: APPLICATION generate lite-app \[OPTIONS\] \[NAME\]/,
'right output';
$buffer = '';
{
Expand All @@ -133,9 +163,9 @@ $buffer = '';
{
open my $handle, '>', \$buffer;
local *STDOUT = $handle;
$commands->run('generate', 'lite_app', '--help');
$commands->run('generate', 'lite-app', '--help');
}
like $buffer, qr/Usage: APPLICATION generate lite_app \[OPTIONS\] \[NAME\]/,
like $buffer, qr/Usage: APPLICATION generate lite-app \[OPTIONS\] \[NAME\]/,
'right output';

# get
Expand Down Expand Up @@ -245,7 +275,7 @@ $buffer = '';
$generator->run;
}
like $buffer,
qr/Usage: APPLICATION generate GENERATOR \[OPTIONS\].*lite_app.*plugin/s,
qr/Usage: APPLICATION generate GENERATOR \[OPTIONS\].*lite-app.*plugin/s,
'right output';

# generate app
Expand Down Expand Up @@ -280,7 +310,7 @@ chdir $cwd;
require Mojolicious::Command::Author::generate::lite_app;
$app = Mojolicious::Command::Author::generate::lite_app->new;
ok $app->description, 'has a description';
like $app->usage, qr/lite_app/, 'has usage information';
like $app->usage, qr/lite-app/, 'has usage information';
$dir = tempdir CLEANUP => 1;
chdir $dir;
$buffer = '';
Expand Down
6 changes: 6 additions & 0 deletions t/mojolicious/lib/MojoliciousTest/Command/_test2_command.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package MojoliciousTest::Command::_test2_command;
use Mojo::Base 'Mojolicious::Command';

sub run {'works 2!'}

1;

0 comments on commit 863f97b

Please sign in to comment.