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