-
Notifications
You must be signed in to change notification settings - Fork 28
/
CClctrl.h
177 lines (161 loc) · 4.42 KB
/
CClctrl.h
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* This file and CClctrl.cpp provides a base class for command line control. Various functionality such as parsing arguments and reading input files is provided.
File Notes
21/10/2005 Incorporated William Baxter's isCurrentArg modification.
20/10/2005 Minor modifications by William V. Baxter as part of the porting to Microsoft Visual C++. */
#ifndef CCLCTRL_H
#define CCLCTRL_H
#include <iostream>
#include <ctime>
#include <cstring>
#include "ndlstrutil.h"
#include "ndlutil.h"
#include "ndlfortran.h"
#include "ndlexceptions.h"
#include "CMatrix.h"
// Command line control class header.
using namespace std;
class CClctrl {
public:
// Constructor given the input arguments.
CClctrl(int argc, char** argv);
virtual ~CClctrl() {}
// Check if the current argument is one of the given flags
bool isCurrentArg(string shortName, string longName);
// Check that the current argument is valid.
void confirmCurrentArg(string argument);
void unrecognisedFlag();
// helper function for formatting help information.
void helpUsage(const string description, const int width=79, const int padding=0);
// helper function for formating help information.
void helpDescriptor(const string description, const int width=79, const int padding=5);
// helper function for formating help information.
void helpArgument(const string flags, const string explanation, const int width=79, const int padding=5);
// pauses the screen until user presses space.
void waitForSpace();
// Read in a data file in Thorsten Joachim's SVM light format.
int readSvmlDataFile(CMatrix& X, CMatrix& y, const string fileName);
// Read in a data file.
void readData(CMatrix& X, CMatrix& y, const string fileName);
// exit normally.
void exitNormal();
// Exit with an error.
void exitError(const string error);
// Virtual functions for help commands.
virtual void helpInfo()=0;
virtual void helpHeader()=0;
// true if there are still unread flags from the command line.
bool isFlags() const
{
return flags && getCurrentArgumentNo()<argc;
}
void setFlags(bool val)
{
flags = val;
}
// get and set the verbosity level.
int getVerbosity() const
{
return verbosity;
}
void setVerbosity(int val)
{
verbosity = val;
}
// get and set the seed for the random number generator.
unsigned long getSeed() const
{
return seed;
}
void setSeed(unsigned long val)
{
ndlutil::init_genrand(val);
seed = val;
}
// get and set the file format for input files.
int getFileFormat() const
{
return fileFormat;
}
void setFileFormat(int val)
{
fileFormat = val;
}
// Manipulate the current command line argument number
void incrementArgument()
{
argNo++;
}
int getCurrentArgumentNo() const
{
return argNo;
}
void setCurrentArgumentNo(int val)
{
argNo=val;
}
// Recover information from the command line arguments.
string getCurrentArgument() const
{
BOUNDCHECK(argNo<argc && argNo>=0);
return argv[argNo];
}
int getIntFromCurrentArgument() const
{
BOUNDCHECK(argNo<argc && argNo>=0);
return atol(argv[argNo]);
}
double getDoubleFromCurrentArgument() const
{
BOUNDCHECK(argNo<argc && argNo>=0);
return atof(argv[argNo]);
}
bool getBoolFromCurrentArgument() const
{
BOUNDCHECK(argNo<argc && argNo>=0);
// Assume it is a string first.
string arg = getStringFromCurrentArgument();
if(arg=="true" || arg=="1")
return true;
else if(arg=="false" || arg=="0")
return false;
else
throw ndlexceptions::CommandLineError("Current argument is not boolean.");
}
string getStringFromCurrentArgument() const
{
BOUNDCHECK(argNo<argc && argNo>=0);
return argv[argNo];
}
double getCurrentArgumentLength() const
{
return strlen(argv[argNo]);
}
// test if the argument is a flag (i.e. starts with -)
bool isCurrentArgumentFlag() const
{
if(argv[argNo][0]=='-')
return true;
else
return false;
}
// manipulate the mode --- typically for determining what the help file output will be
void setMode(string val)
{
mode = val;
}
string getMode() const
{
return mode;
}
private:
bool flags;
unsigned long seed;
int verbosity;
int fileFormat;
int argNo;
string mode;
protected:
int argc;
char** argv;
};
#endif