NAME

CLI::Dispatch::Command

SYNOPSIS

package MyScript::Convert;

use strict;
use base 'CLI::Dispatch::Command';
use Encode;

sub options {qw( from=s to=s )}

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

  die $self->usage(1) unless @args;

  # this message will be printed when "verbose" option is set
  $self->log( info => 'going to convert encoding' );

  # debug message will be printed only when "debug" option is set
  $self->log( debug => 'going to convert encoding' );

  my $decoded = decode( $self->option('from'), $args[0] );
  print encode( $self->option('to'), $decoded );
}

1;

__END__

=head1 NAME

MyScript::Convert - this will be shown in a command list

=head1 SYNOPSIS

the following will be shown when you run the script
with a "help" option/command.

DESCRIPTION

CLI::Dispatch::Command is a base class for an actual command. Basically, all you need to do is override the run method to let it do what you want, and optionally override the options method to provide option specifications for the command. Also, you are expected to write a decent pod for the command, which will be shown when you run a script with help option/command, or when you run it without any command.

METHODS

run

this is where you are expected to write what you want the command to do.

options

this is where you are expected to write an array of command-specific option specifications.

option

is a read-only accessor to the option of the name (returns an empty string if the option is not defined).

log, logger, logfile, logfilter

CLI::Dispatch uses Log::Dump as a logger, and the logger is enabled when the verbose option is set.

log takes a label and arbitrary messages (strings, references and objects), and dumps them to stderr by default.

$self->log( label => @messages );

If you want to dump to a file, pass the file name to logfile, and if you want to dump only messages with selected labels, use logfilter. If you want to use other loggers like Log::Dispatch, pass its instance to logger.

See Log::Dump for detailed instrution.

check (since 0.05)

will be executed before run, mainly to see if the command is really available for the user. If the command happens to die there, the dying message will also be shown in the commands list.

package MyScript::UnportableCommand;

use strict;
use base 'CLI::Dispatch::Command';

sub check {
  my $self = shift;

  eval "require Module::For::Unix::Only";
  die "Unsupported OS" if $@;  # for a better message in the list
}

sub run { ... }

usage (since 0.07)

will print the first section of the pod for the command. If you pass a true value as the first argument, it just returns the usage text instead without printing.

run_directly (since 0.09)

loads CLI::Dispatch to dispatch directly back to the command. This is handy if you prefer writing a set of independent scripts to writing one dispatcher script.

new

creates a command object.

set_options

takes a hash of options from the dispatcher, and set them into the command object.

AUTHOR

Kenichi Ishigaki, <ishigaki@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2008 by Kenichi Ishigaki.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.