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