CARDS 2.4.87
Package manager for the NuTyX GNU/Linux distribution
argument_parser.h
1 // argument_parser.h
2 //
3 // Copyright (c) 2004 Johannes Winkelmann (jw at tks6 dot net)
4 // Copyright (c) 2014-2017 by NuTyX team (http://nutyx.org)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 //
21 
22 #ifndef _ARGPARSER_H_
23 #define _ARGPARSER_H_
24 
25 
26 #include "error_treat.h"
27 
28 #include <map>
29 #include <vector>
30 #include <string>
31 
32 // TODO:
33 // -- important
34 // - allow multiple occurences of arguments:
35 // prt-get --config-append="..." --config-append="..."
36 // - Allow global --help, --usage
37 // - check for duplicate entries
38 
39 // -- nice to have
40 // 2. allow optional values for args, like --with-gtk[=DIR]
41 // 4. improve predefined commands: allow descriptions even for such
42 // 7. Add predefined --version, show in usage; add Contact text
43 // 8. Allow disabling of predefined options
44 // 9. make parseError more convenient (passing cmd->name all over...)
45 
46 class APOpt;
47 class APCmd;
48 
49 
56 class ArgParser
57 {
58 public:
62  enum ArgNumberCheck { NONE, MIN, EQ, MAX };
63 
71  class APOpt
72  {
73  public:
74  friend class ArgParser;
75  APOpt() : id(-1), m_initialized(false) {}
76  bool operator ==(const APOpt& other) const { return other.id == id; }
77 
78  void init(const std::string& longName,
79  char shortName,
80  const std::string& description,
81  const bool valueRequired=false,
82  const std::string& valueName="") {
83 
84  m_initialized = true;
85  m_longName = longName;
86  m_shortName = shortName;
87  m_description = description;
88  m_valueRequired = valueRequired;
89  m_valueName = valueName;
90  }
91 
92  private:
93  int id;
94  std::string m_longName;
95  char m_shortName;
96  std::string m_shortInfo;
97  std::string m_description;
98  bool m_valueRequired;
99  std::string m_valueName;
100 
101  bool m_initialized;
102  };
103 
104  class APCmd
105  {
106  public:
107  friend class ArgParser;
108  APCmd() : id(-1) {}
109  bool operator ==(const APCmd& other) const { return other.id == id; }
110 
111  private:
112  int id;
113  };
114 
115 private:
116 
117  // internal representation of options and commands
118  class Option
119  {
120  public:
121  int id;
122  std::string description;
123  char shortName;
124  std::string longName;
125 
126 
127  bool requiresValue;
128  std::string valueName;
129  };
130 
131  class Command
132  {
133  public:
134  int id;
135  std::string name;
136  std::string description;
137  std::string shortInfo;
138  ArgNumberCheck argNumberCheck;
139  unsigned int argNumber;
140  std::string otherArguments;
141 
142  std::map<int, Option*> mandatoryOptions;
143  std::map<int, Option*> options;
144 
145  ArgParser::APCmd* apCmd;
146  };
147 
148 
149 
150 public:
151  ArgParser();
152  virtual ~ArgParser();
153 
154 
166  int addCommand(APCmd& cmd,
167  const std::string& name,
168  const std::string& shortInfo,
169  const std::string& description,
170  ArgNumberCheck argNumberCheck,
171  const int argNumber=-1,
172  const std::string& otherArguments="");
173 
174 
183  int addOption(const APCmd& cmd,
184  APOpt& key,
185  bool required);
186 
190  void parse(int argc, char** argv);
191 
196  APCmd command() const;
197 
201  std::string appName() const;
202 
206  bool isSet(const APOpt& key) const;
207 
211  std::string getOptionValue(const APOpt& key) const;
212 
216  const std::vector<std::string>& otherArguments() const;
217 
221  virtual std::string getAppIdentification() const { return ""; }
222 
223 
227  void printHelp(const std::string& cmd);
228 
229 private:
230 
231  std::string generateHelpForCommand(const std::string& command) const;
232  std::string generateUsage() const;
233 
234  bool isSet(int key) const;
235 
236  std::string generateOptionString(Option* o) const;
237 
238  void parseError(const std::string& error, const std::string& cmd="") const;
239 
240  std::map<std::string, Command*> m_commands;
241  std::map<int, Command*> m_commandIdMap;
242  std::map<int, Option*> m_options;
243 
244  std::map<char, Option*> m_optionsByShortName;
245  std::map<std::string, Option*> m_optionsByLongName;
246  std::map<int, std::string> m_setOptions;
247 
248  std::vector<std::string> m_otherArguments;
249  APCmd m_command;
250  std::string m_appName;
251 
252 
253  int m_cmdIdCounter;
254  int m_optIdCounter;
255  APOpt PREDEFINED_CMD_HELP;
256 };
257 
258 
259 #endif /* _ARGPARSER_H_ */
260 // vim:set ts=2 :
argument parser class
Definition: argument_parser.h:56
std::string appName() const
Definition: argument_parser.cxx:346
bool isSet(const APOpt &key) const
Definition: argument_parser.cxx:327
void printHelp(const std::string &cmd)
Definition: argument_parser.cxx:337
ArgNumberCheck
Definition: argument_parser.h:62
std::string getOptionValue(const APOpt &key) const
Definition: argument_parser.cxx:341
int addOption(const APCmd &cmd, APOpt &key, bool required)
Definition: argument_parser.cxx:91
void parse(int argc, char **argv)
Definition: argument_parser.cxx:144
int addCommand(APCmd &cmd, const std::string &name, const std::string &shortInfo, const std::string &description, ArgNumberCheck argNumberCheck, const int argNumber=-1, const std::string &otherArguments="")
Definition: argument_parser.cxx:53
virtual std::string getAppIdentification() const
Definition: argument_parser.h:221
Definition: argument_parser.h:71
const std::vector< std::string > & otherArguments() const
Definition: argument_parser.cxx:450
APCmd command() const
Definition: argument_parser.cxx:322
Definition: argument_parser.h:104