Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement --version #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/App/Cmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,25 @@ Valid arguments are:

no_help_plugin - if true, the help plugin is not added

no_version_plugin - if true, the version plugin is not added

plugin_search_path - The path to search for commands in. Defaults to
results of plugin_search_path method

If C<no_commands_plugin> is not given, App::Cmd::Command::commands will be
If C<no_commands_plugin> is not given, L<App::Cmd::Command::commands> will be
required, and it will be registered to handle all of its command names not
handled by other plugins.

If C<no_help_plugin> is not given, App::Cmd::Command::help will be required,
If C<no_help_plugin> is not given, L<App::Cmd::Command::help> will be required,
and it will be registered to handle all of its command names not handled by
other plugins. B<Note:> "help" is the default command, so if you do not load
the default help plugin, you should provide your own or override the
C<default_command> method.

If C<no_version_plugin> is not given, L<App::Cmd::Command::version> will be
required to show the application's version with command C<--version>. The
version command is not included in the command list.

=cut

sub new {
Expand Down Expand Up @@ -197,7 +203,7 @@ sub _command {
}
}

$self->_load_default_plugin($_, $arg, \%plugin) for qw(commands help);
$self->_load_default_plugin($_, $arg, \%plugin) for qw(commands help version);

if ($self->allow_any_unambiguous_abbrev) {
# add abbreviations to list of authorized commands
Expand Down
1 change: 1 addition & 0 deletions lib/App/Cmd/Command/commands.pm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sub execute {
print { $target } "Available commands:\n\n";

my @primary_commands =
grep { $_ ne '--version' }
map { ($_->command_names)[0] }
$self->app->command_plugins;

Expand Down
27 changes: 27 additions & 0 deletions lib/App/Cmd/Command/version.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use strict;
use warnings;

package App::Cmd::Command::version;
use App::Cmd::Command;
BEGIN { our @ISA = 'App::Cmd::Command'; }

# ABSTRACT: display an app's version

=head1 DESCRIPTION

This plugin implements the C<--version> command. On execution it shows the
program name, it's base class with version number, and the full program name.

=cut

sub command_names { qw/--version/ }

sub execute {
my ($self, $opts, $args) = @_;

printf "%s (%s) version %s (%s)\n",
$self->app->arg0, ref($self->app),
$self->app->VERSION, $self->app->full_arg0;
}

1;
1 change: 1 addition & 0 deletions t/abbrev.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Test::MyCmdAbbrev;
my $app = Test::MyCmdAbbrev->new( {
no_commands_plugin => 1,
no_help_plugin => 1,
no_version_plugin => 1,
} );

is_deeply(
Expand Down
12 changes: 9 additions & 3 deletions t/basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 13;
use Test::More tests => 15;
use App::Cmd::Tester;

use lib 't/lib';
Expand All @@ -16,7 +16,7 @@ isa_ok($app, 'Test::MyCmd');

is_deeply(
[ sort $app->command_names ],
[ sort qw(help --help -h -? commands exit frob frobulate hello justusage stock) ],
[ sort qw(help --help -h --version -? commands exit frob frobulate hello justusage stock) ],
"got correct list of registered command names",
);

Expand All @@ -25,6 +25,7 @@ is_deeply(
[ qw(
App::Cmd::Command::commands
App::Cmd::Command::help
App::Cmd::Command::version
Test::MyCmd::Command::exit
Test::MyCmd::Command::frobulate
Test::MyCmd::Command::hello
Expand Down Expand Up @@ -65,11 +66,16 @@ is_deeply(
like($@, qr/mandatory method/, "un-subclassed &run leads to death");
}

my $return = test_app('Test::MyCmd', [ qw(commands) ]);
my $return = test_app('Test::MyCmd', [ qw(--version) ]);
my $version_expect = "basic.t (Test::MyCmd) version 0.123 (t/basic.t)\n";
is($return->stdout, $version_expect, "version plugin enabled");

$return = test_app('Test::MyCmd', [ qw(commands) ]);

for my $name (qw(commands frobulate hello justusage stock)) {
like($return->stdout, qr/^\s+\Q$name\E/sm, "$name plugin in listing");
}
unlike($return->stdout, qr/--version/, "version plugin not in listing");

{
my $return = test_app('Test::MyCmd', [ qw(exit 1) ]);
Expand Down
2 changes: 1 addition & 1 deletion t/callback.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ my $app = $CLASS->new;

is_deeply(
[ sort $app->command_names ],
[ sort qw(help --help -h -? commands lol) ],
[ sort qw(help --help -h --version -? commands lol) ],
"got correct list of registered command names",
);

Expand Down
2 changes: 2 additions & 0 deletions t/lib/Test/MyCmd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ use warnings;

use base qw(App::Cmd);

our $VERSION = '0.123';

1;
3 changes: 2 additions & 1 deletion t/setup-inner.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ my $app = $CLASS->new;

is_deeply(
[ sort $app->command_names ],
[ sort qw(help --help -h -? commands poot) ],
[ sort qw(help --help -h --version -? commands poot) ],
"got correct list of registered command names",
);

Expand All @@ -25,6 +25,7 @@ is_deeply(
[ qw(
App::Cmd::Command::commands
App::Cmd::Command::help
App::Cmd::Command::version
Test::WSOF::Command::poot
) ],
"got correct list of registered command plugins",
Expand Down
3 changes: 2 additions & 1 deletion t/setup-nocmd.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ my $app = $CLASS->new;

is_deeply(
[ sort $app->command_names ],
[ sort qw(help --help -h -? commands blort) ],
[ sort qw(help --help -h --version -? commands blort) ],
"got correct list of registered command names",
);

Expand All @@ -34,6 +34,7 @@ is_deeply(
[ qw(
App::Cmd::Command::commands
App::Cmd::Command::help
App::Cmd::Command::version
Test::WSNCC::Command::blort
) ],
"got correct list of registered command plugins",
Expand Down
3 changes: 2 additions & 1 deletion t/setup.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ my $app = $CLASS->new;

is_deeply(
[ sort $app->command_names ],
[ sort qw(help --help -h -? commands alfie bertie) ],
[ sort qw(help --help -h --version -? commands alfie bertie) ],
"got correct list of registered command names",
);

Expand All @@ -25,6 +25,7 @@ is_deeply(
[ qw(
App::Cmd::Command::commands
App::Cmd::Command::help
App::Cmd::Command::version
Test::WithSetup::Command::alfie
Test::WithSetup::Command::bertie
) ],
Expand Down