Cmd::Interpreter - Support for line-oriented command interpreters
use Cmd::Interpreter;
our @ISA = qw(Cmd::Interpreter);
Cmd::Interpreter provides a simple framework for writing line-oriented command interpreters.
-
Write your class
package Example::Hello; use strict; use warnings; use Cmd::Interpreter; our @ISA = qw(Cmd::Interpreter); sub help { my $self = shift; print "common help\n"; return ''; } sub do_hello { my $self = shift; print "Hello " . (shift || "World") . "!\n"; return ''; } sub help_hello { my $self = shift; print "help for hello\n"; return ''; } sub do_quit { my $self = shift; print "By\n"; return "quit"; } sub empty_line { } 1;
-
Use your class
#!/usr/bin/env perl use strict; use warnings; use Example::Hello; my $ex = Example::Hello->new(prompt => 'example> '); $ex->run("Welcome to hello world app.");
You can pass program name as prog_name
, prompt as prompt
.
Loop stoping if function returns true value aka stop flag
.
-
do_foo
Will execute on command 'foo'.
-
help_foo
Will execute on command '?foo' or 'help foo'.
-
help
Will execute when input is '?' or 'help'.
-
pre_loop
Will execute before loop.
-
post_loop
Will execute after loop.
-
pre_cmd
Receive input line, return one (can be changed).
-
post_cmd
Receive stop flag, line (from pre_cmd). Return stop flag.
-
default_action
Will execute when input command not exists.
-
empty_line
Will execute when input defined but empty. By default execute last command if one exists.
-
no_input
Will execute when input undefined.
-
do_shell
Will execute when input is '!cmd [args]' or 'shell cmd [args]'.
Command history works fine with such module like Term::ReadLine::Perl.
You can install Cmd::Interpreter from cpanm git@github.com:oakulikov/Cmd-Interpreter.git
.
Oleg Kulikov oakulikov@yandex.ru
Authors of Python Lib/cmd.py
Copyright (C) Oleg Kulikov.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.