HelpCommand.cc Source File

Back to the index.

HelpCommand.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2010 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "commands/HelpCommand.h"
29 #include "GXemul.h"
30 
31 
33  : Command("help", "[command-name]")
34 {
35 }
36 
37 
39 {
40 }
41 
42 
43 bool HelpCommand::Execute(GXemul& gxemul, const vector<string>& arguments)
44 {
45  const Commands& commands = gxemul.GetCommandInterpreter().GetCommands();
46  Commands::const_iterator it;
47 
48  if (arguments.size() > 1) {
49  gxemul.GetUI()->ShowDebugMessage("usage: help [cmd]\n");
50  return false;
51  }
52 
53  if (arguments.size() == 1) {
54  // Special case: root isn't really a command
55  if (arguments[0] == "root") {
56  gxemul.GetUI()->ShowDebugMessage(
57  "\nroot\n----\n\n"
58  "Shows the component tree which makes up the current emulation configuration.\n"
59  "Technically, 'root' is not a command, but the name of the root node. Typing\n"
60  "the name of any node in the configuration tree will dump the sub-tree starting\n"
61  "at that node, followed by that node's own variables.\n\n");
62  return true;
63  }
64 
65  it = commands.find(arguments[0]);
66  if (it == commands.end()) {
67  gxemul.GetUI()->ShowDebugMessage(
68  "Command \"" + arguments[0] + "\" unknown.\n");
69  return false;
70  }
71 
72  // command name + " " + argument format
73  // ------------------------------------
74  //
75  // Long description.
76 
77  string longhelp = arguments[0];
78  const string& argumentFormat = it->second->GetArgumentFormat();
79  if (argumentFormat != "")
80  longhelp += " " + argumentFormat;
81  int len = longhelp.length();
82 
83  longhelp += "\n";
84  for (int i=0; i<len; i++)
85  longhelp += "-";
86 
87  longhelp += "\n\n" + it->second->GetLongDescription();
88  if (longhelp[longhelp.length() - 1] != '\n')
89  longhelp += '\n';
90 
91  gxemul.GetUI()->ShowDebugMessage("\n" + longhelp + "\n");
92 
93  if (it->second->MayBeReexecutedWithoutArgs())
94  gxemul.GetUI()->ShowDebugMessage(
95  "This command will be re-executed (without "
96  "arguments), if an empty command\n"
97  "line is issued.\n\n");
98 
99  return true;
100  }
101 
102  // Find the longest name + argument:
103  size_t longest = 0;
104  for (it = commands.begin(); it != commands.end(); ++ it) {
105  const Command& command = *it->second;
106  size_t len = command.GetCommandName().length();
107 
108  if (command.GetArgumentFormat() != "")
109  len += 1 + command.GetArgumentFormat().length();
110 
111  if (len > longest)
112  longest = len;
113  }
114 
115  // Show all commands followed by their short descriptions:
116  for (it = commands.begin(); it != commands.end(); ++ it) {
117  const Command& command = *it->second;
118 
119  const string leadingSpaces = " ";
120  const int nLeadingSpaces = 2;
121 
122  string str = leadingSpaces + command.GetCommandName();
123  if (command.GetArgumentFormat() != "")
124  str += " " + command.GetArgumentFormat();
125 
126  while (str.length() < longest + nLeadingSpaces)
127  str += " ";
128 
129  str += " " + command.GetShortDescription();
130 
131  gxemul.GetUI()->ShowDebugMessage(str + "\n");
132  }
133 
134  // ... followed by a short description of other possible commands:
135  gxemul.GetUI()->ShowDebugMessage(" ------------------------------"
136  "----------------------------------------------\n");
137  gxemul.GetUI()->ShowDebugMessage(" root "
138  "Shows the entire component tree.\n");
139  gxemul.GetUI()->ShowDebugMessage(" <name> "
140  "Shows the component tree, from the named component.\n");
141  gxemul.GetUI()->ShowDebugMessage(" <name>.<method> "
142  "Executes a method of a named component.\n");
143  gxemul.GetUI()->ShowDebugMessage(" <name>.<var> "
144  "Prints the value of a variable of a named component.\n");
145  gxemul.GetUI()->ShowDebugMessage(" <name>.<var> = <value> "
146  "Assigns a value to a variable of a named component.\n");
147 
148  return true;
149 }
150 
151 
153 {
154  return "Shows a help message.";
155 }
156 
157 
159 {
160  return "Shows a summary of all available commands, or, "
161  "when given a specific command\n"
162  "name as an argument, shows a detailed description about "
163  "that command.\n";
164 }
165 
166 
Commands
map< string, refcount_ptr< Command > > Commands
Definition: Command.h:135
HelpCommand::Execute
virtual bool Execute(GXemul &gxemul, const vector< string > &arguments)
Executes the command on a given GXemul instance.
Definition: HelpCommand.cc:43
Command::GetArgumentFormat
const string & GetArgumentFormat() const
Gets the argument format for the command.
Definition: Command.h:80
HelpCommand.h
HelpCommand::GetShortDescription
virtual string GetShortDescription() const
Returns a short (one-line) description of the command.
Definition: HelpCommand.cc:152
GXemul
The main emulator class.
Definition: GXemul.h:54
GXemul::GetCommandInterpreter
CommandInterpreter & GetCommandInterpreter()
Gets a reference to the CommandInterpreter.
Definition: GXemul.cc:631
Command
A Command is a named function, executed by the CommandInterpreter.
Definition: Command.h:48
UI::ShowDebugMessage
virtual void ShowDebugMessage(const string &msg)=0
Shows a debug message.
HelpCommand::HelpCommand
HelpCommand()
Constructs a HelpCommand.
Definition: HelpCommand.cc:32
Command::GetCommandName
const string & GetCommandName() const
Gets the name of the command.
Definition: Command.h:70
HelpCommand::~HelpCommand
virtual ~HelpCommand()
Definition: HelpCommand.cc:38
HelpCommand::GetLongDescription
virtual string GetLongDescription() const
Returns a long description/help message for the command.
Definition: HelpCommand.cc:158
CommandInterpreter::GetCommands
const Commands & GetCommands() const
Gets a collection of all commands.
Definition: CommandInterpreter.cc:63
GXemul::GetUI
UI * GetUI()
Gets a pointer to the GXemul instance' active UI.
Definition: GXemul.cc:661
Command::GetShortDescription
virtual string GetShortDescription() const =0
Returns a short (one-line) description of the command.
GXemul.h

Generated on Tue Mar 24 2020 14:04:48 for GXemul by doxygen 1.8.17