App-Rad
view release on metacpan or search on metacpan
lib/App/Rad.pm view on Meta::CPAN
);
The code above will register as commands all subs with names B<not> ending in 'foo', but it B<will> register the 'foo' sub as well. It will also give the 'bar' command the help string. This behavior is handy for registering several commands and havin...
You don't have to worry about the order of your elements passed, App::Rad will figure them out for you in a DWIM fashion.
# this does the same as the code above
$c->register_commands(
{ bar => 'all your command line are belong to us' },
'foo',
{ -ignore_suffix => 'foo' },
);
You can even bundle the hash reference to include your C<< cmd => help >> and special keys:
# this behaves the same way as the code above:
$c->register_commands(
'foo',
{
-ignore_suffix => 'foo',
bar => 'all your command line are belong to us',
}
);
=head2 $c->unregister_command ( I<NAME> )
Longer alias for C<< $c->unregister() >>. The use of the shorter form is encouraged, and this alias may be removed in future versions. You have been warned.
=head3 $c->unregister ( I<NAME> )
Unregisters a given command name so it's not available anymore. Note that the subroutine will still be there to be called from inside your program - it just won't be accessible via command line anymore.
=head2 $c->debug( I<MESSAGE> )
Will print the given message on screen only if the debug flag is enabled:
use App::Rad qw( debug );
Note that, if debug is enabled, App::Rad itself will print several debug messages stating its current flow, so you can easily find out where everything is happening.
=head2 $c->plugins()
Returns a list of all loaded plugins, in the order in which they were loaded.
=head2 $c->load_plugin( I<PLUGIN NAME> )
This method will dinamically load the given plugin. The plugin needs to be under the C<< App::Rad::Plugin >> namespace, and the name should be relative to this path (i.e. $c->load_plugin('MyPlugin') will try to load 'App::Rad::Plugin::MyPlugin'). If ...
=head1 CONTROL FUNCTIONS (to possibly override)
App::Rad implements some control functions which are expected to be overridden by implementing them in your program. They are as follows:
=head2 setup()
This function is responsible for setting up what your program can and cannot do, plus everything you need to set before actually running any command (connecting to a database or host, check and validate things, download a document, whatever). Note th...
Another interesting thing you can do with setup is to manipulate the command list. For instance, you may want to be able to use the C<include> and C<exclude> commands, but not let them available for all users. So instead of writing:
use App::Rad qw(include exclude);
App::Rad->run();
you can write something like this:
use App::Rad;
App::Rad->run();
sub setup {
my $c = shift;
$c->register_commands();
# EUID is 'root'
if ( $> == 0 ) {
$c->register('include', \&App::Rad::include);
$c->register('exclude', \&App::Rad::exclude);
}
}
to get something like this:
[user@host]$ myapp.pl help
Usage: myapp.pl command [arguments]
Available Commands:
help
[user@host]$ sudo myapp.pl help
Usage: myapp.pl command [arguments]
Available Commands:
exclude
help
include
=head2 default()
If no command is given to your application, it will fall in here. Please note that invalid (non-existant) command will fall here too, but you can change this behavior with the invalid() function below (although usually you don't want to).
Default's default (grin) is just an alias for the help command.
sub default {
my $c = shift;
# will fall here if the given
# command isn't valid.
}
You are free (and encouraged) to change the default behavior to whatever you want. This is rather useful for when your program will only do one thing, and as such it receives only parameters instead of command names. In those cases, use the "C<< defa...
=head2 invalid()
This is a special function to provide even more flexibility while creating your command line applications. This is called when the user requests a command that does not exist. The built-in C<< invalid() >> will simply redirect itself to C<< default()...
=head2 teardown()
( run in 0.512 second using v1.01-cache-2.11-cpan-39bf76dae61 )