blib/lib/Devel/Trepan/CmdProcessor/Command/Subcmd/Core.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 273 CC 18 SCAR 19.7
CC
18
Cov
0%
new
20.0
run_show_val
20.0
run_set_bool
12.0
CRAP 7.2
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2011-2015 Rocky Bernstein <rocky@cpan.org>
3 # A base class for debugger subcommands.
4 #
5 use Exporter;
6 use warnings;
7 no warnings 'redefine';
8  
9 use rlib '../../../../..';
10 use Devel::Trepan::CmdProcessor::Command;
11  
12 package Devel::Trepan::CmdProcessor::Command::Subcmd;
13 use Devel::Trepan::CmdProcessor::Validate;
14  
15 BEGIN {
16 @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 use strict;
21  
22 my $NotImplementedMessage =
23 "This method must be overridden in a subclass";
24  
25 use vars qw(@SUBCMD_VARS @EXPORT @ISA @SUBCMD_ISA);
26 use vars @SUBCMD_VARS;
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 use constant MIN_ARGS => 0;
36 use constant MAX_ARGS => 0;
37 use constant NEED_STACK => 0;
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 my ($class, $cmd, $name) = @_;
49 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 $self->{dbgr} = $cmd->{dbgr};
57 $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 my $base_prefix="Devel::Trepan::CmdProcessor::Command::Subcmd::";
62 for my $field (@SUBCMD_VARS) {
63 my $sigil = substr($field, 0, 1);
64 my $new_field = index('$@', $sigil) >= 0 ? substr($field, 1) : $field;
65 if ($sigil eq '$') {
66 $self->{lc $new_field} =
67 eval "\$${class}::${new_field} || \$${base_prefix}${new_field}";
68 } elsif ($sigil eq '@') {
69 $self->{lc $new_field} = eval "[\@${class}::${new_field}]";
70 } else {
71 die "Woah - bad sigil: $sigil";
72 }
73 }
74 # Done after above since $NAME is in @SUBCMD_VARS;
75 $self->{name} = $name;
76 $self->{short_help} ||= $self->{help};
77 bless $self, $class;
78 $self->set_name_prefix($class);
79 $self;
80 }
81  
82 # Convenience short-hand for $proc->confirm
83 sub confirm($$;$) {
84 my ($self, $msg, $default) = @_;
85 return($self->{proc}->confirm($msg, $default));
86 }
87  
88 # Set a Boolean-valued debugger setting.
89 sub run_set_bool($$;$)
90 {
91 my ($self, $args, $default) = @_;
92 $default = 1 if scalar @_ < 3;
93 my $onoff_arg = @$args < 3 ? 'on' : $args->[2];
94 my $key = $self->subcmd_setting_key();
95 $self->{proc}{settings}{$key} = $self->{proc}->get_onoff($onoff_arg);
96 $self->run_show_bool();
97 }
98  
99 # set an Integer-valued debugger setting.
100 sub run_set_int($$$;$$)
101 {
102 my ($self, $arg, $msg_on_error, $min_value, $max_value) = @_;
103 if ($arg =~/^\s*$/) {
104 $self->{proc}->errmsg('You need to supply a number.');
105 return undef;
106 }
107 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 if (defined ($val)) {
113 $self->{settings}{subcmd_setting_key} = $val;
114 $self->run_show_int();
115 }
116 }
117  
118 # Generic subcommand showing a boolean-valued debugger setting.
119 sub run_show_bool($;$)
120 {
121 my ($self, $what) = @_;
122 my $key = $self->subcmd_setting_key();
123 my $val = $self->show_onoff($self->{proc}{settings}{$key});
124 $what = $self->{name} unless $what;
125 $self->{proc}->msg(sprintf "%s is %s.", $what, $val);
126 }
127  
128 # Generic subcommand integer value display
129 sub run_show_int($;$)
130 {
131 my ($self, $what) = @_;
132 my $val = $self->{settings}{$self->subcmd_setting_key()};
133 unless ($what) {
134 my @what = @$self->{prefix};
135 pop @what;
136 $what = join(' ', @what);
137 }
138 $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 my ($self, $opts) = @_;
157 $opts ||= {};
158 my $what = exists $opts->{what} ? $opts->{what} : $self->{string_in_show};
159 my $name = exists $opts->{name} ? $opts->{name} : $self->{name};
160 my $val = exists $opts->{value} ? $opts->{value} : $self->{settings}{$name};
161 my $msg = sprintf("%s is %s.", $what, $val);
162 $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 my $self = shift;
172 join(' ', $self->{prefix});
173 }
174  
175 sub subcmd_setting_key($)
176 {
177 my $self = shift;
178 return $self->{subcmd_setting_key} if $self->{subcmd_setting_key};
179 my @prefix = @{$self->{prefix}}; shift @prefix;
180 $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 my ($self, $bool) = @_;
187 if (!defined($bool)) {
188 return 'unset';
189 } elsif ($bool) {
190 return 'on';
191 } else {
192 return 'off'
193 }
194 }
195  
196 sub set_name_prefix($$)
197 {
198 my ($self, $class) = @_;
199 my @prefix = split(/::/, $class);
200 splice(@prefix, 0, 4); # Remove Devel::Trepan::CmdProcessor::Command
201 @prefix = map {lc $_} @prefix;
202 $self->{prefix} = \@prefix;
203 $self->{cmd_str} = join(' ', @prefix);
204 }
205  
206 sub string_in_show($)
207 {
208 my ($self, $bool) = @_;
209 my $skip_len = length('Show ');
210 ucfirst substr($self->{short_help}, $skip_len);
211 }
212  
213 sub summary_help($$)
214 {
215 my ($self, $subcmd_name) = @_;
216 my $msg = sprintf("%-12s: %s", $subcmd_name, $self->{short_help});
217 $self->msg_nocr($msg);
218 }
219  
220  
221 package Devel::Trepan::CmdProcessor::Command::SetBoolSubcmd;
222 use vars qw(@ISA);
223 @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subcmd);
224  
225 sub complete($$)
226 {
227 my ($self, $prefix) = @_;
228 return Devel::Trepan::Complete::complete_token(['on', 'off'], $prefix);
229 }
230  
231  
232 sub new($$$) {
233 my ($class, $cmd, $name) = @_;
234 my $self = Devel::Trepan::CmdProcessor::Command::Subcmd::new($class, $cmd, $name);
235 $self->{max_args} = 1;
236 bless $self, $class;
237 $self;
238 }
239  
240 sub run($$) {
241 my ($self, $args) = @_;
242 $self->run_set_bool($args);
243 }
244  
245 sub save_command($) {
246 my ($self) = @_;
247 my %settings = $self->{settings};
248 my $val = $settings{$self->subcmd_setting_key()} ? 'on' : 'off';
249 [$self->subcmd_prefix_string . " ${val}"];
250 }
251  
252 package Devel::Trepan::CmdProcessor::Command::ShowBoolSubcmd;
253 use vars qw(@ISA);
254 @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subcmd);
255 sub run($)
256 {
257 my ($self, $args) = @_;
258 $self->run_show_bool($self->string_in_show());
259 }
260  
261 package Devel::Trepan::CmdProcessor::Command::ShowIntSubcmd;
262 use vars qw(@ISA);
263 @ISA = qw(Exporter Devel::Trepan::CmdProcessor::Command::Subcmd);
264  
265 sub run($) {
266 my ($self, $args) = @_;
267 my $doc;
268 if ($self->{short_help}) {
269 $doc = $self->{short_help};
270 } else {
271 my $len = length($self->{help}) - 6;
272 $doc = ucfirst substr($self->{help}, 5, $len);
273 }
274 $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;