-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathargsettings.hh
117 lines (93 loc) · 3.31 KB
/
argsettings.hh
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
/*
PowerMail versatile mail receiver
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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef ARGUMENTS_HH
#define ARGUMENTS_HH
#include <map>
#include <string>
#include <vector>
#include "ahuexception.hh"
class ArgumentException
{
public:
ArgumentException(const string &str)
{
d_reason=str;
}
string txtReason()
{
return d_reason;
}
private:
string d_reason;
};
using namespace std;
class Argument;
class ArgSettings
{
public:
void preparseArgs(int argc, char **argv,const string &preparg);
void parseArgs(int argc, char **argv); //!< use this to parse from argc and argv
bool parseFiles(string &response, const char *first, ...);
bool parseFile(const char *fname); //!< Parses a file with parameters
void getRest(int argc, char **argv,vector<string>&commands); //!< return all non-option parts
bool commandGiven(const string &name); //!< returns true if the command was given
bool switchSet(const string &name); //!< returns true if a switch was set
int paramAsNum(const string &var); //!< return a variable value as a number
double paramAsDouble(const string &var); //!< return a variable value as a number
const string ¶mString(const string &var);
void addParameter(const string &name, const string &help, const string &value); //!< Does the same but also allows to specify a help message
void ignore(const string &name);
void addCommand(const string &name, const string &help); //!< Add a command flag
void addSwitch(const string &, const string &help, bool isset); //!< Add a switch which can have a value
string makeHelp(); //!< generates the --help
string makeConfig(); //!< generates the --mkconfig
typedef enum {Parameter,Switch,Command,Ignore} argtype_t;
private:
map<string,Argument> d_arguments;
void parseOne(const string &piece, Argument *arg);
void tupleToParts(vector<string>&parts,const string& tuple);
void setParam(const vector<string> &parts, bool lax=false);
string makeLeft(const string &name, const Argument &arg, bool config=false);
string wordWrap(const string &input, int leftskip);
};
class Argument
{
public:
Argument(){}
Argument(const string &help, const string &value, ArgSettings::argtype_t type);
Argument(const string &help, ArgSettings::argtype_t type);
const string &getHelp() const
{
return d_help;
}
const string ¶meterValue() const
{
return d_value;
}
bool switchIsSet() const;
bool commandGiven() const;
void switchSet(bool val);
void commandGive();
void parameterSet(const string &val);
ArgSettings::argtype_t getType() const
{
return d_type;
}
private:
string d_value;
string d_help;
ArgSettings::argtype_t d_type;
};
#endif /* ARGUMENTS_HH */