]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/arguments.hh
allow empty parent
[thirdparty/pdns.git] / pdns / arguments.hh
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 version 2
7 as published by the Free Software Foundation
8
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #ifndef ARGUMENTS_HH
20 #define ARGUMENTS_HH
21
22 #include <map>
23 #include <set>
24 #include <string>
25 #include <vector>
26 #include <fstream>
27 #include <iostream>
28 #include "misc.hh"
29 #include "ahuexception.hh"
30 #ifndef WIN32
31 # include <sys/types.h>
32 # include <pwd.h>
33 # include <grp.h>
34 #endif
35
36 #include "namespaces.hh"
37
38 typedef PDNSException ArgException;
39
40 /** This class helps parsing argc and argv into a map of parameters. We have 3 kinds of formats:
41
42
43 -w this leads to a key/value pair of "w"/void
44
45 --port=25 "port"/"25"
46
47 --daemon "daemon"/void
48
49 We do not support "--port 25" syntax.
50
51 It can also read from a file. This file can contain '#' to delimit comments.
52
53 Some sample code:
54
55 \code
56
57 ArgvMap R;
58
59 R.set("port")="25"; // use this to specify default parameters
60 R.file("./default.conf"); // parse configuration file
61
62 R.parse(argc, argv); // read the arguments from main()
63
64 cout<<"Will we be a deamon?: "<<R.isset("daemon")<<endl;
65 cout<<"Our port will be "<<R["port"]<<endl;
66
67 map<string,string>::const_iterator i;
68 cout<<"via iterator"<<endl;
69 for(i=R.begin();i!=R.end();i++)
70 cout<<i->first<<"="<<i->second<<endl;
71 \endcode
72 */
73
74
75
76 class ArgvMap
77 {
78 public:
79 ArgvMap();
80 void parse(int &argc, char **argv, bool lax=false); //!< use this to parse from argc and argv
81 void laxParse(int &argc, char **argv) //!< use this to parse from argc and argv
82 {
83 parse(argc,argv,true);
84 }
85 void preParse(int &argc, char **argv, const string &arg); //!< use this to preparse a single var
86 bool preParseFile(const char *fname, const string &arg, const string& theDefault=""); //!< use this to preparse a single var in configuration
87
88 bool file(const char *fname, bool lax=false); //!< Parses a file with parameters
89 bool file(const char *fname, bool lax, bool included);
90 bool laxFile(const char *fname)
91 {
92 return file(fname,true);
93 }
94 typedef map<string,string> param_t; //!< use this if you need to know the content of the map
95 bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value
96 bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help)
97 int asNum(const string &var); //!< return a variable value as a number
98 #ifndef WIN32
99 mode_t asMode(const string &var); //<!< return value interpreted as octal number
100 uid_t asUid(const string &var); //!< return user id, resolves if necessary
101 gid_t asGid(const string &var); //!< return group id, resolves if necessary
102 #endif
103 double asDouble(const string &var); //!< return a variable value as a number
104 string &set(const string &); //!< Gives a writable reference and allocates space for it
105 string &set(const string &, const string &); //!< Does the same but also allows to specify a help message
106 void setCmd(const string &, const string &); //!< Add a command flag
107 string &setSwitch(const string &, const string &); //!< Add a command flag
108 string helpstring(string prefix=""); //!< generates the --help
109 string configstring(); //!< generates the --mkconfig
110 bool contains(const string &var, const string &val);
111 bool isEmpty(const string &var); //<! checks if variable has value
112
113 vector<string>list();
114 string getHelp(const string &item);
115
116 const param_t::const_iterator begin(); //!< iterator semantics
117 const param_t::const_iterator end(); //!< iterator semantics
118 const string &operator[](const string &); //!< iterator semantics
119 const vector<string>&getCommands();
120 private:
121 void parseOne(const string &unparsed, const string &parseOnly="", bool lax=false);
122 typedef map<string,string> params_t;
123 params_t params;
124 map<string,string> helpmap;
125 map<string,string> d_typeMap;
126 vector<string> d_cmds;
127 std::set<string> d_cleared;
128 };
129
130 extern ArgvMap &arg();
131
132 #endif /* ARGUMENTS_HH */