pdns  4.5.1
About: PowerDNS daemon - a high performance authoritative nameserver.
  Fossies Dox: pdns-4.5.1.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ArgvMap Class Reference

This class helps parsing argc and argv into a map of parameters. More...

#include <arguments.hh>

Public Types

typedef map< string, string > param_t
 use this if you need to know the content of the map
 

Public Member Functions

 ArgvMap ()
  More...
 
void parse (int &argc, char **argv, bool lax=false)
 use this to parse from argc and argv More...
 
void laxParse (int &argc, char **argv)
  More...
 
void preParse (int &argc, char **argv, const string &arg)
 use this to preparse a single var More...
 
bool preParseFile (const char *fname, const string &arg, const string &theDefault="")
 use this to preparse a single var in configuration More...
 
bool file (const char *fname, bool lax=false)
 Parses a file with parameters. More...
 
bool file (const char *fname, bool lax, bool included)
  More...
 
bool laxFile (const char *fname)
  More...
 
bool parseFile (const char *fname, const string &arg, bool lax)
  More...
 
bool parmIsset (const string &var)
 Checks if a parameter is set to a value. More...
 
bool mustDo (const string &var)
 if a switch is given, if we must do something (–help) More...
 
int asNum (const string &var, int def=0)
 return a variable value as a number or the default if the variable is empty More...
 
mode_t asMode (const string &var)
 return value interpreted as octal number More...
 
uid_t asUid (const string &var)
 return user id, resolves if necessary More...
 
gid_t asGid (const string &var)
 return group id, resolves if necessary More...
 
double asDouble (const string &var)
 return a variable value as a number More...
 
string & set (const string &)
 Gives a writable reference and allocates space for it. More...
 
string & set (const string &, const string &)
 Does the same but also allows one to specify a help message. More...
 
void setCmd (const string &, const string &)
 Add a command flag. More...
 
string & setSwitch (const string &, const string &)
 Add a switch flag. More...
 
string helpstring (string prefix="")
 generates the –help More...
 
string configstring (bool current, bool full)
 generates the –config More...
 
bool contains (const string &var, const string &val)
  More...
 
bool isEmpty (const string &var)
 checks if variable has value More...
 
void setDefault (const string &var, const string &value)
  More...
 
void setDefaults ()
  More...
 
vector< string > list ()
  More...
 
string getHelp (const string &item)
  More...
 
const param_t::const_iterator begin ()
 iterator semantics More...
 
const param_t::const_iterator end ()
 iterator semantics More...
 
const string & operator[] (const string &)
 iterator semantics More...
 
const vector< string > & getCommands ()
  More...
 
void gatherIncludes (std::vector< std::string > &extraConfigs)
  More...
 

Private Member Functions

void parseOne (const string &unparsed, const string &parseOnly="", bool lax=false)
  More...
 
const string formatOne (bool running, bool full, const string &var, const string &help, const string &theDefault, const string &value)
  More...
 

Private Attributes

map< string, string > d_params
  More...
 
map< string, string > d_unknownParams
  More...
 
map< string, string > helpmap
  More...
 
map< string, string > defaultmap
  More...
 
map< string, string > d_typeMap
  More...
 
vector< string > d_cmds
  More...
 
std::set< string > d_cleared
  More...
 

Detailed Description

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:

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 daemon?: "<<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;
This class helps parsing argc and argv into a map of parameters.
Definition: arguments.hh:76
const param_t::const_iterator end()
iterator semantics
Definition: arguments.cc:43
const param_t::const_iterator begin()
iterator semantics
Definition: arguments.cc:38
bool file(const char *fname, bool lax=false)
Parses a file with parameters.
Definition: arguments.cc:517
void parse(int &argc, char **argv, bool lax=false)
use this to parse from argc and argv
Definition: arguments.cc:448
string & set(const string &)
Gives a writable reference and allocates space for it.
Definition: arguments.cc:48

Definition at line 75 of file arguments.hh.

Member Function Documentation

◆ asDouble()

double ArgvMap::asDouble ( const string &  var)

return a variable value as a number

Definition at line 319 of file arguments.cc.

References arg(), d_params, and parmIsset().

◆ asGid()

gid_t ArgvMap::asGid ( const string &  var)

return group id, resolves if necessary

Definition at line 249 of file arguments.cc.

References arg(), d_params, and parmIsset().

◆ asMode()

mode_t ArgvMap::asMode ( const string &  var)

return value interpreted as octal number

Definition at line 233 of file arguments.cc.

References arg(), d_params, convert-yaml-to-json::mode, and parmIsset().

◆ asNum()

◆ asUid()

uid_t ArgvMap::asUid ( const string &  var)

return user id, resolves if necessary

Definition at line 270 of file arguments.cc.

References arg(), d_params, and parmIsset().

◆ begin()

const ArgvMap::param_t::const_iterator ArgvMap::begin ( )

iterator semantics

Definition at line 38 of file arguments.cc.

References d_params.

◆ configstring()

string ArgvMap::configstring ( bool  current,
bool  full 
)

generates the –config

Definition at line 191 of file arguments.cc.

References d_params, d_typeMap, d_unknownParams, defaultmap, formatOne(), helpmap, and nowTime().

Referenced by DLCurrentConfigHandler(), and main().

◆ contains()

bool ArgvMap::contains ( const string &  var,
const string &  val 
)

Definition at line 106 of file arguments.cc.

References d_params, and stringtok().

◆ end()

const ArgvMap::param_t::const_iterator ArgvMap::end ( )

iterator semantics

Definition at line 43 of file arguments.cc.

References d_params.

◆ file() [1/2]

bool ArgvMap::file ( const char *  fname,
bool  lax,
bool  included 
)

◆ file() [2/2]

bool ArgvMap::file ( const char *  fname,
bool  lax = false 
)

Parses a file with parameters.

Definition at line 517 of file arguments.cc.

References file().

Referenced by file(), laxFile(), and main().

◆ formatOne()

const string ArgvMap::formatOne ( bool  running,
bool  full,
const string &  var,
const string &  help,
const string &  theDefault,
const string &  value 
)
private

Definition at line 157 of file arguments.cc.

Referenced by configstring().

◆ gatherIncludes()

void ArgvMap::gatherIncludes ( std::vector< std::string > &  extraConfigs)

Definition at line 547 of file arguments.cc.

References d_params, Logger::Error, and g_log.

Referenced by file().

◆ getCommands()

const vector< string > & ArgvMap::getCommands ( )

Definition at line 443 of file arguments.cc.

References d_cmds.

Referenced by main().

◆ getHelp()

string ArgvMap::getHelp ( const string &  item)

Definition at line 79 of file arguments.cc.

References helpmap.

Referenced by AuthWebServer::printargs().

◆ helpstring()

string ArgvMap::helpstring ( string  prefix = "")

generates the –help

Definition at line 124 of file arguments.cc.

References d_typeMap, helpmap, and prefix.

Referenced by loadMainConfig(), and main().

◆ isEmpty()

bool ArgvMap::isEmpty ( const string &  var)

checks if variable has value

Definition at line 312 of file arguments.cc.

References arg(), d_params, and parmIsset().

◆ laxFile()

bool ArgvMap::laxFile ( const char *  fname)
inline

Definition at line 89 of file arguments.hh.

References file().

Referenced by loadMainConfig(), and main().

◆ laxParse()

void ArgvMap::laxParse ( int &  argc,
char **  argv 
)
inline
Parameters
argvuse this to parse from argc and argv

Definition at line 80 of file arguments.hh.

References parse().

Referenced by main().

◆ list()

vector< string > ArgvMap::list ( )

Definition at line 71 of file arguments.cc.

References d_params.

Referenced by apiServerConfig(), and AuthWebServer::printargs().

◆ mustDo()

◆ operator[]()

const string & ArgvMap::operator[] ( const string &  arg)

iterator semantics

Definition at line 225 of file arguments.cc.

References arg(), d_params, and parmIsset().

◆ parmIsset()

bool ArgvMap::parmIsset ( const string &  var)

Checks if a parameter is set to a value.

Definition at line 345 of file arguments.cc.

References d_params.

Referenced by asDouble(), asGid(), asMode(), asNum(), asUid(), file(), isEmpty(), operator[](), and parseOne().

◆ parse()

void ArgvMap::parse ( int &  argc,
char **  argv,
bool  lax = false 
)

use this to parse from argc and argv

Definition at line 448 of file arguments.cc.

References d_cleared, d_cmds, and parseOne().

Referenced by laxParse(), and main().

◆ parseFile()

bool ArgvMap::parseFile ( const char *  fname,
const string &  arg,
bool  lax 
)

Definition at line 466 of file arguments.cc.

References arg(), and parseOne().

Referenced by file(), and preParseFile().

◆ parseOne()

void ArgvMap::parseOne ( const string &  unparsed,
const string &  parseOnly = "",
bool  lax = false 
)
private

◆ preParse()

void ArgvMap::preParse ( int &  argc,
char **  argv,
const string &  arg 
)

use this to preparse a single var

Definition at line 457 of file arguments.cc.

References arg(), and parseOne().

◆ preParseFile()

bool ArgvMap::preParseFile ( const char *  fname,
const string &  arg,
const string &  theDefault = "" 
)

use this to preparse a single var in configuration

Definition at line 510 of file arguments.cc.

References arg(), d_params, and parseFile().

◆ set() [1/2]

string & ArgvMap::set ( const string &  var)

Gives a writable reference and allocates space for it.

Definition at line 48 of file arguments.cc.

References d_params.

Referenced by ArgvMap(), dbBench(), BackendFactory::declare(), declareArguments(), DLSettingsHandler(), file(), loadMainConfig(), main(), set(), setCmd(), and setSwitch().

◆ set() [2/2]

string & ArgvMap::set ( const string &  var,
const string &  help 
)

Does the same but also allows one to specify a help message.

Definition at line 84 of file arguments.cc.

References d_typeMap, helpmap, and set().

◆ setCmd()

void ArgvMap::setCmd ( const string &  var,
const string &  help 
)

Add a command flag.

Definition at line 91 of file arguments.cc.

References d_typeMap, helpmap, and set().

Referenced by declareArguments(), loadMainConfig(), and main().

◆ setDefault()

void ArgvMap::setDefault ( const string &  var,
const string &  value 
)

Definition at line 53 of file arguments.cc.

References defaultmap.

Referenced by BackendFactory::declare().

◆ setDefaults()

void ArgvMap::setDefaults ( )

Definition at line 59 of file arguments.cc.

References d_params, and defaultmap.

Referenced by declareArguments().

◆ setSwitch()

string & ArgvMap::setSwitch ( const string &  var,
const string &  help 
)

Add a switch flag.

Definition at line 98 of file arguments.cc.

References d_typeMap, helpmap, and set().

Referenced by declareArguments(), loadMainConfig(), and main().


The documentation for this class was generated from the following files: