/* * Copyright (C) 2000,2003 Daniel Heck * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. * * $Id: argp.hh,v 1.1 2003/01/19 17:19:32 dheck Exp $ */ #ifndef PX_ARGP_HH #define PX_ARGP_HH #include "tools.hh" #include #include #include namespace px { namespace argp { class ArgParser : public px::Nocopy { public: // Constructors. ArgParser (); template ArgParser(Iter beg, Iter end) { feed (beg, end); } // Destructor. virtual ~ArgParser(); // Types. enum ErrorType { UnknownOpt, // Unkown option found AmbiguousOpt, // Abbreviation matches two options MissingParam, // Expected parameter is missing InvalidParam // Option does not take parameter }; // // Public interface. // virtual void on_error (ErrorType t, const std::string &arg); virtual void on_argument (const std::string &arg); virtual void on_option (int id, const std::string ¶m); // // Functions. // /* Feed new arguments into the parser. */ template void feed (ForwardIterator begin, ForwardIterator end) { std::copy(begin, end, std::back_inserter(m_arglist)); } /* Define a new option and return the corresponding . */ void def (int id, char abbr, const std::string &name, bool param = false); /* Parse all command line arguments, calling the appropriate handlers at the right time. */ void parse(); std::string errormsg(ErrorType t, const std::string &arg) const; private: struct Option { Option (int id_, char s='\0', const std::string& l="", bool param=false) : id(id_), shortopt(s), longopt(l), takesparam(param) {} int id; char shortopt; std::string longopt; bool takesparam; }; // Private Variables. typedef std::list