Subject: | commands via command line |
Even though Term::ShellUI is an interactive shell, there are advantages
if one can use it passing commands via command line and reusing the
parsing features of Term::ShellUI.
An example would probably help to explain:
myshell - invokes an interactive Term::ShellUI
myshell help - invokes the help command in Term::ShellUI and exits
myshell do something - invokes the 'do' command with parameter
'something' in Term::ShellUI and exits
Please find the attached patch. It would be very helpful for my project
if you can add this (or an improved version).
Kind Regards
Christian Kuelker
Subject: | patch-term-shellui-0.9-add-cmd-line-exec.diff |
diff -ruN Term-ShellUI-0.9-orig/lib/Term/ShellUI.pm Term-ShellUI-0.9-add-cmd-line-exec/lib/Term/ShellUI.pm
--- Term-ShellUI-0.9-orig/lib/Term/ShellUI.pm 2011-03-20 09:45:35.000000000 +0100
+++ Term-ShellUI-0.9-add-cmd-line-exec/lib/Term/ShellUI.pm 2011-05-16 12:07:54.000000000 +0200
@@ -773,6 +773,7 @@
sub process_a_cmd
{
my $self = shift;
+ my (@cmd)= @_; # commands via command line
$self->{completeline} = "";
my $OUT = $self->{'OUT'};
@@ -782,7 +783,15 @@
my $prompt = $self->prompt();
$prompt = $prompt->[length $rawline ? 1 : 0] if ref $prompt eq 'ARRAY';
$prompt = $prompt->($self, $rawline) if ref $prompt eq 'CODE';
- my $newline = $self->{term}->readline($prompt);
+
+ # dispatch cmd via cmd-line or loop
+ my $newline = undef;
+ if(scalar @cmd) {
+ $newline = join q{ }, @cmd;
+ $self->exit_requested(1);
+ } else {
+ $newline = $self->{term}->readline($prompt);
+ }
# EOF exits
unless(defined $newline) {
@@ -867,11 +876,12 @@
sub run
{
my $self = shift;
+ my (@cmd)= @_; # commands via command line
$self->load_history();
while(!$self->{done}) {
- $self->process_a_cmd();
+ $self->process_a_cmd(@cmd);
}
$self->save_history();