Util--  1.1
commandline.hh
Go to the documentation of this file.
1 #ifndef UTILMM_COMMANDLINE_HH
2 #define UTILMM_COMMANDLINE_HH
3 
4 #include <string>
5 #include <vector>
6 #include <list>
7 // #include <getopt.h>
8 #include <iosfwd>
9 
10 namespace utilmm
11 {
12  class config_set;
13 
14  class bad_syntax : public std::exception
15  {
16  public:
17  ~bad_syntax() throw() {}
18 
19  std::string source, error;
20  bad_syntax(std::string const& source_, std::string const& error_ = "")
21  : source(source_), error(error_) {}
22  char const* what() const throw() { return error.c_str(); }
23  };
24 
25  class commandline_error : public std::exception
26  {
27  public:
28  ~commandline_error() throw() {}
29  std::string error;
30  commandline_error(std::string const& error_)
31  : error(error_) {}
32  char const* what() const throw() { return error.c_str(); }
33  };
34 
40  {
41  public:
48  {
49  None = 0,
50  Optional = 1,
51 
52  IntArgument = 2,
53  BoolArgument = 4,
54  StringArgument = 8,
55 
56  DefaultValue = 16
57  };
58 
59  public:
64  cmdline_option(const std::string& description);
65  ~cmdline_option();
66 
67  bool isMultiple() const;
68  bool isRequired() const;
69  std::string getConfigKey() const;
70  std::string getLong() const;
71  std::string getShort() const;
72  std::string getHelp() const;
73 
74  int getArgumentFlags() const;
75 
76  bool hasArgument() const;
77  bool isArgumentOptional() const;
78 
79  bool hasDefaultValue() const;
80  std::string getDefaultValue() const;
81 
85  bool checkArgument(const std::string& value) const;
86 
87  private:
88  bool m_multiple;
89  bool m_required;
90  std::string m_config, m_long, m_short, m_help;
91 
92  int m_argument_flags;
93  std::string m_default;
94  };
95 
166  {
167  private:
168  typedef std::vector<cmdline_option> Options;
169 
170  public:
174  command_line(const char* options[]);
175 
177  command_line(const std::list<std::string>& description);
178 
179  ~command_line();
180 
188  void parse(int argc, char const* const argv[], config_set& config);
189 
195  std::list<std::string> remaining() const;
196 
198  void setBanner(std::string const& banner);
199 
201  void usage(std::ostream& out) const;
202 
203  private:
204  void add_argument(config_set& config, cmdline_option const& optdesc, std::string const& value);
205  int option_match(config_set& config, cmdline_option const& opt, int argc, char const* const* argv, int i);
206 
207  std::string m_banner;
208  Options m_options;
209  std::list<std::string> m_remaining;
210  };
211 
212  std::ostream& operator << (std::ostream& stream, utilmm::command_line const& cmdline)
213  {
214  cmdline.usage(stream);
215  return stream;
216  }
217 }
218 
219 #endif
220 
~commandline_error()
Definition: commandline.hh:28
std::string error
Definition: commandline.hh:29
Definition: commandline.hh:165
std::ostream & operator<<(std::ostream &stream, utilmm::command_line const &cmdline)
Definition: commandline.hh:212
char const * what() const
Definition: commandline.hh:22
Definition: commandline.hh:39
Definition: configset.hh:24
commandline_error(std::string const &error_)
Definition: commandline.hh:30
char const * what() const
Definition: commandline.hh:32
Definition: commandline.hh:25
std::string source
Definition: commandline.hh:19
ArgumentType
Definition: commandline.hh:47
Definition: auto_flag.hh:6
~bad_syntax()
Definition: commandline.hh:17
void usage(std::ostream &out) const
bad_syntax(std::string const &source_, std::string const &error_="")
Definition: commandline.hh:20
std::string error
Definition: commandline.hh:19
Definition: commandline.hh:14

Generated on Mon Oct 22 2018 18:39:28 for Util-- by doxygen 1.8.13
SourceForge.net Project Page