wibble  0.1.28
parser.h
Go to the documentation of this file.
00001 #ifndef WIBBLE_COMMANDLINE_PARSER_H
00002 #define WIBBLE_COMMANDLINE_PARSER_H
00003 
00004 #include <wibble/commandline/engine.h>
00005 #include <iosfwd>
00006 
00007 namespace wibble {
00008 namespace commandline {
00009 
00013 class Parser : public Engine
00014 {
00015 protected:
00016     ArgList m_args;
00017 
00018     MemoryManager m_manager;
00019 
00020 public:
00021     Parser(const std::string& name,
00022            const std::string& usage = std::string(),
00023            const std::string& description = std::string(),
00024            const std::string& longDescription = std::string())
00025         : Engine(&m_manager, name, usage, description, longDescription) {}
00026 
00033     bool parse(int argc, const char* argv[])
00034     {
00035         m_args.clear();
00036         for (int i = 1; i < argc; i++)
00037             m_args.push_back(argv[i]);
00038         parseList(m_args);
00039         return false;
00040     }
00041 
00042     bool hasNext() const { return !m_args.empty(); }
00043 
00044     std::string next()
00045     {
00046         if (m_args.empty())
00047             return std::string();
00048         std::string res(*m_args.begin());
00049         m_args.erase(m_args.begin());
00050         return res;
00051     }
00052 };
00053 
00057 class StandardParser : public Parser
00058 {
00059 protected:
00060     std::string m_version;
00061 
00062 public:
00063     StandardParser(const std::string& appname, const std::string& version) :
00064         Parser(appname), m_version(version)
00065     {
00066         helpGroup = addGroup("Help options");
00067         help = helpGroup->add<BoolOption>("help", 'h', "help", "",
00068                 "print commandline help and exit");
00069         help->addAlias('?');
00070         this->version = helpGroup->add<BoolOption>("version", 0, "version", "",
00071                 "print the program version and exit");
00072     }
00073 
00074     void outputHelp(std::ostream& out);
00075 
00076     bool parse(int argc, const char* argv[]);
00077 
00078     OptionGroup* helpGroup;
00079     BoolOption* help;
00080     BoolOption* version;
00081 };
00082 
00087 class StandardParserWithManpage : public StandardParser
00088 {
00089 protected:
00090     int m_section;
00091     std::string m_author;
00092 
00093 public:
00094     StandardParserWithManpage(
00095             const std::string& appname,
00096             const std::string& version,
00097             int section,
00098             const std::string& author) :
00099         StandardParser(appname, version),
00100         m_section(section), m_author(author)
00101     {
00102         manpage = helpGroup->add<StringOption>("manpage", 0, "manpage", "[hooks]",
00103                 "output the " + name() + " manpage and exit");
00104     }
00105 
00106     bool parse(int argc, const char* argv[]);
00107 
00108     StringOption* manpage;
00109 };
00110 
00115 class StandardParserWithMandatoryCommand : public StandardParserWithManpage
00116 {
00117 public:
00118     StandardParserWithMandatoryCommand(
00119             const std::string& appname,
00120             const std::string& version,
00121             int section,
00122             const std::string& author) :
00123         StandardParserWithManpage(appname, version, section, author)
00124     {
00125         helpCommand = addEngine("help", "[command]", "print help information",
00126                 "With no arguments, print a summary of available commands.  "
00127                 "If given a command name as argument, print detailed informations "
00128                 "about that command.");
00129     }
00130 
00131     bool parse(int argc, const char* argv[]);
00132 
00133     Engine* helpCommand;
00134 };
00135 
00136 }
00137 }
00138 
00139 // vim:set ts=4 sw=4:
00140 #endif