argparser.h (2627B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * src/argparser.h 5 * 6 * Description: 7 * <---> 8 * 9 * Authors: 10 * Andreas Aardal Hanssen <bincimap@andreas.hanssen.name> 11 * 12 * Bugs: 13 * 14 * ChangeLog: 15 * 16 * -------------------------------------------------------------------- 17 * Copyright 2002-2005 Andreas Aardal Hanssen 18 * 19 * This program is free software; you can redistribute it and/or modify 20 * it under the terms of the GNU General Public License as published by 21 * the Free Software Foundation; either version 2 of the License, or 22 * (at your option) any later version. 23 * 24 * This program is distributed in the hope that it will be useful, 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 * GNU General Public License for more details. 28 * 29 * You should have received a copy of the GNU General Public License 30 * along with this program; if not, write to the Free Software 31 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. 32 * -------------------------------------------------------------------- 33 */ 34 #ifndef ARGPARSER_H_INCLUDED 35 #define ARGPARSER_H_INCLUDED 36 #include <map> 37 #include <string> 38 #include <vector> 39 40 namespace Binc { 41 class ArgOpts { 42 public: 43 std::string c; 44 bool b; 45 bool o; 46 std::string desc; 47 48 inline ArgOpts(const std::string &chr, bool boolean, bool optional, 49 const std::string &descr) 50 { 51 c = chr; 52 b = boolean; 53 o = optional; 54 desc = descr; 55 } 56 }; 57 58 class CommandLineArgs { 59 public: 60 CommandLineArgs(void); 61 62 bool parse(int argc, char *argv[]); 63 std::string errorString(void) const; 64 65 int argc(void) const; 66 67 const std::string operator [](const std::string &arg) const; 68 69 void addOptional(const std::string &arg, const std::string &desc, 70 bool boolean); 71 void addRequired(const std::string &arg, const std::string &desc, 72 bool boolean); 73 bool hasArg(const std::string &arg) const; 74 75 std::string usageString(void) const; 76 77 void setTail(const std::string &str); 78 79 const std::vector<std::string> &getUnqualifiedArgs() const; 80 81 private: 82 void registerArg(const std::string &arg, const std::string &desc, 83 bool boolean, bool optional); 84 85 std::string errString; 86 std::map<std::string, ArgOpts> reg; 87 std::map<std::string, std::string> args; 88 std::map<std::string, bool> passedArgs; 89 std::vector<std::string> unqualified; 90 std::string tail; 91 std::string head; 92 int ac; 93 }; 94 } 95 96 #endif