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
wibble::commandline::StandardParserWithManpage
Parser for commandline arguments, with builting help functions and manpage generation.
Definition: parser.h:87
wibble::commandline::Parser::parse
bool parse(int argc, const char *argv[])
Parse the commandline.
Definition: parser.h:33
wibble::commandline::SingleOption
Definition: options.h:161
wibble::commandline::Help
Definition: doc.h:25
wibble::commandline::StandardParserWithMandatoryCommand::helpCommand
Engine * helpCommand
Definition: parser.h:133
wibble::commandline::Engine::name
const std::string & name() const
Definition: engine.h:107
wibble::commandline::Engine::addGroup
OptionGroup * addGroup(const std::string &description)
Create an OptionGroup and add it to this engine.
Definition: engine.h:161
wibble::commandline::StandardParser::outputHelp
void outputHelp(std::ostream &out)
Definition: parser.cpp:10
wibble::commandline::Manpage
Definition: doc.h:38
wibble::commandline::Parser
Generic parser for commandline arguments.
Definition: parser.h:13
wibble::commandline::Manpage::output
void output(std::ostream &out, const Engine &cp)
Definition: doc.cpp:326
wibble::commandline::Parser::m_args
ArgList m_args
Definition: parser.h:16
wibble::commandline::StandardParser::version
BoolOption * version
Definition: parser.h:80
wibble::commandline::ArgList
Definition: core.h:29
wibble::commandline::Parser::hasNext
bool hasNext() const
Definition: parser.h:42
wibble::commandline::Manpage::readHooks
void readHooks(const std::string &file)
Definition: doc.cpp:409
wibble::commandline::StandardParser::m_version
std::string m_version
Definition: parser.h:60
wibble::commandline::Engine::longDescription
std::string longDescription
Definition: engine.h:224
wibble::commandline::StandardParser::StandardParser
StandardParser(const std::string &appname, const std::string &version)
Definition: parser.h:63
wibble::commandline::StandardParserWithMandatoryCommand
Parser for commandline arguments, with builting help functions and manpage generation,...
Definition: parser.h:115
wibble::commandline::StandardParser
Parser for commandline arguments, with builting help functions.
Definition: parser.h:57
wibble::commandline::Option::addAlias
void addAlias(char c)
Definition: options.h:113
wibble::commandline::Engine::usage
std::string usage
Definition: engine.h:222
wibble::commandline::StandardParserWithMandatoryCommand::parse
bool parse(int argc, const char *argv[])
Definition: parser.cpp:61
wibble::commandline::StandardParserWithManpage::m_section
int m_section
Definition: parser.h:90
wibble::commandline::StandardParserWithManpage::StandardParserWithManpage
StandardParserWithManpage(const std::string &appname, const std::string &version, int section, const std::string &author)
Definition: parser.h:94
wibble::commandline::Help::outputHelp
void outputHelp(std::ostream &out, const Engine &cp)
Definition: doc.cpp:145
wibble::commandline::StandardParser::help
BoolOption * help
Definition: parser.h:79
wibble::exception::BadOption
Definition: core.h:12
wibble::commandline::OptionGroup::add
Option * add(Option *o)
Definition: options.h:368
wibble::commandline::Engine::addEngine
Engine * addEngine(const std::string &name, const std::string &usage=std::string(), const std::string &description=std::string(), const std::string &longDescription=std::string())
Create a Engine and add it to this engine as a command.
Definition: engine.h:182
wibble::commandline::Engine
Parse commandline options.
Definition: engine.h:38
wibble::commandline::StandardParserWithManpage::m_author
std::string m_author
Definition: parser.h:91
wibble::commandline::OptionGroup
Group related commandline options.
Definition: options.h:359
wibble::commandline::StandardParser::helpGroup
OptionGroup * helpGroup
Definition: parser.h:78
wibble::commandline::Parser::next
std::string next()
Definition: parser.h:44
wibble::commandline::MemoryManager
Keep track of various wibble::commandline components, and deallocate them at object destruction.
Definition: core.h:62
wibble::commandline::StandardParser::parse
bool parse(int argc, const char *argv[])
Definition: parser.cpp:23
wibble::commandline::Engine::parseList
ArgList::iterator parseList(ArgList &list)
Parse the list of arguments, starting at the beginning and removing the arguments it successfully par...
Definition: engine.h:89
wibble::commandline::Engine::description
std::string description
Definition: engine.h:223
parser.h
wibble::commandline::StandardParserWithMandatoryCommand::StandardParserWithMandatoryCommand
StandardParserWithMandatoryCommand(const std::string &appname, const std::string &version, int section, const std::string &author)
Definition: parser.h:118
wibble::commandline::BoolOption
Boolean option.
Definition: options.h:134
wibble::commandline::StandardParserWithManpage::manpage
StringOption * manpage
Definition: parser.h:108
wibble::commandline::Parser::m_manager
MemoryManager m_manager
Definition: parser.h:18
wibble
Definition: amorph.h:17
engine.h
wibble::commandline::Help::outputVersion
void outputVersion(std::ostream &out)
Definition: doc.cpp:140
wibble::commandline::StandardParserWithManpage::parse
bool parse(int argc, const char *argv[])
Definition: parser.cpp:44
doc.h
wibble::commandline::Parser::Parser
Parser(const std::string &name, const std::string &usage=std::string(), const std::string &description=std::string(), const std::string &longDescription=std::string())
Definition: parser.h:21