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