OpenASIP  2.0
CmdLineParser.hh
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 CmdLineParser.hh
26  *
27  * Declaration of class CmdLineParser.
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  * @note reviewed 3 December 2003 by jn, kl, ao
32  * @note rating: red
33  */
34 
35 #ifndef TTA_CMD_LINE_PARSER_HH
36 #define TTA_CMD_LINE_PARSER_HH
37 
38 #include <map>
39 #include <vector>
40 #include <string>
41 
42 #include "CmdLineOptionParser.hh"
43 #include "Exception.hh"
44 #include "Application.hh"
45 
46 class Options;
47 
48 /**
49  * Abstract base class for command line parsers.
50  *
51  * Is capable of storing and parsing commmand line options.
52  */
54 public:
55  CmdLineParser(std::string description);
56  virtual ~CmdLineParser();
57 
58  virtual void parse(char* argv[], int argc);
59  virtual void parse(std::vector<std::string> options);
60  virtual void storeOptions(Options& options);
61 
62  virtual int numberOfArguments() const;
63  virtual std::string argument(int index) const;
64 
65 protected:
66  void addOption(CmdLineOptionParser* opt);
67  CmdLineOptionParser* findOption(std::string name) const;
68 
69  bool parseOption(
70  std::string option, std::string& name, std::string& arguments,
71  std::string& prefix, bool& hasArgument) const;
72  bool readPrefix(
73  std::string& option,
74  std::string& prefix,
75  bool& longOption) const;
76  bool isPrefix(std::string name) const;
77 
78  /// Database for holding options with their long names as a key.
79  std::map<std::string, CmdLineOptionParser*> optionLongNames_;
80  /// Database for holding options with their short names as a key.
81  std::map<std::string, CmdLineOptionParser*> optionShortNames_;
82 
83  /// Command line is stored here.
84  std::vector<std::string> commandLine_;
85  /// Command line arguments are stored here.
86  std::vector<std::string> arguments_;
87  /// Legal prefixes are stored here.
88  std::vector<std::string> prefixes_;
89 
90 private:
91  /// For adding new values to maps.
92  typedef std::map<std::string, CmdLineOptionParser*>::value_type valType;
93  /// For traversing non-const maps.
94  typedef std::map<std::string, CmdLineOptionParser*>::iterator mapIter;
95  /// For traversing const maps.
96  typedef
97  std::map<std::string, CmdLineOptionParser*>::const_iterator constMapIter;
98 
99  /// Copying not allowed.
101  /// Assignment not allowed.
103 
104  void parseAll();
105 
106  /// The name of the program.
107  std::string progName_;
108  /// The description of usage of program.
109  std::string description_;
110 
111  /// Number of characters reserved for printing short version
112  /// of commandline flag.
113  static const int SHORT_FLAG;
114 
115  /// Number of characters reserved for printing long version
116  /// of commandline flag.
117  static const int LONG_FLAG;
118 };
119 
120 #include "CmdLineParser.icc"
121 
122 #endif
Options
Definition: Options.hh:51
CmdLineParser::isPrefix
bool isPrefix(std::string name) const
Exception.hh
CmdLineParser::numberOfArguments
virtual int numberOfArguments() const
CmdLineParser::storeOptions
virtual void storeOptions(Options &options)
Definition: CmdLineParser.cc:83
CmdLineOptionParser.hh
CmdLineParser::LONG_FLAG
static const int LONG_FLAG
Number of characters reserved for printing long version of commandline flag.
Definition: CmdLineParser.hh:117
CmdLineParser::constMapIter
std::map< std::string, CmdLineOptionParser * >::const_iterator constMapIter
For traversing const maps.
Definition: CmdLineParser.hh:97
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
CmdLineParser::~CmdLineParser
virtual ~CmdLineParser()
Definition: CmdLineParser.cc:68
CmdLineParser::parseAll
void parseAll()
Definition: CmdLineParser.cc:182
CmdLineParser::valType
std::map< std::string, CmdLineOptionParser * >::value_type valType
For adding new values to maps.
Definition: CmdLineParser.hh:92
CmdLineParser::parseOption
bool parseOption(std::string option, std::string &name, std::string &arguments, std::string &prefix, bool &hasArgument) const
Definition: CmdLineParser.cc:277
CmdLineParser::operator=
CmdLineParser & operator=(const CmdLineParser &)
Assignment not allowed.
CmdLineParser::addOption
void addOption(CmdLineOptionParser *opt)
CmdLineParser::mapIter
std::map< std::string, CmdLineOptionParser * >::iterator mapIter
For traversing non-const maps.
Definition: CmdLineParser.hh:94
CmdLineOptionParser
Definition: CmdLineOptionParser.hh:56
Application.hh
CmdLineParser::arguments_
std::vector< std::string > arguments_
Command line arguments are stored here.
Definition: CmdLineParser.hh:86
CmdLineParser::readPrefix
bool readPrefix(std::string &option, std::string &prefix, bool &longOption) const
Definition: CmdLineParser.cc:327
CmdLineParser::CmdLineParser
CmdLineParser(std::string description)
Definition: CmdLineParser.cc:59
CmdLineParser::description_
std::string description_
The description of usage of program.
Definition: CmdLineParser.hh:109
CmdLineParser::progName_
std::string progName_
The name of the program.
Definition: CmdLineParser.hh:107
options
static MachInfoCmdLineOptions options
Definition: MachInfo.cc:46
CmdLineParser::optionShortNames_
std::map< std::string, CmdLineOptionParser * > optionShortNames_
Database for holding options with their short names as a key.
Definition: CmdLineParser.hh:81
CmdLineParser.icc
CmdLineParser::SHORT_FLAG
static const int SHORT_FLAG
Number of characters reserved for printing short version of commandline flag.
Definition: CmdLineParser.hh:113
CmdLineParser::parse
virtual void parse(char *argv[], int argc)
Definition: CmdLineParser.cc:109
CmdLineParser::findOption
CmdLineOptionParser * findOption(std::string name) const
Definition: CmdLineParser.cc:160
CmdLineParser::prefixes_
std::vector< std::string > prefixes_
Legal prefixes are stored here.
Definition: CmdLineParser.hh:88
CmdLineParser::argument
virtual std::string argument(int index) const
CmdLineParser
Definition: CmdLineParser.hh:53