OpenASIP  2.0
CmdHelp.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file CmdHelp.cc
26  *
27  * Declaration of CmdHelp class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @note reviewed 2 June 2004 by jm, pj, tr, jn
31  * @note rating: green
32  */
33 
34 #include <vector>
35 #include <string>
36 
37 #include "CmdHelp.hh"
38 #include "Application.hh"
39 
40 using std::vector;
41 using std::string;
42 
43 
44 /**
45  * Constructor.
46  */
48 }
49 
50 /**
51  * Destructor.
52  */
54 }
55 
56 /**
57  * Executes the help command.
58  *
59  * @param arguments Arguments for the command.
60  * @return True if execution is succesfull, false otherwise
61  * @exception NumberFormatException If conversion of DataObject fails.
62  */
63 bool
64 CmdHelp::execute(const std::vector<DataObject>& arguments) {
65  ScriptInterpreter* interp = interpreter();
66  assert(interp != NULL);
67  DataObject* obj = new DataObject();
68 
69  if (arguments.size() == 1) {
70  string result = "Commands available:\n";
71  vector<string> names = interp->customCommandsSortedByName();
72  for (size_t i = 0; i < names.size(); i++) {
73  result += names[i] + " ";
74  }
75  obj->setString(result);
76  interp->setResult(obj);
77  return true;
78  }
79 
80  if (arguments.size() != 2) {
81  obj->setString("Wrong number of arguments");
82  interp->setResult(obj);
83  return false;
84  }
85 
86  string cmdName = arguments[1].stringValue();
87  CustomCommand* command = interp->customCommand(cmdName);
88  if (command == NULL) {
89  obj->setString("Unknown command: " + cmdName);
90  interp->setResult(obj);
91  return false;
92  } else {
93  obj->setString(command->helpText());
94  interp->setResult(obj);
95  return true;
96  }
97 }
98 
99 /**
100  * Returns help text.
101  *
102  * @return Help text.
103  */
104 string
106  return "Use help command to get help";
107 }
CmdHelp::~CmdHelp
virtual ~CmdHelp()
Definition: CmdHelp.cc:53
DataObject
Definition: DataObject.hh:50
CmdHelp::CmdHelp
CmdHelp()
Definition: CmdHelp.cc:47
CmdHelp::helpText
virtual std::string helpText() const
Definition: CmdHelp.cc:105
assert
#define assert(condition)
Definition: Application.hh:86
ScriptInterpreter::customCommandsSortedByName
std::vector< std::string > customCommandsSortedByName()
Definition: ScriptInterpreter.cc:342
Application.hh
CustomCommand
Definition: CustomCommand.hh:54
ScriptInterpreter::customCommand
virtual CustomCommand * customCommand(const std::string &commandName)
Definition: ScriptInterpreter.cc:110
CustomCommand::interpreter
ScriptInterpreter * interpreter() const
ScriptInterpreter::setResult
virtual void setResult(DataObject *result)
Definition: ScriptInterpreter.cc:128
CmdHelp.hh
CmdHelp::execute
virtual bool execute(const std::vector< DataObject > &arguments)
Definition: CmdHelp.cc:64
DataObject::setString
virtual void setString(std::string value)
Definition: DataObject.cc:130
ScriptInterpreter
Definition: ScriptInterpreter.hh:55
CustomCommand::helpText
virtual std::string helpText() const =0