OpenASIP  2.0
CmdLineOptions.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2011 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 CmdLineOptions.cc
26  *
27  * Definition of CmdLineOptions class.
28  *
29  * @author Jussi Nykänen 2003 (nykanen-no.spam-cs.tut.fi)
30  * @author Jari Mäntyneva 2006 (jari.mantyneva-no.spam-tut.fi)
31  * @author Esa Määttä 2007 (esa.maatta-no.spam-tut.fi)
32  * @author Pekka Jääskeläinen 2011
33  * @note reviewed 3 December 2003 by jn, kl, ao
34  */
35 
36 
37 #include <cstdlib>
38 #include <vector>
39 #include <map>
40 #include <string>
41 #include <iostream>
42 #include <iomanip>
43 #include <sstream>
44 
45 #include "CmdLineOptions.hh"
46 #include "Exception.hh"
47 
48 #include "CmdLineOptionParser.hh"
49 #include "tce_version_string.h"
50 
51 // text column lengths for the flags in --help printout
52 const int CmdLineOptions::SHORT_FLAG = 2;
53 const int CmdLineOptions::LONG_FLAG = 22;
54 
55 const std::string CmdLineOptions::VERBOSE_SWITCH = "verbose";
56 const std::string CmdLineOptions::VERBOSE_SPAM_SWITCH = "verbose_spam";
57 
58 using std::vector;
59 using std::map;
60 using std::string;
61 using std::cout;
62 using std::cerr;
63 using std::endl;
64 using std::setw;
65 using std::left;
66 
67 /**
68  * Constructor.
69  *
70  * @param description Brief description of the program and how to use it.
71  * Only prefix is currently "no-".
72  */
74  std::string description,
75  std::string version) :
76  CmdLineParser(description),
77  progName_(""),
78  description_(description),
79  version_(version) {
80 
81  BoolCmdLineOptionParser* verboseSwitch =
83  VERBOSE_SWITCH, "The verbose switch", "v");
84  addOption(verboseSwitch);
85 
86  BoolCmdLineOptionParser* verboseSpamSwitch =
88  VERBOSE_SPAM_SWITCH, "The verbose spam - switch", "V");
89  addOption(verboseSpamSwitch);
90 }
91 
92 /**
93  * Destructor.
94  */
96 }
97 
98 /**
99  * Loads all command line arguments and parses them.
100  *
101  * @param options Command line options pre-parsed in char* array.
102  * @param argc The number of command line options.
103  * @exception IllegalCommandLine If parsing is not succesfull.
104  * @exception ParserStopRequest If help or version option is found.
105  */
106 void
107 CmdLineOptions::parse(char* argv[], int argc) {
108  // command line is emptied
109  commandLine_.clear();
110  progName_ = string(argv[0]);
111 
112  for (int i = 1; i < argc; i++) {
113  commandLine_.push_back(string(argv[i]));
114  }
115 
116  parseAll();
117 }
118 
119 /**
120  * Loads all command line arguments and parses them.
121  *
122  * @param options Command line options pre-parsed in string array.
123  * @param argc The number of command line options.
124  * @exception IllegalCommandLine If parsing is not succesfull.
125  * @exception ParserStopRequest If help or version option is found.
126  */
127 void
128 CmdLineOptions::parse(string argv[], int argc) {
129  // command line is emptied
130  commandLine_.clear();
131  progName_ = argv[0];
132 
133  for (int i = 1; i < argc; i++) {
134  commandLine_.push_back(argv[i]);
135  }
136 
137  parseAll();
138 }
139 /**
140  * Loads all command line arguments and parses them.
141  *
142  * @param options Command line options pre-parsed in string vector.
143  * @exception IllegalCommandLine If parsing is not succesfull.
144  * @exception ParserStopRequest If help or version option is found.
145  */
146 void
147 CmdLineOptions::parse(std::vector<string> argv) {
148  // command line is emptied
149  commandLine_.clear();
150  progName_ = argv.at(0);
151 
152  for (unsigned int i = 1; i < argv.size(); i++) {
153  commandLine_.push_back(argv[i]);
154  }
155 
156  parseAll();
157 }
158 /**
159  * Parses all command line options.
160  *
161  * @exception IllegalCommandLine If parsing fails.
162  * @exception ParserStopRequest The client should not proceed.
163  */
164 void
166  // finished is set to true when options are parsed and the rest are
167  // command line arguments
168  bool finished = false;
169 
170  // checkArguments is set to false when command line arguments can start
171  // with "-" or "--"
172  bool checkArguments = true;
173  unsigned int i = 0;
174 
175  while (i < commandLine_.size()) {
176  string optString = commandLine_[i];
177 
178  if (!finished) {
179  string prefix = "";
180  string name = "";
181  string arguments = "";
182 
183  // hasArgument is true when option has argument
184  bool hasArgument = true;
185 
186  if (!parseOption(
187  optString, name, arguments, prefix, hasArgument)) {
188  finished = true;
189  if (optString == "--") {
190  checkArguments = false;
191  i++;
192  continue;
193  } else {
194  arguments_.push_back(optString);
195  i++;
196  continue;
197  }
198  }
199 
200  //
201  if (name == "help" || name == "h") {
202  printHelp();
203  string msg = "The client should not proceed";
204  string method = "CmdLineParser::parseAll()";
205  throw ParserStopRequest(__FILE__, __LINE__, method, msg);
206  } else if (name == "version") {
207  printVersion();
208  string msg = "The client should not proceed";
209  string method = "CmdLineParser::parseAll()";
210  throw ParserStopRequest(__FILE__, __LINE__, method, msg);
211  }
212 
213  CmdLineOptionParser* opt = findOption(name);
214 
215  if (arguments == "" &&
216  dynamic_cast<BoolCmdLineOptionParser*>(opt) == NULL) {
217 
218  // argument for an option may be separated with space
219  if (i < commandLine_.size() - 1 &&
220  commandLine_[i+1].substr(0, 1) != "-") {
221  hasArgument = true;
222  arguments = commandLine_[i+1];
223  i++;
224  }
225  }
226 
227  bool doneWithParsing = opt->parseValue(arguments, prefix);
228 
229  if (!doneWithParsing) {
230  if (hasArgument) {
231  // all the rest in command line are "extra" strings
232  arguments_.push_back(arguments);
233  finished = true;
234  i++;
235  } else {
236  // this is the situation when we have something like
237  // -abcd (multible flags put together)
238  for (unsigned int i = 0; i < arguments.length(); i++) {
239  opt = findOption(arguments.substr(i, 1));
240  opt->parseValue("", prefix);
241  }
242  }
243  }
244  } else {
245 
246  // finished reading options, all rest are command line arguments
247  if (checkArguments && optString[0] == '-') {
248  string msg = "Illegal command line argument: " + optString;
249  string method = "CmdLineOptions::parse()";
250  throw IllegalCommandLine(__FILE__, __LINE__, method, msg);
251  }
252  arguments_.push_back(optString);
253  }
254  i++;
255  }
256 }
257 
258 /**
259  * Prints the help menu of the program.
260  */
261 void
263  cout << description_ << endl;
264  cout << "Options:" << endl;
265  constMapIter i;
266  for (i = optionLongNames_.begin(); i != optionLongNames_.end(); i++) {
267  CmdLineOptionParser* opt = findOption((*i).first);
268  if (opt->isHidden()) continue;
269  if (opt->shortName() != "") {
270  cout << left << setw(SHORT_FLAG) << "-" + opt->shortName() + ", "
271  << left << setw(LONG_FLAG) << "--" + opt->longName();
272  } else {
273  cout << " "
274  << left << setw(LONG_FLAG) << "--" + opt->longName();
275  }
276 
277  std::stringstream str(opt->description());
278  int charsPrintedInRow = 0;
279  while (!str.eof()) {
280  std::string nextToken = "";
281  str >> nextToken;
282  if (nextToken.size() + charsPrintedInRow > 60) {
283  cout << std::endl;
284  cout << std::left << setw(SHORT_FLAG + LONG_FLAG + 2) << " ";
285  charsPrintedInRow = 0;
286  }
287  cout << nextToken << " ";
288  charsPrintedInRow += nextToken.size();
289  }
290  cout << std::endl;
291  }
292 }
293 
294 /**
295  * Return true if the verbose switch was defined in the command line.
296  *
297  * @return True if the verbose switch was defined in the command line.
298  */
299 bool
302 }
303 
304 /**
305  * Return true if the verbose spam switch was defined in the command line.
306  *
307  * @return True if the verbose spam switch was defined in the command line.
308  */
309 bool
312 }
313 
314 /**
315  * Returns true if there is a value available for given option.
316  *
317  * @return True if the option is defined.
318  */
319 bool
320 CmdLineOptions::optionGiven(std::string key) const {
321  try {
322  /// @todo: This returns always true if trying to find added
323  /// CmdLineOption...
324  return findOption(key)->isDefined();
325  } catch (const IllegalCommandLine&) {
326  return false;
327  }
328  return true;
329 }
CmdLineOptions::description_
std::string description_
The description of usage of program.
Definition: CmdLineOptions.hh:89
ParserStopRequest
Definition: Exception.hh:491
Exception.hh
CmdLineOptionParser::isDefined
bool isDefined()
Definition: CmdLineOptionParser.cc:169
CmdLineOptions::VERBOSE_SPAM_SWITCH
static const std::string VERBOSE_SPAM_SWITCH
Switch for verbose output listing spam from scheduler internals.
Definition: CmdLineOptions.hh:105
CmdLineOptions::LONG_FLAG
static const int LONG_FLAG
Number of characters reserved for printing long version of commandline flag.
Definition: CmdLineOptions.hh:99
CmdLineOptionParser.hh
CmdLineOptions.hh
CmdLineOptions::parseAll
void parseAll()
Definition: CmdLineOptions.cc:165
IllegalCommandLine
Definition: Exception.hh:438
CmdLineParser::commandLine_
std::vector< std::string > commandLine_
Command line is stored here.
Definition: CmdLineParser.hh:84
CmdLineParser::optionLongNames_
std::map< std::string, CmdLineOptionParser * > optionLongNames_
Database for holding options with their long names as a key.
Definition: CmdLineParser.hh:79
CmdLineOptionParser::isHidden
bool isHidden()
Definition: CmdLineOptionParser.hh:75
CmdLineOptions::optionGiven
bool optionGiven(std::string key) const
Definition: CmdLineOptions.cc:320
CmdLineOptions::progName_
std::string progName_
The name of the program.
Definition: CmdLineOptions.hh:87
CmdLineOptionParser::shortName
std::string shortName() const
CmdLineOptionParser::parseValue
virtual bool parseValue(std::string arguments, std::string prefix)=0
Pure virtual function that parses the value of option.
CmdLineParser::parseOption
bool parseOption(std::string option, std::string &name, std::string &arguments, std::string &prefix, bool &hasArgument) const
Definition: CmdLineParser.cc:277
CmdLineParser::addOption
void addOption(CmdLineOptionParser *opt)
CmdLineOptionParser
Definition: CmdLineOptionParser.hh:56
CmdLineOptions::~CmdLineOptions
virtual ~CmdLineOptions()
Definition: CmdLineOptions.cc:95
CmdLineOptions::CmdLineOptions
CmdLineOptions(std::string description, std::string version="")
Definition: CmdLineOptions.cc:73
CmdLineParser::arguments_
std::vector< std::string > arguments_
Command line arguments are stored here.
Definition: CmdLineParser.hh:86
BoolCmdLineOptionParser
Definition: CmdLineOptionParser.hh:278
CmdLineOptions::isVerboseSwitchDefined
virtual bool isVerboseSwitchDefined() const
Definition: CmdLineOptions.cc:300
CmdLineOptionParser::longName
std::string longName() const
CmdLineOptions::printHelp
virtual void printHelp() const
Definition: CmdLineOptions.cc:262
CmdLineOptions::parse
void parse(char *argv[], int argc)
Definition: CmdLineOptions.cc:107
CmdLineOptions::SHORT_FLAG
static const int SHORT_FLAG
Number of characters reserved for printing short version of commandline flag.
Definition: CmdLineOptions.hh:95
CmdLineOptionParser::description
std::string description() const
CmdLineOptions::printVersion
virtual void printVersion() const =0
CmdLineOptions::constMapIter
std::map< std::string, CmdLineOptionParser * >::const_iterator constMapIter
For traversing const maps.
Definition: CmdLineOptions.hh:77
CmdLineOptions::VERBOSE_SWITCH
static const std::string VERBOSE_SWITCH
Switch for verbose output listing scheduler modules.
Definition: CmdLineOptions.hh:102
CmdLineParser::findOption
CmdLineOptionParser * findOption(std::string name) const
Definition: CmdLineParser.cc:160
CmdLineParser
Definition: CmdLineParser.hh:53
CmdLineOptions::isVerboseSpamSwitchDefined
virtual bool isVerboseSpamSwitchDefined() const
Definition: CmdLineOptions.cc:310