]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
3 | Copyright (C) 2002 PowerDNS.COM BV | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | #ifndef ARGUMENTS_HH | |
20 | #define ARGUMENTS_HH | |
21 | ||
22 | #include <map> | |
23 | #include <string> | |
24 | #include <vector> | |
25 | #include "ahuexception.hh" | |
26 | ||
27 | using namespace std; | |
28 | ||
29 | typedef AhuException ArgException; | |
30 | ||
31 | /** This class helps parsing argc and argv into a map of parameters. We have 3 kinds of formats: | |
32 | ||
33 | ||
34 | -w this leads to a key/value pair of "w"/void | |
35 | ||
36 | --port=25 "port"/"25" | |
37 | ||
38 | --daemon "daemon"/void | |
39 | ||
40 | We do not support "--port 25" syntax. | |
41 | ||
42 | It can also read from a file. This file can contain '#' to delimit comments. | |
43 | ||
44 | Some sample code: | |
45 | ||
46 | \code | |
47 | ||
48 | ArgvMap R; | |
49 | ||
50 | R.set("port")="25"; // use this to specify default parameters | |
51 | R.file("./default.conf"); // parse configuration file | |
52 | ||
53 | R.parse(argc, argv); // read the arguments from main() | |
54 | ||
55 | cout<<"Will we be a deamon?: "<<R.isset("daemon")<<endl; | |
56 | cout<<"Our port will be "<<R["port"]<<endl; | |
57 | ||
58 | map<string,string>::const_iterator i; | |
59 | cout<<"via iterator"<<endl; | |
60 | for(i=R.begin();i!=R.end();i++) | |
61 | cout<<i->first<<"="<<i->second<<endl; | |
62 | \endcode | |
63 | */ | |
64 | ||
65 | ||
66 | ||
67 | class ArgvMap | |
68 | { | |
69 | public: | |
70 | ArgvMap(); | |
71 | void parse(int &argc, char **argv, bool lax=false); //!< use this to parse from argc and argv | |
72 | void laxParse(int &argc, char **argv) //!< use this to parse from argc and argv | |
73 | { | |
74 | parse(argc,argv,true); | |
75 | } | |
76 | void ArgvMap::preParse(int &argc, char **argv, const string &arg); //!< use this to preparse a single var | |
77 | bool ArgvMap::preParseFile(const char *fname, const string &arg); //!< use this to preparse a single var in configuration | |
78 | ||
79 | bool file(const char *fname, bool lax=false); //!< Parses a file with parameters | |
80 | bool laxFile(const char *fname) | |
81 | { | |
82 | return file(fname,true); | |
83 | } | |
84 | typedef map<string,string> param_t; //!< use this if you need to know the content of the map | |
85 | bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value | |
86 | bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help) | |
87 | int asNum(const string &var); //!< return a variable value as a number | |
88 | double asDouble(const string &var); //!< return a variable value as a number | |
89 | string &set(const string &); //!< Gives a writable reference and allocates space for it | |
90 | string &set(const string &, const string &); //!< Does the same but also allows to specify a help message | |
91 | void setCmd(const string &, const string &); //!< Add a command flag | |
92 | string &setSwitch(const string &, const string &); //!< Add a command flag | |
93 | string helpstring(string prefix=""); //!< generates the --help | |
94 | string configstring(); //!< generates the --mkconfig | |
95 | bool contains(const string &var, const string &val); | |
96 | ||
97 | vector<string>list(); | |
98 | string getHelp(const string &item); | |
99 | ||
100 | const param_t::const_iterator begin(); //!< iterator semantics | |
101 | const param_t::const_iterator end(); //!< iterator semantics | |
102 | const string &operator[](const string &); //!< iterator semantics | |
103 | const vector<string>&getCommands(); | |
104 | private: | |
105 | void parseOne(const string &unparsed, const string &parseOnly="", bool lax=false); | |
106 | map<string,string> params; | |
107 | map<string,string> helpmap; | |
108 | map<string,string> d_typeMap; | |
109 | vector<string> d_cmds; | |
110 | }; | |
111 | ||
112 | extern ArgvMap &arg(); | |
113 | ||
114 | #endif /* ARGUMENTS_HH */ |