NAME

App::CLI - Dispatcher module for command line interface programs

SYNOPSIS

package MyApp;
use base 'App::CLI';        # the DISPATCHER of your App
                            # it's not necessary to put the dispatcher
                            # on the top level of your App

package main;

MyApp->dispatch;            # call the dispatcher where you want


package MyApp::List;
use base qw(App::CLI::Command); # any (SUB)COMMAND of your App

use constant options => (
    "h|help"   => "help",
    "verbose"  => "verbose",
    'n|name=s'  => 'name',
);

use constant subcommands => qw(User Nickname type); # if you want subcommands
                                                    # automatically dispatch to subcommands
                                                    # when invoke $ myapp list [user|nickname|--type]
                                                    # note 'type' is not capitalized
                                                    # it is a deprecated subcommand

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

    print "verbose" if $self->{verbose};
    my $name = $self->{name}; # get arg following long option --name

    if ($self->{help}) {
        # if $ myapp list --help or $ myapp list -h
        # only output PODs
    } else {
        # do something when invoking $ myapp list
        # without subcommand and --help
    }
}


package MyApp::List::User;
use base qw(App::CLI::Command);
use constant options => (
    "h|help"  =>  "help",
);

sub run {
    my ($self,@args) = @_;
    # code for listing user
}


pakcage MyApp::List::Nickname;
use base qw(App::CLI::Command);
use constant options => (
    "sort=s"  =>  "sort",
);

sub run {
    my ($self,@args) = @_;
    # code for listing nickname
}


package MyApp::List::type;   # old genre of subcommand could not cascade infinitely
use base qw(MyApp::List);    # should inherit its parent's command

sub run {
    my ($self, @args);
    # run to here when invoking $ myapp list --type
}


package MyApp::Help;
use base 'App::CLI::Command::Help';

use constant options => (
    'verbose' => 'verbose',
);

sub run {
    my ($self, @arg) = @_;
    # do something
    $self->SUPER(@_); # App::CLI::Command::Help would output POD of each command
}

DESCRIPTION

App::CLI dispatches CLI (command line interface) based commands into command classes. It also supports subcommand and per-command options.

Methods

get_opt([@config], %opt_map)

Give options map, processed by Getopt::Long::Parser.

dispatch(@args)

Interface of dispatcher

cmd_map($cmd)

Find the name of the package implementing the requested command.

The command is first searched for in alias. If the alias exists and points to a package name starting with the + sign, then that package name (minus the + sign) is returned. This makes it possible to map commands to arbitrary packages.

Otherwise, the package is searched for in the result of calling commands, and a package name is constructed by upper-casing the first character of the command name, and appending it to the package name of the app itself.

If both of these fail, and the command does not map to any package name, undef is returned instead.

get_cmd($cmd, @arg)

Return subcommand of first level via $ARGV[0].

SEE ALSO

AUTHORS

  • Chia-liang Kao <clkao@clkao.org>

  • Alex Vandiver <alexmv@bestpractical.com>

  • Yo-An Lin <cornelius.howl@gmail.com>

  • Shelling <navyblueshellingford@gmail.com>

  • Paul Cochrane <paul@liekut.de> (current maintainer)

CONTRIBUTORS

The following people have contributed patches to the project:

  • José Joaquín Atria <jjatria@gmail.com>

  • sunnavy <sunnavy@gmail.com>

  • Ildar Shaimordanov <ildar.shaimordanov@gmail.com>

COPYRIGHT

Copyright 2005-2010 by Chia-liang Kao <clkao@clkao.org>. Copyright 2010 by Yo-An Lin <cornelius.howl@gmail.com> and Shelling <navyblueshellingford@gmail.com>. Copyright 2017-2018 by Paul Cochrane <paul@liekut.de>

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

See http://www.perl.com/perl/misc/Artistic.html