File Coverage

lib/Term/RouterCLI/Help.pm
Criterion Covered Total %
statement 18 101 17.8
branch 0 32 0.0
condition 0 20 0.0
subroutine 6 10 60.0
pod 0 1 0.0
total 24 164 14.6


line stmt bran cond sub pod time code
1             #####################################################################
2             # This program is not guaranteed to work at all, and by using this #
3             # program you release the author of any and all liability. #
4             # #
5             # You may use this code as long as you are in compliance with the #
6             # license (see the LICENSE file) and this notice, disclaimer and #
7             # comment box remain intact and unchanged. #
8             # #
9             # Package: Term::RouterCLI #
10             # Class: Help #
11             # Description: Methods for building a Router (Stanford) style CLI #
12             # #
13             # Written by: Bret Jordan (jordan at open1x littledot org) #
14             # Created: 2011-02-21 #
15             #####################################################################
16             #
17             #
18             #
19             #
20             package Term::RouterCLI::Help;
21              
22 4     4   44 use 5.8.8;
  4         11  
  4         166  
23 4     4   21 use strict;
  4         9  
  4         108  
24 4     4   19 use warnings;
  4         7  
  4         170  
25 4     4   20 use Term::RouterCLI::Debugger;
  4         7  
  4         121  
26 4     4   22 use Log::Log4perl;
  4         7  
  4         47  
27              
28 4     4   248 use parent qw(Exporter);
  4         5  
  4         37  
29             our @EXPORT = qw();
30             our @EXPORT_OK = qw( PrintHelp _GetCommandHelp _GetCommandSummaries _GetCommandSummary);
31             our %EXPORT_TAGS = ( 'all' => [ @EXPORT_OK ] );
32             our $VERSION = '1.00';
33             $VERSION = eval $VERSION;
34              
35              
36             my $oDebugger = new Term::RouterCLI::Debugger();
37              
38              
39             # ----------------------------------------
40             # Public Methods
41             # ----------------------------------------
42              
43             sub PrintHelp
44             {
45             # This method will print out a short description or long description depending on whether
46             # or not an argument "topic" is passed in. If there is a command argument, then we will
47             # print the detailed help topics.
48             # Required:
49             # array_ref ($self->{'_aCommandArguments'})
50 0     0 0   my $self = shift;
51 0           my $logger = $oDebugger->GetLogger($self);
52 0           my $OUT = $self->{OUT};
53              
54 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
55              
56             # If there is an argument passed to the help function, then lets process those arguments
57             # finding the corresponding command and its help directives. If there is no argument
58             # passed in, then we will just print out all of the help summaries
59 0           my $iNumberOfArguments = @{$self->{'_aCommandArguments'}};
  0            
60 0           $logger->debug("$self->{'_sName'} - ", "iNumberOfArguments: $iNumberOfArguments");
61 0 0         if ($iNumberOfArguments > 0)
62             {
63 0           $logger->debug("$self->{'_sName'} - ", "Step 2");
64 0           my $sHelpAboutACommand = $self->_GetCommandHelp();
65 0           $logger->debug("$self->{'_sName'} - ", "sHelpAboutACommand: $$sHelpAboutACommand");
66              
67 0           print $OUT $$sHelpAboutACommand;
68 0           print $OUT "\n";
69             }
70             else
71             {
72 0           $logger->debug("$self->{'_sName'} - ", "Step 3");
73 0 0         unless (exists($self->{'_hCommandDirectives'}->{cmds})) {$self->{'_hCommandTreeAtLevel'} = $self->GetFullCommandTree();}
  0            
74 0           print $OUT $self->_GetCommandSummaries();
75             }
76 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
77             }
78              
79              
80             # ----------------------------------------
81             # Private Methods
82             # ----------------------------------------
83              
84             sub _GetCommandHelp
85             {
86             # This method will get the command details from the help directive
87             # Required:
88             # hash_ref ($self->{'_hCommandTreeAtLevel'})
89             # hash_ref ($self->{'_hCommandDirectives'})
90             # Return:
91             # string_ref(help details for the command in question)
92 0     0     my $self = shift;
93 0           my $logger = $oDebugger->GetLogger($self);
94 0           my $sHelpDetails = "";
95              
96 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
97             # Lets get the current data for the tree location that we are now at
98 0           $self->_FindCommandInCommandTree();
99              
100             # If their are no command directives then lets look for a default command
101 0 0         if (!$self->{'_hCommandDirectives'})
102             {
103 0           $logger->debug("$self->{'_sName'} - ", "Step 1");
104 0 0         if (exists $self->{'_hCommandTreeAtLevel'}->{''}) { $self->{'_hCommandDirectives'} = $self->{'_hCommandTreeAtLevel'}->{''}; }
  0            
105             else
106             {
107 0           my ($sCommandName) = $self->_GetFullCommandName();
108 0           $sHelpDetails = "$sCommandName doesn't exist.\n";
109             }
110             }
111             else
112             {
113 0           $logger->debug("$self->{'_sName'} - ", "Step 2");
114 0 0         if ($self->{display_summary_in_help})
115             {
116 0           my ($sCommand) = $self->_GetFullCommandName();
117 0           $logger->debug("$self->{'_sName'} - ", "Step 2.1");
118 0           $logger->debug("$self->{'_sName'} - ", "sCommand: $sCommand");
119              
120             # We need to take in to account if the desc or help is not in the translated lanugage pack
121 0 0 0       if (exists($self->{'_hCommandDirectives'}->{'desc'}) && (defined $self->{'_hCommandDirectives'}->{'desc'}))
122             {
123 0           $sHelpDetails = "$sCommand: " . $self->{'_hCommandDirectives'}->{'desc'} . "\n";
124             }
125 0           else { $sHelpDetails = "$sCommand: Command description not found\n"; }
126             }
127            
128 0 0 0       if (exists($self->{'_hCommandDirectives'}->{'help'}) && (defined $self->{'_hCommandDirectives'}->{'help'}))
129             {
130 0           $sHelpDetails .= $self->{'_hCommandDirectives'}->{'help'};
131 0           $sHelpDetails .= "\n";
132             }
133 0           else { $sHelpDetails = "No additional help found\n"; }
134              
135 0 0 0       if ($self->{'display_subcommands_in_help'} && exists($self->{'_hCommandDirectives'}->{'cmds'}))
136             {
137 0           $sHelpDetails .= "\nSubcommands available:\n";
138 0           $sHelpDetails .= $self->_GetCommandSummaries();
139             }
140             }
141 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
142 0           return \$sHelpDetails;
143             }
144              
145             sub _GetCommandSummaries
146             {
147             # This method will return the command summaries for all commands at the current level of a command tree
148             # Required:
149             # hash_ref ($self->{'_hCommandTreeAtLevel'})
150             # Optional:
151             # array_ref (commands)
152 0     0     my $self = shift;
153 0           my $aCommands = shift;
154 0           my $logger = $oDebugger->GetLogger($self);
155 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
156            
157             # This was added to support "?" mark tab completion when there is nothing yet entered on the command prompt
158 0 0         unless (exists $aCommands->[0])
159             {
160 0 0         unless ( defined $self->{'_hCommandTreeAtLevel'} ) { $self->{'_hCommandTreeAtLevel'} = $self->GetCurrentCommandTree(); }
  0            
161 0           foreach (sort(keys(%{$self->{'_hCommandTreeAtLevel'}})))
  0            
162             {
163 0           push @$aCommands, $_;
164             }
165             }
166              
167 0           $logger->debug("$self->{'_sName'} - ", "_hCommandTreeAtLevel: ", ${$oDebugger->DumpHashKeys($self->{'_hCommandTreeAtLevel'})});
  0            
168 0           $logger->debug("$self->{'_sName'} - ", "aCommands: ", ${$oDebugger->DumpArray($aCommands)});
  0            
169              
170 0           my $sAllCommandSummaries = "";
171              
172             # We need to push values in to this string for the following use cases:
173             # 1) There is a code directive found on the command, meaning it can be ran by itself
174             # ()example "show interface" and "show interface brief")
175             # 2) An actual argument is possible, we should print out some helper text so that the user will know what
176             # they should be entering
177            
178 0 0 0       if ( exists $self->{'_hCommandDirectives'}->{'maxargs'} && $self->{'_hCommandDirectives'}->{'maxargs'} >= 1 )
179             {
180 0           my $sArgDescription = "unknown";
181 0 0 0       if (exists $self->{'_hCommandDirectives'}->{'argdesc'} && defined $self->{'_hCommandDirectives'}->{'argdesc'}) { $sArgDescription = $self->{'_hCommandDirectives'}->{'argdesc'}; }
  0            
182 0           $sAllCommandSummaries .= sprintf(" %-20s $sArgDescription\n", "WORD");
183             }
184            
185 0           foreach (sort(@$aCommands))
186             {
187             # We now exclude synonyms from the command summaries.
188 0 0 0       next if exists $self->{'_hCommandTreeAtLevel'}->{$_}->{'alias'} || exists $self->{'_hCommandTreeAtLevel'}->{$_}->{'syn'};
189             # Lets not show the default command in any summaries
190 0 0         next if $_ eq '';
191             # Lets not show "hidden" options in any summaries
192 0 0         next if exists $self->{'_hCommandTreeAtLevel'}->{$_}->{'hidden'};
193              
194 0           $sAllCommandSummaries .= $self->_GetCommandSummary("$_");
195             }
196 0 0         if ( exists $self->{'_hCommandDirectives'}->{'code'} ) { $sAllCommandSummaries .= sprintf(" %-20s\n", ""); }
  0            
197              
198 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
199 0           return $sAllCommandSummaries;
200             }
201              
202             sub _GetCommandSummary
203             {
204             # This method returns the command summary for a specific command at a certain command tree level
205             # Required:
206             # hash_ref ($self->{'_hCommandTreeAtLevel'})
207             # string (command name)
208             # Return:
209             # string (command summary line)
210 0     0     my $self = shift;
211 0           my $sCommandName = shift;
212 0           my $logger = $oDebugger->GetLogger($self);
213 0           my $sCommandSummary;
214              
215 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
216 0   0       $sCommandSummary = $self->{'_hCommandTreeAtLevel'}->{$sCommandName}->{'desc'} || "(no description)";
217            
218 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
219 0           return sprintf(" %-20s $sCommandSummary\n", $sCommandName);
220             }
221              
222             return 1;