File: arguments.hh

package info (click to toggle)
pdns-recursor 3.2-4+deb6u1
  • links: PTS
  • area: main
  • in suites: squeeze-lts
  • size: 1,220 kB
  • ctags: 2,721
  • sloc: cpp: 12,564; ansic: 2,587; sh: 510; makefile: 133
file content (129 lines) | stat: -rw-r--r-- 4,468 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
    PowerDNS Versatile Database Driven Nameserver
    Copyright (C) 2002  PowerDNS.COM BV

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2
    as published by the Free Software Foundation
    

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#ifndef ARGUMENTS_HH
#define ARGUMENTS_HH

#include <map>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include "misc.hh"
#include "ahuexception.hh"
#ifndef WIN32
# include <sys/types.h>
# include <pwd.h>
# include <grp.h>
#endif

using namespace std;

typedef AhuException ArgException;

/** This class helps parsing argc and argv into a map of parameters. We have 3 kinds of formats:


    -w           this leads to a key/value pair of "w"/void

    --port=25    "port"/"25"

    --daemon     "daemon"/void

    We do not support "--port 25" syntax.

    It can also read from a file. This file can contain '#' to delimit comments.

    Some sample code:

    \code

    ArgvMap R;
  
    R.set("port")="25";  // use this to specify default parameters
    R.file("./default.conf"); // parse configuration file
    
    R.parse(argc, argv); // read the arguments from main()
    
    cout<<"Will we be a deamon?: "<<R.isset("daemon")<<endl;
    cout<<"Our port will be "<<R["port"]<<endl;
    
    map<string,string>::const_iterator i;
    cout<<"via iterator"<<endl;
    for(i=R.begin();i!=R.end();i++)
    cout<<i->first<<"="<<i->second<<endl;
    \endcode
*/



class ArgvMap
{
public:
  ArgvMap();
  void parse(int &argc, char **argv, bool lax=false); //!< use this to parse from argc and argv
  void laxParse(int &argc, char **argv) //!< use this to parse from argc and argv
  {
    parse(argc,argv,true);
  }
  void preParse(int &argc, char **argv, const string &arg); //!< use this to preparse a single var
  bool preParseFile(const char *fname, const string &arg, const string& theDefault=""); //!< use this to preparse a single var in configuration

  bool file(const char *fname, bool lax=false); //!< Parses a file with parameters
  bool laxFile(const char *fname) 
  {
    return file(fname,true);
  }
  typedef map<string,string> param_t; //!< use this if you need to know the content of the map
  bool parmIsset(const string &var); //!< Checks if a parameter is set to *a* value
  bool mustDo(const string &var); //!< if a switch is given, if we must do something (--help)
  int asNum(const string &var); //!< return a variable value as a number
#ifndef WIN32
  mode_t asMode(const string &var); //<!< return value interprepted as octal number
  uid_t asUid(const string &var); //!< return user id, resolves if necessary
  gid_t asGid(const string &var); //!< return group id, resolves if necessary
#endif
  double asDouble(const string &var); //!< return a variable value as a number
  string &set(const string &); //!< Gives a writable reference and allocates space for it
  string &set(const string &, const string &); //!< Does the same but also allows to specify a help message
  void setCmd(const string &, const string &); //!< Add a command flag
  string &setSwitch(const string &, const string &); //!< Add a command flag
  string helpstring(string prefix=""); //!< generates the --help
  string configstring(); //!< generates the --mkconfig
  bool contains(const string &var, const string &val);
  bool isEmpty(const string &var); //<! checks if variable has value

  vector<string>list();
  string getHelp(const string &item);

  const param_t::const_iterator begin(); //!< iterator semantics
  const param_t::const_iterator end(); //!< iterator semantics
  const string &operator[](const string &); //!< iterator semantics
  const vector<string>&getCommands();
private:
  void parseOne(const string &unparsed, const string &parseOnly="", bool lax=false);
  typedef map<string,string> params_t;
  params_t params;
  map<string,string> helpmap;
  map<string,string> d_typeMap;
  vector<string> d_cmds;
};

extern ArgvMap &arg();

#endif /* ARGUMENTS_HH */