Last active
June 1, 2019 17:16
-
-
Save jberger/a0139ebe8390bb208e21a12e0e930273 to your computer and use it in GitHub Desktop.
A simple CLI app runner with commands in another namespace
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use feature 'say'; | |
package CLI; | |
use Moo; | |
use Carp (); | |
has commands => ( | |
is => 'ro', | |
default => 'CLI::Commands', | |
); | |
sub run { | |
my ($self, $command) = (shift, shift); | |
my $sub = $self->commands->can($command); | |
Carp::croak "No such command: $command" unless $sub; | |
return $sub->(@_); | |
} | |
package CLI::Commands; | |
sub greet { | |
my $name = shift || 'World'; | |
say "Hello $name!"; | |
} | |
package main; | |
my $cli = CLI->new; | |
$cli->run(@ARGV); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ perl cli_app.pl greet Dave Hello Dave!