casacore
Loading...
Searching...
No Matches
Input.h
Go to the documentation of this file.
1//# Input.h: A simple command-line argument method for applications.
2//# Copyright (C) 1993,1994,1995,1999,2000,2001
3//# Associated Universities, Inc. Washington DC, USA.
4//#
5//# This library is free software; you can redistribute it and/or modify it
6//# under the terms of the GNU Library General Public License as published by
7//# the Free Software Foundation; either version 2 of the License, or (at your
8//# option) any later version.
9//#
10//# This library is distributed in the hope that it will be useful, but WITHOUT
11//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12//# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13//# License for more details.
14//#
15//# You should have received a copy of the GNU Library General Public License
16//# along with this library; if not, write to the Free Software Foundation,
17//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18//#
19//# Correspondence concerning AIPS++ should be addressed as follows:
20//# Internet email: casa-feedback@nrao.edu.
21//# Postal address: AIPS++ Project Office
22//# National Radio Astronomy Observatory
23//# 520 Edgemont Road
24//# Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_INPUT_H
27#define CASA_INPUT_H
28
29
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32#include <casacore/casa/Inputs/Param.h>
33#include <vector>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// Input.h: A simple command-line argument method for applications.
39// </summary>
40
41// <use visibility=export>
42
43// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tInput.cc" demos="">
44//</reviewed>
45
46// <prerequisite>
47// <li> none noted
48// </prerequisite>
49//
50// <etymology>
51// The Input class name is a reflection of it's role as the early command
52// line user interface for Casacore applications. This class provides "inputs"
53// in the form "key=value" or "-key value."
54// </etymology>
55//
56// <synopsis>
57// The Input class is a holder of parameters, either automatically assigned
58// values or altered at the execution of the program which utilizes them. The
59// parameters are associations of String "keys" to "values". The parameters
60// may be used as internal values during the program's run. The shell command
61// <srcblock>
62// shell% myexecutable limits=1000 happy=True
63// </srcblock>
64// would run "myexecutable" and set the internal parameter "limits" to a value
65// of 1000 and "happy" to True.
66//
67// The Input class is instantiated by a constructor with a single Int argument
68// which, when non-zero, switches on the filling of the keys "debug" and
69// "help" from environment variables. These two keys always exist in an
70// instance of Input. No argument to the Input constructor defaults to "debug"
71// and "help" being set to zero.
72//
73// The default existance of the help parameter allows the user to specify
74// predefined modes for the "help" key. The argument "help=prompt" turns
75// on prompting for parameter values not specified on the command-line. In
76// such an instance, the optional String arguments to Input::create become
77// important. The argument "help=keys" will print to standard output a list
78// of all the parameters.
79//
80// The default existance of the debug parameter allows the user to specify
81// levels of debugging, where 0 implies none and higher integers means more.
82// The usage would be as follows:
83// <srcblock>
84// Input inp;
85// // this will execute the block only for values higher than 5
86// if(inp.debug(5))
87// {
88// // do debugging stuff here
89// }
90// </srcblock>
91//
92// Additional parameters must be created inside the main block (or deeper)
93// of your application. The member function create() is overloaded to accept
94// from one to six String arguments. All but the first are optional. However,
95// should the user decide to call any of the get() functions which return a
96// String, that String will be empty. In this case it is assumed that all
97// values will be filled from the command line.
98// Some examples:
99// <srcblock>
100// int main(int argc,const char* argv[])
101// {
102// Input inp;
103// // Create a parameter called "foo" which defaults to True.
104// inp.create("foo", "True");
105// // Create a parameter called "iterbound" which defaults to 2000 and
106// // has a help String used in cases of prompting.
107// inp.create("iterbound", "2000", "The upper boundary of the iterator");
108// // Create a normalising value with a range, type, and unit.
109// inp.create("dividend", "10000", The normalization factor of the chutspah",
110// "0-100000", "Double", "clean steps");
111// </srcblock>
112// The parameters are "filled" from the command line arguments by the member
113// function ReadArguments(int argc, const char* argv[]). If an argument is not defined
114// within the main block but specified at the command line, an exception is
115// thrown.
116// <srcblock>
117// inp.readArguments(argc, argv);
118// </srcblock>
119//
120// Finally, the values of the various parameter's are utilized by calling the
121// Input::getWhatever(key) member functions. They return either a String or
122// are converted to the data type chosen by the "whatever" in the name of the
123// function. The value associated with the passed key is returned.
124// <srcblock>
125// // get a boolean
126// if(inp.getBool("foo")
127// // get an iteration boundary
128// for(Int i=0; i<inp.getInt("iterbound"); i++) {
129// // get a double
130// chutspah /= inp.getDouble("dividend");
131// }
132// </srcblock>
133//
134// Optional items include:
135// <ol> <li> specifying a version <src> inp.version("$ID:");</src>
136// will print at run time the version of the program being run.
137// <li> run time checking of ranges
138// <src> inp.makeMaskFromRanges(const String &ranges, uInt length,
139// Bool oneRelative=False); </src>
140// </ol>
141// </synopsis>
142//
143// <example>
144// <srcblock>
145// #include <casacore/casa/Inputs/Input.h>
146// int main(int argc, const char* argv[])
147// {
148// // instantiate an Input. The integer argument of 1 to the ctor builds
149// // the system parameters "debug" and "help" and sets their values to the
150// // shell environment variables DEBUG and HELP.
151// Input inp(1);
152// // set the version to be automatically expanded by the RCS. This will
153// // print the version at run time.
154// inp.version("$ID:$");
155// // We will now create some parameters.
156// // Create a parameter with no default value i.e. it must be set by a
157// // command line argument.
158// inp.create("test");
159// // Create a parameter with a default value.
160// inp.create("file", "$AIPSROOT/data.txt");
161// // Create a parameter with a help String which will be displayed when in
162// // the prompted entry mode.
163// inp.create("ubound", "1000", "The number of iterations to clean.");
164// // Create a parameter with a range of acceptable values. Note: checking
165// // must be done by the user as this isn't coded in.
166// inp.create("gainstride", "0.5", "The factor by which the Clean strides.",
167// "Double", "0-1.0");
168// // Create a parameter with a unit String. Note: checking must be done
169// // by the user as this test isn't coded in.
170// String help("The velocity of the Earth in the direction of the object.");
171// inp.create("velocity", "2.89e+05", help, "Double", "0-3.0e+06", "m/s");
172// // Now we close parameter creation and get the values from the command line
173// // arguments.
174// inp.readArguments(argc, argv);
175// // Now we may utilize the values from the paramters we have created.
176// // Here we are getting a boolean from the parameter with the key "test".
177// if(inp.getBool("test") {
178// // Here we get a String from the parameter with the key "file".
179// Image myImage(inp.getString("file"));
180// // Here we set the boundary of the loop.
181// for(Int i=0;i<inp.getInt("ubound"), i++) {
182// // Here we set a value to the number of baselines.
183// Int baseline = inp.getInt("baseline");
184// // Here we set the gain stride.
185// Cleaner.gain(inp.getDouble("gainstride"));
186// // lets add a debugging block
187// if(inp.debug(5)) cout << "the chutspah is " << chutspah << endl;
188// }
189// }
190// </srcblock>
191// </example>
192//
193// <motivation>
194// In the earliest days of the old AIPS++ project, the desire to start coding
195// right away led to the need for a user interface. The preexistant C language
196// method of argc/argv was enclosed in an object for easier use.
197// </motivation>
198
199
200class Input {
201public:
202
203 // The default constructor enables the creation of parameters.
204 // If the optional Int argument is non-zero, the parameters "help" and
205 // "debug" are created from their shell environment values.
206 // This puts the program in no-prompt mode unless environment variable HELP
207 // is defined with value "prompt". The output debug level is set according
208 // to the value of the environment variable DEBUG.
209 Input (Int createEnv=0);
210
211 // Destructor.
213
214 // Create a new parameter, either from scratch or looking it
215 // up from an internal list of templates.
216 // The function also checks whether parameters can still be created,
217 // and whether key is unique for the program.
218 // The value, help and remaining arguments are all optional.
219 // <note> The multiple definitions are to allow default values</note>
220 // <group>
221 void create (const String& key);
222 void create (const String& key, const String& value);
223 void create (const String& key, const String& value, const String& help);
224 void create (const String& key, const String& value, const String& help,
225 const String& type);
226 void create (const String& key, const String& value, const String& help,
227 const String& type, const String& range);
228 void create (const String& key, const String& value, const String& help,
229 const String& type, const String& range, const String& unit);
230 // </group>
231
232 // Disable the creation of parameters. Highly recommended, but
233 // not required. readArguments calls it when filling the values from argv[].
234 void close();
235
236 // fill the parameter list from argc, argv command line args
237 void readArguments (int argc, char const* const* argv);
238
239 // Get the double value of the parameter (or 0.0 if unknown key).
240 // If the program is in prompt mode, ask the user for the value.
241 Double getDouble (const String& key);
242
243 // Get the Block<double> value of the parameter (or default Block if unknown
244 // key).
245 // If the program is in prompt mode, ask the user for the value.
247
248 // Get the int value of the parameter (or 0 if unknown key).
249 // If the program is in prompt mode, ask the user for the value.
250 Int getInt (const String& key);
251
252 // Get the Block<int> value of parameter (or default Block if unknown key)
253 // If the program is in prompt mode, ask the user for the value.
255
256 // Get the String value of the parameter (or "" if unknown key).
257 // If the program is in prompt mode, ask the user for the value.
258 String getString (const String& key);
259
260 // Get the boolean value of the parameter (or FALSE if unknown key).
261 // If the program is in prompt mode, ask the user for the value.
262 Bool getBool (const String& key);
263
264 // Get the total number of parameters of this program
265 Int count() const;
266
267 // See if the current debug level is thresholded
268 Bool debug (Int l) const
269 { return (debug_level >= l) ? True : False; }
270
271 // Set a new value for an existing named parameter
272 // Returns FALSE if key is an unknown parameter name.
273 // <group>
274 Bool put (const String& key, const String& value);
275
276 // The single argument is of the form `key=value', where key is a valid
277 // parameter name.
278 Bool put (const String& keyval);
279 // </group>
280
281 // Set version string for announcements
282 void version (const String&);
283
284 // Announce program and version.
285 void announce();
286
287 // Turn a string in the form "5,7,9-11,13,2-4" into a Vector<Bool>, where
288 // each specified position or range, is set to True and every other position
289 // is set to False. While the returned vector always has a zero origin, if
290 // oneRelative is True, all the numbers in the supplied string are
291 // decremented before use. Spaces in ranges are ignored, but otherwise
292 // ill-formed strings, or numbers that would fill in beyond the length
293 // of the Vector<Bool> results in an exception being thrown.
295 Bool oneRelative=False);
296
297
298private:
299 // Get the index of the named parameter (-1 if unknown key).
300 // Anywhere from 0.. if a key is found.
301 Int getParam (const String& key) const;
302
303 // Prompt the user for a value for the parameter.
304 // If he gives a non-empty answer, set that value.
305 void prompt (Param& parameter) const;
306
307 // Bind an environment variable to a parameter
308 void envCreate (const Char *env, const String& key, const String& def);
309
310 // The actual creation of a new (system/program) parameter
311 void createPar (Int, const String&, const String&, const String&,
312 const String&, const String&, const String&);
313
314 // output to stdout a listing of all "key=value" pairs.
315 void keys();
316
317
318 // container of parameters
319 std::vector<Param> parList_p;
320
321 // version id
323
324 // parameter creation allowed?
326
327 // ask user for parameter value?
329
330 // threshold value for debug output
332
333 // "prompt" or "keys" indicates the various types of help.
335
336 // count of program parameters
338};
339
340
341} //# NAMESPACE CASACORE - END
342
343#endif
344
345
simple 1-D array
Definition Block.h:198
void create(const String &key, const String &value)
Int p_count
count of program parameters
Definition Input.h:337
void announce()
Announce program and version.
void envCreate(const Char *env, const String &key, const String &def)
Bind an environment variable to a parameter.
Bool debug(Int l) const
See if the current debug level is thresholded.
Definition Input.h:268
void create(const String &key, const String &value, const String &help, const String &type)
String getString(const String &key)
Get the String value of the parameter (or "" if unknown key).
Bool do_prompt
ask user for parameter value?
Definition Input.h:328
Int getInt(const String &key)
Get the int value of the parameter (or 0 if unknown key).
void version(const String &)
Set version string for announcements.
void prompt(Param &parameter) const
Prompt the user for a value for the parameter.
Block< Double > getDoubleArray(const String &key)
Get the Block<double> value of the parameter (or default Block if unknown key).
static Vector< Bool > makeMaskFromRanges(const String &ranges, uInt length, Bool oneRelative=False)
Turn a string in the form "5,7,9-11,13,2-4" into a Vector<Bool>, where each specified position or ran...
void close()
Disable the creation of parameters.
String version_id
version id
Definition Input.h:322
void createPar(Int, const String &, const String &, const String &, const String &, const String &, const String &)
The actual creation of a new (system/program) parameter.
Bool is_closed
parameter creation allowed?
Definition Input.h:325
Double getDouble(const String &key)
Get the double value of the parameter (or 0.0 if unknown key).
void readArguments(int argc, char const *const *argv)
fill the parameter list from argc, argv command line args
~Input()
Destructor.
std::vector< Param > parList_p
container of parameters
Definition Input.h:319
Block< Int > getIntArray(const String &key)
Get the Block<int> value of parameter (or default Block if unknown key) If the program is in prompt m...
void create(const String &key, const String &value, const String &help)
void create(const String &key, const String &value, const String &help, const String &type, const String &range)
Bool put(const String &keyval)
The single argument is of the form ‘key=value’, where key is a valid parameter name.
Int count() const
Get the total number of parameters of this program.
Input(Int createEnv=0)
The default constructor enables the creation of parameters.
void create(const String &key, const String &value, const String &help, const String &type, const String &range, const String &unit)
Bool getBool(const String &key)
Get the boolean value of the parameter (or FALSE if unknown key).
void create(const String &key)
Create a new parameter, either from scratch or looking it up from an internal list of templates.
Bool put(const String &key, const String &value)
Set a new value for an existing named parameter Returns FALSE if key is an unknown parameter name.
Int getParam(const String &key) const
Get the index of the named parameter (-1 if unknown key).
void keys()
output to stdout a listing of all "key=value" pairs.
Int debug_level
threshold value for debug output
Definition Input.h:331
String help_mode
"prompt" or "keys" indicates the various types of help.
Definition Input.h:334
String: the storage and methods of handling collections of characters.
Definition String.h:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode length(const LatticeExprNode &expr, const LatticeExprNode &axis)
2-argument function to get the length of an axis.
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44