Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 17a0869f

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 17a0869fc518978c148ff7d376039133de9bfe07
  • Vorgänger 128f3100
  • Nachfolger de66b95f

Console: Diverse Kommandozeilen und Optionen

Z.B. "-e code" zum direkten Ausführen von Code oder "-f filename" zum
Ausführen von Code aus einer Datei.

Unterschiede anzeigen:

scripts/console
11 11

  
12 12
use Data::Dumper;
13 13
use Devel::REPL 1.002001;
14
use File::Slurp;
15
use Getopt::Long;
16
use Pod::Usage;
14 17
use Term::ReadLine::Perl::Bind;     # use sane key binding for rxvt users
15 18

  
16 19
use SL::LxOfficeConf;
17 20
SL::LxOfficeConf->read;
18 21

  
19
my $login        = shift || $::lx_office_conf{console}{login}        || 'demo';
20
my $history_file =          $::lx_office_conf{console}{history_file} || '/tmp/lxoffice_console_history.log'; # fallback if users is not writable
21
my $debug_file   =          $::lx_office_conf{console}{log_file}     || '/tmp/lxoffice_console_debug.log';
22
my $autorun      =          $::lx_office_conf{console}{autorun};
22
my $login        = $::lx_office_conf{console}{login}        || 'demo';
23
my $history_file = $::lx_office_conf{console}{history_file} || '/tmp/kivitendo_console_history.log'; # fallback if users is not writable
24
my $debug_file   = $::lx_office_conf{console}{log_file}     || '/tmp/kivitendo_console_debug.log';
25
my $autorun      = $::lx_office_conf{console}{autorun};
26
my ($execute_code, $execute_file, $help, $man);
27

  
28
my $result = GetOptions(
29
  "login|l=s"        => \$login,
30
  "history-file|i=s" => \$history_file,
31
  "log-file|o=s"     => \$debug_file,
32
  "execute|e=s"      => \$execute_code,
33
  "file|f=s"         => \$execute_file,
34
  "help|h"           => \$help,
35
  "man"              => \$man,
36
);
37
pod2usage(2)                               if !$result;
38
pod2usage(1)                               if $help;
39
pod2usage(-exitstatus => 0, -verbose => 2) if $man;
23 40

  
24 41
# will be configed eventually
25 42
my @plugins      = qw(History LexEnv Colors MultiLine::PPI FancyPrompt PermanentHistory AutoloadModules);
26 43

  
44
sub execute_code {
45
  my ($repl, $code) = @_;
46

  
47
  my $result = $repl->eval($code);
48
  if (ref($result) eq 'Devel::REPL::Error') {
49
    $repl->print($result->message);
50
    return 0;
51
  }
52
  if ($@) {
53
    $repl->print($@);
54
    return 0;
55
  }
56

  
57
  return 1;
58
}
59

  
27 60
my $repl = Devel::REPL->new;
28 61
$repl->load_plugin($_) for @plugins;
29 62
$repl->load_history($history_file);
30 63
$repl->eval('help');
31 64
$repl->print("trying to auto login as '$login'...");
32 65
$repl->print($repl->eval("lxinit '$login'"));
33
if ($autorun) {
34
  my $result = $repl->eval($autorun);
35
  $repl->print($result->message) if ref($result) eq 'Devel::REPL::Error';
36
}
66

  
67
my @code_to_execute = grep { $_ } ($autorun, $execute_code, $execute_file ? join('', read_file($execute_file)) : undef);
68
execute_code($repl, $_) || exit 1 for @code_to_execute;
69
exit  if $execute_code || $execute_file;
70

  
37 71
$repl->run;
38 72

  
39 73
package Devel::REPL;
......
180 214

  
181 215
=head1 SYNOPSIS
182 216

  
183
  ./script/console
217
  ./script/console [options]
184 218
  > help               # displays a brief documentation
185 219

  
220
=head1 OPTIONS
221

  
222
The list of supported command line options includes:
223

  
224
=over 8
225

  
226
=item B<--help>, B<-h>
227

  
228
Print this help message and exit.
229

  
230
=item B<--man>
231

  
232
Print the manual page and exit.
233

  
234
=item B<-l>, B<--login>=C<username>
235

  
236
Log in as C<username>. The default is to use the value from the
237
configuration file and C<demo> if none is set there.
238

  
239
=item B<-o>, B<--log-file>=C<filename>
240

  
241
Use C<filename> as the log file. The default is to use the value from
242
the configuration file and C</tmp/kivitendo_console_debug.log> if none
243
is set there.
244

  
245
=item B<-i>, B<--history-file>=C<filename>
246

  
247
Use C<filename> as the history file for commands input by the
248
user. The default is to use the value from the configuration file and
249
C</tmp/kivitendo_console_history.log> if none is set there.
250

  
251
=item B<-e>, B<--execute>=C<perl-code>
252

  
253
Execute this code on startup and exit afterwards.
254

  
255
=item B<-f>, B<--file>=C<filename>
256

  
257
Execute the code from the file C<filename> on startup and exit
258
afterwards.
259

  
260
=back
261

  
186 262
=head1 DESCRIPTION
187 263

  
188 264
Users of Ruby on Rails will recognize this as a perl reimplementation of the

Auch abrufbar als: Unified diff