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 );



    =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.


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.



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


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


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.

See Log::Dump for detailed instrution.

If you want to use other loggers, write your own log method, and Log::Dump will not be loaded (since 0.15).

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.


creates a command object. It may take a hash of options.


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


Kenichi Ishigaki, <>


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.