OpenASIP 2.2
Loading...
Searching...
No Matches
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
49#include "tce_version_string.h"
50
51// text column lengths for the flags in --help printout
52const int CmdLineOptions::SHORT_FLAG = 2;
53const int CmdLineOptions::LONG_FLAG = 22;
54
55const std::string CmdLineOptions::VERBOSE_SWITCH = "verbose";
56const std::string CmdLineOptions::VERBOSE_SPAM_SWITCH = "verbose_spam";
57
58using std::vector;
59using std::map;
60using std::string;
61using std::cout;
62using std::cerr;
63using std::endl;
64using std::setw;
65using 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 */
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 */
106void
107CmdLineOptions::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 */
127void
128CmdLineOptions::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 */
146void
147CmdLineOptions::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 */
164void
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 */
261void
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 */
299bool
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 */
309bool
313
314/**
315 * Returns true if there is a value available for given option.
316 *
317 * @return True if the option is defined.
318 */
319bool
320CmdLineOptions::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}
std::string longName() const
std::string shortName() const
std::string description() const
virtual bool parseValue(std::string arguments, std::string prefix)=0
Pure virtual function that parses the value of option.
static const int LONG_FLAG
Number of characters reserved for printing long version of commandline flag.
std::map< std::string, CmdLineOptionParser * >::const_iterator constMapIter
For traversing const maps.
void parse(char *argv[], int argc)
std::string progName_
The name of the program.
virtual ~CmdLineOptions()
static const std::string VERBOSE_SWITCH
Switch for verbose output listing scheduler modules.
static const int SHORT_FLAG
Number of characters reserved for printing short version of commandline flag.
bool optionGiven(std::string key) const
virtual bool isVerboseSwitchDefined() const
CmdLineOptions(std::string description, std::string version="")
static const std::string VERBOSE_SPAM_SWITCH
Switch for verbose output listing spam from scheduler internals.
virtual bool isVerboseSpamSwitchDefined() const
virtual void printHelp() const
virtual void printVersion() const =0
std::string description_
The description of usage of program.
std::map< std::string, CmdLineOptionParser * > optionLongNames_
Database for holding options with their long names as a key.
std::vector< std::string > commandLine_
Command line is stored here.
CmdLineOptionParser * findOption(std::string name) const
std::vector< std::string > arguments_
Command line arguments are stored here.
bool parseOption(std::string option, std::string &name, std::string &arguments, std::string &prefix, bool &hasArgument) const
void addOption(CmdLineOptionParser *opt)