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