PrePAN

Sign in to PrePAN

Term::CLI CLI interpreter based on Term::ReadLine

Good

Synopsis

use 5.014_001;
use Term::CLI;

my $cli = Term::CLI->new(
    name => 'myapp',
    prompt => 'demo> ',
    skip => qr/^\s*(?:#.*)$/,
);

$cli->add_command(
    Term::CLI::Command::Help->new,
    Term::CLI::Command->new(
        name => 'exit',
        summary => 'exit the program with I<excode>',
        description => 'Exit the program with I<excode> if given, else 0.',
        arguments =>  [
            Term::CLI::Argument::Number::Int->new(
                name => 'excode',
                min_occur => 0,
                min => 1,
            ),
        ],
        callback => sub {
            my ($cmd, %args) = @_;
            return %args if $args{status} < 0;
            my $excode = $args{arguments}->[0] // 0;
            say "-- exit: $excode";
            exit $excode;
        },
    )
);

while (defined (my $l = $cli->readline)) {
    $cli->execute($l);
}

Description

Implement an easy-to-use command line interpreter based on Term::ReadLine and Term::ReadLine::Gnu.

First-time users may want to read the documentation for Term::CLI::Tutorial and Term::CLI::Intro first, and peruse the example scripts in the source distribution's examples and tutorial directories.

Comments

Thank you for bringing attention to this module! I'm going to try using it in my REPLA project and will provide feedback.

Please sign up to post a review.