]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/arguments.hh
Merge pull request #2008 from rubenk/dont-chmod-init-script
[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
f782fe38
MH
8
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
12c86877
BH
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
06bd9ccf 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12c86877
BH
21*/
22#ifndef ARGUMENTS_HH
23#define ARGUMENTS_HH
24
25#include <map>
58a57699 26#include <set>
12c86877
BH
27#include <string>
28#include <vector>
eefd15f9
BH
29#include <fstream>
30#include <iostream>
31#include "misc.hh"
5c409fa2 32#include "pdnsexception.hh"
76473b92
KM
33#include <sys/types.h>
34#include <pwd.h>
35#include <grp.h>
12c86877 36
10f4eea8 37#include "namespaces.hh"
12c86877 38
3f81d239 39typedef PDNSException ArgException;
12c86877
BH
40
41/** This class helps parsing argc and argv into a map of parameters. We have 3 kinds of formats:
42
43
44 -w this leads to a key/value pair of "w"/void
45
46 --port=25 "port"/"25"
47
48 --daemon "daemon"/void
49
50 We do not support "--port 25" syntax.
51
52 It can also read from a file. This file can contain '#' to delimit comments.
53
54 Some sample code:
55
56 \code
57
58 ArgvMap R;
59
60 R.set("port")="25"; // use this to specify default parameters
61 R.file("./default.conf"); // parse configuration file
62
63 R.parse(argc, argv); // read the arguments from main()
64
65 cout<<"Will we be a deamon?: "<<R.isset("daemon")<<endl;
66 cout<<"Our port will be "<<R["port"]<<endl;
67
68 map<string,string>::const_iterator i;
69 cout<<"via iterator"<<endl;
70 for(i=R.begin();i!=R.end();i++)
71 cout<<i->first<<"="<<i->second<<endl;
72 \endcode
73*/
74
75
76
77class ArgvMap
78{
79public:
80 ArgvMap();
81 void parse(int &argc, char **argv, bool lax=false); //!< use this to parse from argc and argv
82 void laxParse(int &argc, char **argv) //!< use this to parse from argc and argv
83 {
84 parse(argc,argv,true);
85 }
fd4b10f7 86 void preParse(int &argc, char **argv, const string &arg); //!< use this to preparse a single var
f64de501 87 bool preParseFile(const char *fname, const string &arg, const string& theDefault=""); //!< use this to preparse a single var in configuration
12c86877
BH
88
89 bool file(const char *fname, bool lax=false); //!< Parses a file with parameters
4f6ef75f 90 bool file(const char *fname, bool lax, bool included);
12c86877
BH
91 bool laxFile(const char *fname)
92 {
93 return file(fname,true);
94 }
d3002a04 95 bool parseFile(const char *fname, const string& arg, bool lax); //<! parse one line
12c86877
BH
96 typedef map<string,string> param_t; //!< use this if you need to know the content of the map
97 bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value
98 bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help)
ff99a74b 99 int asNum(const string &var, int def=0); //!< return a variable value as a number or the default if the variable is empty
16276aa8 100 mode_t asMode(const string &var); //!< return value interpreted as octal number
cc2978dc
BH
101 uid_t asUid(const string &var); //!< return user id, resolves if necessary
102 gid_t asGid(const string &var); //!< return group id, resolves if necessary
12c86877
BH
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
7bb72d73 105 string &set(const string &, const string &); //!< Does the same but also allows one to specify a help message
12c86877
BH
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
6bef3d91 109 string configstring(bool current=false); //!< generates the --mkconfig
12c86877 110 bool contains(const string &var, const string &val);
16276aa8 111 bool isEmpty(const string &var); //!< checks if variable has value
12c86877
BH
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();
93d7e233 120 void gatherIncludes(std::vector<std::string> &extraConfigs);
12c86877
BH
121private:
122 void parseOne(const string &unparsed, const string &parseOnly="", bool lax=false);
102eb646
BH
123 typedef map<string,string> params_t;
124 params_t params;
12c86877
BH
125 map<string,string> helpmap;
126 map<string,string> d_typeMap;
127 vector<string> d_cmds;
58a57699 128 std::set<string> d_cleared;
12c86877
BH
129};
130
131extern ArgvMap &arg();
132
133#endif /* ARGUMENTS_HH */