casacore
|
#include <Input.h>
Public Member Functions | |
Input (Int createEnv=0) | |
The default constructor enables the creation of parameters. More... | |
~Input () | |
Destructor. More... | |
void | create (const String &key) |
Create a new parameter, either from scratch or looking it up from an internal list of templates. More... | |
void | create (const String &key, const String &value) |
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) |
void | create (const String &key, const String &value, const String &help, const String &type, const String &range) |
void | create (const String &key, const String &value, const String &help, const String &type, const String &range, const String &unit) |
void | close () |
Disable the creation of parameters. More... | |
void | readArguments (int argc, char const *const *argv) |
fill the parameter list from argc, argv command line args More... | |
Double | getDouble (const String &key) |
Get the double value of the parameter (or 0.0 if unknown key). More... | |
Block< Double > | getDoubleArray (const String &key) |
Get the Block<double> value of the parameter (or default Block if unknown key). More... | |
Int | getInt (const String &key) |
Get the int value of the parameter (or 0 if unknown key). More... | |
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 mode, ask the user for the value. More... | |
String | getString (const String &key) |
Get the String value of the parameter (or "" if unknown key). More... | |
Bool | getBool (const String &key) |
Get the boolean value of the parameter (or FALSE if unknown key). More... | |
Int | count () const |
Get the total number of parameters of this program. More... | |
Bool | debug (Int l) const |
See if the current debug level is thresholded. More... | |
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. More... | |
Bool | put (const String &keyval) |
The single argument is of the form ‘key=value’, where key is a valid parameter name. More... | |
void | version (const String &) |
Set version string for announcements. More... | |
void | announce () |
Announce program and version. More... | |
Static Public Member Functions | |
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 range, is set to True and every other position is set to False. More... | |
Private Member Functions | |
Int | getParam (const String &key) const |
Get the index of the named parameter (-1 if unknown key). More... | |
void | prompt (Param ¶meter) const |
Prompt the user for a value for the parameter. More... | |
void | envCreate (const Char *env, const String &key, const String &def) |
Bind an environment variable to a parameter. More... | |
void | createPar (Int, const String &, const String &, const String &, const String &, const String &, const String &) |
The actual creation of a new (system/program) parameter. More... | |
void | keys () |
output to stdout a listing of all "key=value" pairs. More... | |
Private Attributes | |
std::vector< Param > | parList_p |
container of parameters More... | |
String | version_id |
version id More... | |
Bool | is_closed |
parameter creation allowed? More... | |
Bool | do_prompt |
ask user for parameter value? More... | |
Int | debug_level |
threshold value for debug output More... | |
String | help_mode |
"prompt" or "keys" indicates the various types of help. More... | |
Int | p_count |
count of program parameters More... | |
Input.h: A simple command-line argument method for applications.
Public interface
The Input class name is a reflection of it's role as the early command line user interface for Casacore applications. This class provides "inputs" in the form "key=value" or "-key value."
The Input class is a holder of parameters, either automatically assigned values or altered at the execution of the program which utilizes them. The parameters are associations of String "keys" to "values". The parameters may be used as internal values during the program's run. The shell command
would run "myexecutable" and set the internal parameter "limits" to a value of 1000 and "happy" to True.
The Input class is instantiated by a constructor with a single Int argument which, when non-zero, switches on the filling of the keys "debug" and "help" from environment variables. These two keys always exist in an instance of Input. No argument to the Input constructor defaults to "debug" and "help" being set to zero.
The default existance of the help parameter allows the user to specify predefined modes for the "help" key. The argument "help=prompt" turns on prompting for parameter values not specified on the command-line. In such an instance, the optional String arguments to Input::create become important. The argument "help=keys" will print to standard output a list of all the parameters.
The default existance of the debug parameter allows the user to specify levels of debugging, where 0 implies none and higher integers means more. The usage would be as follows:
Additional parameters must be created inside the main block (or deeper) of your application. The member function create() is overloaded to accept from one to six String arguments. All but the first are optional. However, should the user decide to call any of the get() functions which return a String, that String will be empty. In this case it is assumed that all values will be filled from the command line. Some examples:
The parameters are "filled" from the command line arguments by the member function ReadArguments(int argc, const char* argv[]). If an argument is not defined within the main block but specified at the command line, an exception is thrown.
Finally, the values of the various parameter's are utilized by calling the Input::getWhatever(key) member functions. They return either a String or are converted to the data type chosen by the "whatever" in the name of the function. The value associated with the passed key is returned.
Optional items include:
inp.version("$ID:");
will print at run time the version of the program being run. inp.makeMaskFromRanges(const String &ranges, uInt length, Bool oneRelative=False);
In the earliest days of the old AIPS++ project, the desire to start coding right away led to the need for a user interface. The preexistant C language method of argc/argv was enclosed in an object for easier use.
casacore::Input::Input | ( | Int | createEnv = 0 | ) |
The default constructor enables the creation of parameters.
If the optional Int argument is non-zero, the parameters "help" and "debug" are created from their shell environment values. This puts the program in no-prompt mode unless environment variable HELP is defined with value "prompt". The output debug level is set according to the value of the environment variable DEBUG.
casacore::Input::~Input | ( | ) |
Destructor.
void casacore::Input::announce | ( | ) |
Announce program and version.
void casacore::Input::close | ( | ) |
Disable the creation of parameters.
Highly recommended, but not required. readArguments calls it when filling the values from argv[].
Int casacore::Input::count | ( | ) | const |
Get the total number of parameters of this program.
void casacore::Input::create | ( | const String & | key | ) |
Create a new parameter, either from scratch or looking it up from an internal list of templates.
The function also checks whether parameters can still be created, and whether key is unique for the program. The value, help and remaining arguments are all optional.
Note: The multiple definitions are to allow default values
void casacore::Input::create | ( | const String & | key, |
const String & | value, | ||
const String & | help, | ||
const String & | type | ||
) |
void casacore::Input::create | ( | const String & | key, |
const String & | value, | ||
const String & | help, | ||
const String & | type, | ||
const String & | range | ||
) |
void casacore::Input::create | ( | const String & | key, |
const String & | value, | ||
const String & | help, | ||
const String & | type, | ||
const String & | range, | ||
const String & | unit | ||
) |
|
private |
The actual creation of a new (system/program) parameter.
See if the current debug level is thresholded.
Definition at line 270 of file Input.h.
References debug_level, casacore::False, and casacore::True.
|
private |
Bind an environment variable to a parameter.
Get the boolean value of the parameter (or FALSE if unknown key).
If the program is in prompt mode, ask the user for the value.
Get the double value of the parameter (or 0.0 if unknown key).
If the program is in prompt mode, ask the user for the value.
Get the Block<double> value of the parameter (or default Block if unknown key).
If the program is in prompt mode, ask the user for the value.
Get the int value of the parameter (or 0 if unknown key).
If the program is in prompt mode, ask the user for the value.
Get the Block<int> value of parameter (or default Block if unknown key) If the program is in prompt mode, ask the user for the value.
Get the index of the named parameter (-1 if unknown key).
Anywhere from 0.. if a key is found.
Get the String value of the parameter (or "" if unknown key).
If the program is in prompt mode, ask the user for the value.
|
private |
output to stdout a listing of all "key=value" pairs.
|
static |
Turn a string in the form "5,7,9-11,13,2-4" into a Vector<Bool>, where each specified position or range, is set to True and every other position is set to False.
While the returned vector always has a zero origin, if oneRelative is True, all the numbers in the supplied string are decremented before use. Spaces in ranges are ignored, but otherwise ill-formed strings, or numbers that would fill in beyond the length of the Vector<Bool> results in an exception being thrown.
|
private |
Prompt the user for a value for the parameter.
If he gives a non-empty answer, set that value.
Set a new value for an existing named parameter Returns FALSE if key is an unknown parameter name.
The single argument is of the form ‘key=value’, where key is a valid parameter name.
void casacore::Input::readArguments | ( | int | argc, |
char const *const * | argv | ||
) |
fill the parameter list from argc, argv command line args
void casacore::Input::version | ( | const String & | ) |
Set version string for announcements.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |