App-Rad-Plugin-ReadLine

 view release on metacpan or  search on metacpan

example/01-myapp  view on Meta::CPAN

#! /usr/bin/perl
use warnings; use strict;             # boring
use lib qw[ lib ];                    # boring
use App::Rad::Plugin::ReadLine::Demo; # boring
use App::Rad qw[ ReadLine ];
App::Rad->run();
sub turtles :Help('do it in the shell'){
    my $c = shift;
    $c->shell({
        GreetWithCommand => '',  # use what App::Rad decides is the default
        ShellPrompt => 'c/,,\\'  # ascii turtle for the prompt
    });
}

lib/App/Rad/Plugin/ReadLine.pm  view on Meta::CPAN



use warnings; 
use strict;

our $GreetWithCommand = 'help';  # when the shell starts, run this command
our $GreetWithSub             ;  # you can pass in a sub too,  both GreetWith's are run
our $DefaultCommand   = ''    ; 
our $ShellPrompt      = "[" . $0 . "]";

# we keep prompting until this is false
# (we register "exit" as a command to set it to 0 upon entering shell)
our $still_going = 1;


use Term::UI;
use Term::ReadLine;


our $term;
sub _terminal { 
    $term ||= Term::ReadLine->new($ENV{TERM} || 'critter');
}

sub _shell_prompt   {
    # add a space to the end
    $ShellPrompt =~ / $/?  $ShellPrompt : "$ShellPrompt " 
}
sub _shell_help     { "run $0 in interactive mode" };
sub _shell_command  { 'shell' } 


sub shell_options { 
    my $c = shift;

lib/App/Rad/Plugin/ReadLine.pm  view on Meta::CPAN

    }, "exit the $0 shell" );

    $c->unregister_command('shell');# Xhibit forbidden

    my $welcome = \&_welcome;
    $c->$welcome();

    while($still_going) {
        (my $cmd, local @ARGV) = split  ' ',
        _terminal->get_reply(
              prompt => _shell_prompt(),
              default => $DefaultCommand,
        );
        if (defined $cmd and $cmd ne '') { 
            @{$c->argv} = @ARGV;
            $c->{'cmd'} = $cmd;

            $c->debug('received command: ' . $c->{'cmd'});
            $c->debug('received parameters: ' . join (' ', @{$c->argv} ));
        }
        $c->_tinygetopt();

lib/App/Rad/Plugin/ReadLine.pm  view on Meta::CPAN

#see ./example/01-myapp

  #! /usr/bin/perl
  #...
  use App::Rad qw[ ReadLine ];
  App::Rad->run();
  sub turtles :Help('do it in the shell'){
      my $c = shift;
      $c->shell({
          GreetWithCommand => '',  # use what App::Rad decides is the default
          ShellPrompt => 'c/,,\\'  # ascii turtle for the prompt
      });
  }

#end of listing

#running ./example/01-myapp demo turtles exit

  Usage: ./example/01-myapp command [arguments]
  
  Available Commands:

lib/App/Rad/Plugin/ReadLine.pm  view on Meta::CPAN


=head3 C<\%options>'s keys are:

C<GreetWithCommand> => run this App::Rad command when the shell starts

C<GreetWithSub>     => run this sub-ref (as a method on $c) when the shell starts

C<DefaultCommand>   => if the user doesn't enter a command, they entered this.
'' will use App::Rad's default command.

C<ShellPrompt>      => what to prompt the user with, defautl is "$0 "

=head3 C<[$command_name, [ $command_help ]]>:

C<$command_name> is the name of the sub-comamnd name

C< $command_help> is the help to pass

... both are passed to C<< $c->register >>, along with C<\&shell>

=head1 BUGS

lib/App/Rad/Plugin/ReadLine/Demo.pm  view on Meta::CPAN


sub demo  #   : Help('run shell commands from @ARGV')
{
    my $c=shift;
    no warnings qw[ redefine ];
    my @answers = (@ARGV, qw[ exit ]);
    *Term::UI::get_reply = sub { 
        my $self= shift;
        my %args = @_;
        my $answer = shift @answers;
        print $args{prompt},$answer, "\n";
        $answer;
    };
    if (@answers) { 
        local $c->{'cmd'} = shift @answers;
        $c->execute();
    }
    else { 
        $c->shell();
    }
    ''



( run in 0.879 second using v1.01-cache-2.11-cpan-6aa56a78535 )