wibble  1.1
parser.h
Go to the documentation of this file.
1 #ifndef WIBBLE_COMMANDLINE_PARSER_H
2 #define WIBBLE_COMMANDLINE_PARSER_H
3 
5 #include <iosfwd>
6 
7 namespace wibble {
8 namespace commandline {
9 
13 class Parser : public Engine
14 {
15 protected:
17 
19 
20 public:
21  Parser(const std::string& name,
22  const std::string& usage = std::string(),
23  const std::string& description = std::string(),
24  const std::string& longDescription = std::string())
26 
33  bool parse(int argc, const char* argv[])
34  {
35  m_args.clear();
36  for (int i = 1; i < argc; i++)
37  m_args.push_back(argv[i]);
39  return false;
40  }
41 
42  bool hasNext() const { return !m_args.empty(); }
43 
44  std::string next()
45  {
46  if (m_args.empty())
47  return std::string();
48  std::string res(*m_args.begin());
49  m_args.erase(m_args.begin());
50  return res;
51  }
52 };
53 
57 class StandardParser : public Parser
58 {
59 protected:
60  std::string m_version;
61 
62 public:
63  StandardParser(const std::string& appname, const std::string& version) :
64  Parser(appname), m_version(version)
65  {
66  helpGroup = addGroup("Help options");
67  help = helpGroup->add<BoolOption>("help", 'h', "help", "",
68  "print commandline help and exit");
69  help->addAlias('?');
70  this->version = helpGroup->add<BoolOption>("version", 0, "version", "",
71  "print the program version and exit");
72  }
73 
74  void outputHelp(std::ostream& out);
75 
76  bool parse(int argc, const char* argv[]);
77 
81 };
82 
88 {
89 protected:
90  int m_section;
91  std::string m_author;
92 
93 public:
95  const std::string& appname,
96  const std::string& version,
97  int section,
98  const std::string& author) :
99  StandardParser(appname, version),
100  m_section(section), m_author(author)
101  {
102  manpage = helpGroup->add<StringOption>("manpage", 0, "manpage", "[hooks]",
103  "output the " + name() + " manpage and exit");
104  }
105 
106  bool parse(int argc, const char* argv[]);
107 
109 };
110 
116 {
117 public:
119  const std::string& appname,
120  const std::string& version,
121  int section,
122  const std::string& author) :
123  StandardParserWithManpage(appname, version, section, author)
124  {
125  helpCommand = addEngine("help", "[command]", "print help information",
126  "With no arguments, print a summary of available commands. "
127  "If given a command name as argument, print detailed informations "
128  "about that command.");
129  }
130 
131  bool parse(int argc, const char* argv[]);
132 
134 };
135 
136 }
137 }
138 
139 // vim:set ts=4 sw=4:
140 #endif