File Coverage

lib/Devel/Trepan/CmdProcessor/Command/Subcmd/Core.pm
Criterion Covered Total %
statement 76 146 52.0
branch 4 34 11.7
condition 1 5 20.0
subroutine 20 35 57.1
pod n/a
total 101 220 45.9


line stmt bran cond sub pod time code
1             # -*- coding: utf-8 -*-
2             # Copyright (C) 2011-2015 Rocky Bernstein <rocky@cpan.org>
3             # A base class for debugger subcommands.
4             #
5 12     12   25655 use Exporter;
  12         33  
  12         497  
6 12     12   71 use warnings;
  12         31  
  12         317  
7 12     12   65 no warnings 'redefine';
  12         28  
  12         415  
8              
9 12     12   72 use rlib '../../../../..';
  12         39  
  12         88  
10 12     12   4995 use Devel::Trepan::CmdProcessor::Command;
  12         31  
  12         1674  
11              
12             package Devel::Trepan::CmdProcessor::Command::Subcmd;
13 12     12   964 use Devel::Trepan::CmdProcessor::Validate;
  12         41  
  12         620  
14              
15             BEGIN {
16 12     12   242 @SUBCMD_VARS = qw($HELP $IN_LIST $RUN_CMD $MIN_ABBREV
17             $MIN_ARGS $MAX_ARGS
18             $NAME $SHORT_HELP @SUBCMD_VARS @SUBCMD_ISA);
19             }
20 12     12   73 use strict;
  12         30  
  12         428  
21              
22             my $NotImplementedMessage =
23             "This method must be overridden in a subclass";
24              
25 12     12   71 use vars qw(@SUBCMD_VARS @EXPORT @ISA @SUBCMD_ISA);
  12         30  
  12         706  
26 12     12   61 use vars @SUBCMD_VARS;
  12         26  
  12         1409  
27             @ISA = qw(Exporter);
28              
29             @SUBCMD_ISA = qw(Devel::Trepan::CmdProcessor::Command::Subcmd);
30             @EXPORT = @SUBCMD_VARS;
31              
32             $IN_LIST = 1; # Show item in help list of commands
33             $RUN_CMD = 1; # Run subcommand for those subcommands like "show"
34             # which append current settings to list output.
35 12     12   80 use constant MIN_ARGS => 0;
  12         33  
  12         698  
36 12     12   85 use constant MAX_ARGS => 0;
  12         29  
  12         571  
37 12     12   74 use constant NEED_STACK => 0;
  12         31  
  12         13254  
38              
39             $MIN_ABBREV = 1;
40             $NAME = 'your_command_name';
41              
42              
43             # $cmd contains the command object that this
44             # command is invoked through. A debugger field gives access to
45             # the stack frame and I/O.
46             sub new($$$)
47             {
48 464     464   1697 my ($class, $cmd, $name) = @_;
49 464         1618 my $self = {cmd => $cmd};
50              
51             # Convenience class access. We don't expect that any of these
52             # will change over the course of the program execution like
53             # errmsg(), msg(), and msg_nocr() might. (See the note below
54             # on these latter 3 methods.)
55             #
56 464         1529 $self->{dbgr} = $cmd->{dbgr};
57 464         1291 $self->{proc} = $cmd->{proc};
58              
59             # FIXME: Inheritence of vars is not working the way I had hoped.
60             # So this is a workaround.
61 464         1062 my $base_prefix="Devel::Trepan::CmdProcessor::Command::Subcmd::";
62 464         1436 for my $field (@SUBCMD_VARS) {
63 4640         12884 my $sigil = substr($field, 0, 1);
64 4640 50       14497 my $new_field = index('$@', $sigil) >= 0 ? substr($field, 1) : $field;
65 4640 100       12384 if ($sigil eq '$') {
    50          
66 3712         192127 $self->{lc $new_field} =
67             eval "\$${class}::${new_field} || \$${base_prefix}${new_field}";
68             } elsif ($sigil eq '@') {
69 928         44695 $self->{lc $new_field} = eval "[\@${class}::${new_field}]";
70             } else {
71 0         0 die "Woah - bad sigil: $sigil";
72             }
73             }
74             # Done after above since $NAME is in @SUBCMD_VARS;
75 464         1527 $self->{name} = $name;
76 464   33     1708 $self->{short_help} ||= $self->{help};
77 464         1449 bless $self, $class;
78 464         3680 $self->set_name_prefix($class);
79 464         11325 $self;
80             }
81              
82             # Convenience short-hand for $proc->confirm
83             sub confirm($$;$) {
84 0     0   0 my ($self, $msg, $default) = @_;
85 0         0 return($self->{proc}->confirm($msg, $default));
86             }
87              
88             # Set a Boolean-valued debugger setting.
89             sub run_set_bool($$;$)
90             {
91 0     0   0 my ($self, $args, $default) = @_;
92 0 0       0 $default = 1 if scalar @_ < 3;
93 0 0       0 my $onoff_arg = @$args < 3 ? 'on' : $args->[2];
94 0         0 my $key = $self->subcmd_setting_key();
95 0         0 $self->{proc}{settings}{$key} = $self->{proc}->get_onoff($onoff_arg);
96 0         0 $self->run_show_bool();
97             }
98              
99             # set an Integer-valued debugger setting.
100             sub run_set_int($$$;$$)
101             {
102 0     0   0 my ($self, $arg, $msg_on_error, $min_value, $max_value) = @_;
103 0 0       0 if ($arg =~/^\s*$/) {
104 0         0 $self->{proc}->errmsg('You need to supply a number.');
105 0         0 return undef;
106             }
107 0         0 my $val = $self->{proc}->get_an_int($arg,
108             {max_value => $max_value,
109             min_value => $min_value,
110             msg_on_error => $msg_on_error
111             });
112 0 0       0 if (defined ($val)) {
113 0         0 $self->{settings}{subcmd_setting_key} = $val;
114 0         0 $self->run_show_int();
115             }
116             }
117              
118             # Generic subcommand showing a boolean-valued debugger setting.
119             sub run_show_bool($;$)
120             {
121 0     0   0 my ($self, $what) = @_;
122 0         0 my $key = $self->subcmd_setting_key();
123 0         0 my $val = $self->show_onoff($self->{proc}{settings}{$key});
124 0 0       0 $what = $self->{name} unless $what;
125 0         0 $self->{proc}->msg(sprintf "%s is %s.", $what, $val);
126             }
127              
128             # Generic subcommand integer value display
129             sub run_show_int($;$)
130             {
131 0     0   0 my ($self, $what) = @_;
132 0         0 my $val = $self->{settings}{$self->subcmd_setting_key()};
133 0 0       0 unless ($what) {
134 0         0 my @what = @$self->{prefix};
135 0         0 pop @what;
136 0         0 $what = join(' ', @what);
137             }
138 0         0 $self->msg(sprintf "%s is %d.", $what, $val);
139             }
140              
141             # Generic subcommand value display. Pass in a hash which may
142             # which optionally contain:
143             #
144             # :name - the String name of key in settings to use. If :value
145             # (described below) is set, then setting :name does
146             # nothing.
147             #
148             # :what - the String name of what we are showing. If none is
149             # given, then we use the part of the SHORT_HELP string.
150             #
151             # :value - a String value associated with "what" above. If none
152             # is given, then we pick up the value from settings.
153             #
154             sub run_show_val($;$)
155             {
156 0     0   0 my ($self, $opts) = @_;
157 0   0     0 $opts ||= {};
158 0 0       0 my $what = exists $opts->{what} ? $opts->{what} : $self->{string_in_show};
159 0 0       0 my $name = exists $opts->{name} ? $opts->{name} : $self->{name};
160 0 0       0 my $val = exists $opts->{value} ? $opts->{value} : $self->{settings}{$name};
161 0         0 my $msg = sprintf("%s is %s.", $what, $val);
162 0         0 $self->msg($msg);
163             }
164              
165             # sub save_command_from_settings
166             # ["${subcmd_prefix_string} ${settings[subcmd_setting_key]}"]
167             # }
168              
169             sub subcmd_prefix_string($)
170             {
171 0     0   0 my $self = shift;
172 0         0 join(' ', $self->{prefix});
173             }
174              
175             sub subcmd_setting_key($)
176             {
177 0     0   0 my $self = shift;
178 0 0       0 return $self->{subcmd_setting_key} if $self->{subcmd_setting_key};
179 0         0 my @prefix = @{$self->{prefix}}; shift @prefix;
  0         0  
  0         0  
180 0         0 $self->{subcmd_setting_key} = join('', @prefix);
181             }
182              
183             # Return 'on' for true and 'off' for false, and ?? for anything else.
184             sub show_onoff($$)
185             {
186 0     0   0 my ($self, $bool) = @_;
187 0 0       0 if (!defined($bool)) {
    0          
188 0         0 return 'unset';
189             } elsif ($bool) {
190 0         0 return 'on';
191             } else {
192 0         0 return 'off'
193             }
194             }
195              
196             sub set_name_prefix($$)
197             {
198 464     464   1531 my ($self, $class) = @_;
199 464         3372 my @prefix = split(/::/, $class);
200 464         1567 splice(@prefix, 0, 4); # Remove Devel::Trepan::CmdProcessor::Command
201 464         1411 @prefix = map {lc $_} @prefix;
  928         3349  
202 464         3307 $self->{prefix} = \@prefix;
203 464         2266 $self->{cmd_str} = join(' ', @prefix);
204             }
205              
206             sub string_in_show($)
207             {
208 0     0   0 my ($self, $bool) = @_;
209 0         0 my $skip_len = length('Show ');
210 0         0 ucfirst substr($self->{short_help}, $skip_len);
211             }
212              
213             sub summary_help($$)
214             {
215 0     0   0 my ($self, $subcmd_name) = @_;
216 0         0 my $msg = sprintf("%-12s: %s", $subcmd_name, $self->{short_help});
217 0         0 $self->msg_nocr($msg);
218             }
219              
220              
221             package Devel::Trepan::CmdProcessor::Command::SetBoolSubcmd;
222 12     12   98 use vars qw(@ISA);
  12         30  
  12         2931  
223             @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subcmd);
224              
225             sub complete($$)
226             {
227 4     4   10 my ($self, $prefix) = @_;
228 4         14 return Devel::Trepan::Complete::complete_token(['on', 'off'], $prefix);
229             }
230              
231              
232             sub new($$$) {
233 70     70   285 my ($class, $cmd, $name) = @_;
234 70         312 my $self = Devel::Trepan::CmdProcessor::Command::Subcmd::new($class, $cmd, $name);
235 70         217 $self->{max_args} = 1;
236 70         217 bless $self, $class;
237 70         2015 $self;
238             }
239              
240             sub run($$) {
241 0     0     my ($self, $args) = @_;
242 0           $self->run_set_bool($args);
243             }
244              
245             sub save_command($) {
246 0     0     my ($self) = @_;
247 0           my %settings = $self->{settings};
248 0 0         my $val = $settings{$self->subcmd_setting_key()} ? 'on' : 'off';
249 0           [$self->subcmd_prefix_string . " ${val}"];
250             }
251              
252             package Devel::Trepan::CmdProcessor::Command::ShowBoolSubcmd;
253 12     12   92 use vars qw(@ISA);
  12         41  
  12         1067  
254             @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subcmd);
255             sub run($)
256             {
257 0     0     my ($self, $args) = @_;
258 0           $self->run_show_bool($self->string_in_show());
259             }
260              
261             package Devel::Trepan::CmdProcessor::Command::ShowIntSubcmd;
262 12     12   72 use vars qw(@ISA);
  12         71  
  12         2834  
263             @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subcmd);
264              
265             sub run($) {
266 0     0     my ($self, $args) = @_;
267 0           my $doc;
268 0 0         if ($self->{short_help}) {
269 0           $doc = $self->{short_help};
270             } else {
271 0           my $len = length($self->{help}) - 6;
272 0           $doc = ucfirst substr($self->{help}, 5, $len);
273             }
274 0           $self->run_show_int($doc);
275             }
276              
277             unless (caller) {
278             # Demo it.
279             require Devel::Trepan::CmdProcessor::Mock;
280             my $proc = Devel::Trepan::CmdProcessor::Mock::setup();
281             my %cmds = %{$proc->{commands}};
282             print join(', ', keys %cmds), "\n";
283             my $subcmd =
284             Devel::Trepan::CmdProcessor::Command::Subcmd->new($cmds{'quit'});
285             print join(', ', keys %{$subcmd->{settings}}), "\n";
286             print $subcmd->show_onoff($subcmd->{settings}{autoeval}), "\n";
287             $subcmd->run_set_int($proc, 'Just a test');
288             }
289              
290             1;