ptypes

ptypes Commit Details


Date:2013-05-24 21:12:00 (11 years 7 months ago)
Author:Natalie Adams
Branch:default
Commit:da6db760aa7b
Parents: 8176df673244
Message:Adding parameter parser

Changes:
Awin32/ppparser.cxx (full)

File differences

win32/ppparser.cxx
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
130
131
132
133
134
135
136
137
138
139
140
141
#include "ptypes.h"
#include "pstreams.h"
USING_PTYPES
pparser::pparser(int argc, char * argv[], const tobjlist<Parameter> & parameters)
{
this->checkreq = true;
// Assumptions:
// *) options will not start with -
// *) parameters will not be repeated
int z = 0;
for(int i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (z != parameters.get_count())
{
for (int zz = z; zz < parameters.get_count(); zz++)
{
if (argv[i] == ("-" + parameters[zz]->longparameter) || argv[i] == ("-" + parameters[zz]->shortparameter))
{
if (parameters[zz]->numberofparameters == 0)
{
put(this->choices, argv[i], "");
} else {
variant options;
if ((i+parameters[zz]->numberofparameters) <= argc)
{
for(int ii = 0; ii < parameters[zz]->numberofparameters; ii++)
{
add(options, (pt::string)argv[ii+i+1]);
}
put(this->choices, argv[i], options);
i += parameters[zz]->numberofparameters;
} else {
this->checkreq = false;
}
}
z++;
}
}
} else {
// hit the end of scanned parameters but user provided parameters still exist?
this->checkreq = false;
this->invalidparas.add(argv[i]);
}
} else {
//should never look at a parameter that doesn't start with a -
this->checkreq = false;
this->invalidparas.add(argv[i]);
}
this->argv.add(argv[i]);
}
if (z != parameters.get_count())
{
//scan through parameters for missing paras
for(int zz = z; zz < parameters.get_count(); zz++)
{
if (parameters[zz]->required)
this->checkreq = false;
this->missingparas.add(const_cast <char *>((const char *)parameters[zz]->shortparameter));
}
}
this->paras = const_cast <tobjlist<Parameter> *>(&parameters);
}
void pparser::printusage(const char * header, const char * pname, const char * footer)
{
pout.put(header);
if (this->invalidparas.get_count() > 0)
{
pout.put("Invalid parameters: ");
for(int i = 0; i < this->invalidparas.get_count(); i++)
{
pout.put(this->invalidparas[i]);
pout.put(" ");
}
pout.put("\n\n");
}
if (this->missingparas.get_count() > 0)
{
pout.put("Missing parameters: ");
for(int i = 0; i < this->missingparas.get_count(); i++)
{
pout.put("-");
pout.put(this->missingparas[i]);
pout.put(" ");
}
pout.put("\n\n");
}
pout.put("USAGE: ");
pout.put(pname);
pout.put(" ");
string parameterlist;
string paralisthints;
for(int i = 0; i < this->paras->get_count(); i++)
{
if ((*this->paras)[i]->required)
parameterlist += "-" + (*this->paras)[i]->shortparameter + " ";
else
parameterlist += "[-" + (*this->paras)[i]->shortparameter + "] ";
paralisthints += "-" + (*this->paras)[i]->shortparameter + ", --" + (*this->paras)[i]->longparameter + "\n" + (*this->paras)[i]->hint;
}
pout.put(parameterlist);
pout.put("\n");
pout.put("\n");
pout.put(paralisthints);
pout.put(footer);
pout.put("\n");
}
bool pparser::isparameterset(const Parameter * para)
{
if (get(this->choices, "--" + para->longparameter) == nullvar && get(this->choices, "-" + para->shortparameter) == nullvar)
{
return false;
}
return true;
}
variant pparser::getoptions(const Parameter * para)
{
if (get(this->choices, "--" + para->longparameter) != nullvar)
return get(this->choices, "--" + para->longparameter);
if (get(this->choices, "-" + para->shortparameter) != nullvar)
return get(this->choices, "-" + para->shortparameter);
return nullvar;
}

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.63228s using 14 queries.