View on
Karl Erisman > CLI-Framework-0.05 > CLI::Framework



Annotate this POD


New  1
Open  1
View/Report Bugs
Module Version: 0.05   Source  


CLI::Framework - Build standardized, flexible, testable command-line applications


CLI::Framework ("CLIF") provides a framework and conceptual pattern for building full-featured command line applications. It intends to make this process simple and consistent. It assumes the responsibility of implementing details that are common to all command-line applications, making it possible for new applications adhering to well-defined conventions to be built without the need to repeatedly write the same command-line interface code.

For instance, a complete application supporting commands and subcommands, with options and arguments for the application itself as well as its commands, can be built by writing concise, understandable code in packages that are easy to test and maintain. The classes can focus on implementation of unique aspects essential to the command's purpose without being concerned with the many details involved in building an interface around those commands. This methodology for building command-line applications also establishes a valuable standard for an organization (or an individual developer).


CLIF has a rich set of features and offers many alternative approaches to building applications, but if you are new to using it, you may want a succinct introduction. For this reason, the CLI::Framework::Tutorial is provided and is the recommended starting point.

After you gain a basic understanding, the other documents can be used as references.


There are a few other distributions on CPAN intended to simplify building modular command line applications. I have not found any that meet my requirements, which are documented in DESIGN GOALS AND FEATURES.


CLIF was designed to offer the following features...



When a command of the form:

    $ app [app-opts] <cmd> [cmd-opts] { <cmd> [cmd-opts] {...} } [cmd-args]


            app      |             [app-opts]            { <cmd>       |   [cmd-opts]    } [cmd-args]
    $ examples/queue |--qin=/tmp/qfile --qout=/tmp/qfile | enqueue     | --tag=x --tag=y | 'item'
    $ gen-report     |              --html               | stats       |  --role=admin   |
                     |                                   | usage       |   --time='2d'   | '/tmp/stats.html'

...causes your application script, <app>, to invoke the run method in your application class, CLI::Framework::Application performs the following actions:

  1. Parse the command request
  2. Validate application options
  3. Initialize application
  4. Prepare command
  5. Invoke command pre-dispatch hook
  6. Dispatch command

These steps are explained in more detail below...

Request parsing

Parse the application options [app-opts], command name <cmd>, command options [cmd-opts], and the remaining part of the command line, which includes command arguments [cmd-args] for the last command and may include multiple subcommands. Everything between the inner brackets ({ ... }) represents recursive subcommand processing -- the "..." represents another string of "<cmd> [cmd-opts] {...}".

The second example above shows a command request that requires recursive subcommand processing. The command might cause an HTML report to be generated with usage statistics for admin users (of some application) for the past two days, writing the report to a file. In one line, it would look like this:

    $ gen-report --html stats --role=admin usage --time='2d' '/tmp/stats.html'

This fictional gen-report application could be designed with such an interface because it could offer various types of reports (as opposed to the statistics report). There might be other statistics reports (as opposed to 'usage'). The stats might be available for users with other roles. The usage report might need to accept custom time frames.

CLIF allows you to choose whether various parts of your data should be supplied as options or as arguments -- these interface decisions are left to your discretion. CLIF also makes it easy to validate command requests and to provide usage information so users know what to change if a command request fails validation.

In general, if a command request is not well-formed, it is replaced with the default command and any arguments present are ignored. The default command prints a help or usage message (you may change this behavior if desired).

Validation of application options

Your application class can optionally define the validate_options method.

If your application class does not override this method, validation is skipped -- any received options are considered to be valid.

Application initialization

Your application class can optionally override the init method. This is a hook that can be used to perform any application-wide initialization that needs to be done independent of individual commands. For example, your application may use the init method to connect to a database and store a connection handle which may be needed by some or all of the commands in your application.

Preparing the command

The requested command is now loaded (if not already done). The command's cache is set (using a reference to the same cache object used by the application).

Command pre-dispatch

Your application class can optionally have a pre_dispatch method that is called with one parameter: the Command object that is about to be dispatched.

Dispatching the command

CLIF uses the dispatch method to actually dispatch a specific command. That method is responsible for running the command or delegating responsibility to a subcommand, if applicable.


After building your CLIF application, in addition to basic non-interactive functionality, you will instantly benefit from the ability to (optionally) run your application in interactive mode. A readline-enabled application command console with an event loop, a command menu, and built-in debugging commands is provided by default.

Inside interactive mode, only steps 4, 5, and 6 above (APPLICATION RUN SEQUENCE) are performed for each command request.

Supporting interactivity in your application is as simple as adding the built-in command CLI::Framework::Command::Console to your command_map.


This distribution comes with some default built-in commands, and more CLIF built-ins can be installed as they become available on CPAN.

Use of the built-ins is optional in most cases, but certain features require specific built-in commands (e.g. the Help command is a fundamental feature of all applications and the Menu command is required in interactive mode). You can override any of the built-ins.

A new application that does not override the command_map hook will include all of the built-ins listed below.

The existing built-ins and their corresponding packages are as follows:

help: Print application or command-specific usage messages


Note: This command is registered automatically. All CLIF applications must have the "help" command defined (though this built-in can replaced by your subclass to change the "help" command behavior or to do nothing if you specifically do not want a help command).

list: Print a list of commands available to the running application


dump: Show the internal state of a running application


tree: Display a tree representation of the commands that are currently registered with the running application


alias: Display the command aliases that are in effect for the running application and its commands


console: Invoke CLIF's interactive mode


menu: Show a command menu including the commands that are available to the running application


Note: This command is registered automatically when an application runs in interactive mode. This built-in may be replaced by a user-defined "menu" command, but any command class to be used for the "menu" command MUST be a subclass of this one.


The class diagram below shows the relationships of the major classes of CLI Framework, including some of their methods. This is not intended to be a comprehensive diagram, only an aid to understanding CLIF at a glance.






Copyright (c) 2009 Karl Erisman ( All rights reserved.

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


Karl Erisman (

syntax highlighting: