Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active June 1, 2019 17:16
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 jberger/a0139ebe8390bb208e21a12e0e930273 to your computer and use it in GitHub Desktop.
Save jberger/a0139ebe8390bb208e21a12e0e930273 to your computer and use it in GitHub Desktop.
A simple CLI app runner with commands in another namespace
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);
@jberger
Copy link
Author

jberger commented Jun 1, 2019

$ perl cli_app.pl greet Dave
Hello Dave!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment