mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
argList enhancement: added convenience methods for accessing options
Oriented somewhat on dictionary methods.
Return the argument string associated with the named option:
Info<< "-foo: " << args.option("foo") << nl;
Return true if the named option is found
if (args.optionFound("foo")) ...
Return an IStringStream to the named option
old: value = readScalar(IStringStream(args.options()["foo"])());
newer: value = readScalar(args.optionLookup("foo")());
also: List<scalar> lst(args.optionLookup("foo")());
Read a value from the named option
newest: value = args.optionRead<scalar>("foo");
Read a value from the named option if present.
old: if (args.options().found("foo"))
{
value = readScalar(IStringStream(args.options()["foo"])());
}
new: args.optionReadIfPresent("foo", value);
Read a List of values from the named option
patches = args.optionReadList<word>("patches");
Didn't bother adding optionReadListIfPresent<T>(const word&), since it
probably wouldn't be common anyhow.
This commit is contained in:
@ -50,6 +50,8 @@ int main(int argc, char *argv[])
|
||||
argList::validOptions.insert("reList", "reList");
|
||||
argList::validOptions.insert("wordList", "wordList");
|
||||
argList::validOptions.insert("stringList", "stringList");
|
||||
argList::validOptions.insert("float", "xx");
|
||||
argList::validOptions.insert("flag", "");
|
||||
|
||||
# include "setRootCase.H"
|
||||
|
||||
@ -84,19 +86,31 @@ int main(int argc, char *argv[])
|
||||
stringList sLst;
|
||||
|
||||
|
||||
if (args.options().found("reList"))
|
||||
scalar xxx(-1);
|
||||
|
||||
if (args.optionFound("flag"))
|
||||
{
|
||||
reLst = readList<wordRe>(IStringStream(args.options()["reList"])());
|
||||
Info<<"-flag:" << args.option("flag") << endl;
|
||||
}
|
||||
|
||||
if (args.options().found("wordList"))
|
||||
if (args.optionReadIfPresent<scalar>("float", xxx))
|
||||
{
|
||||
wLst = readList<word>(IStringStream(args.options()["wordList"])());
|
||||
Info<<"read float " << xxx << endl;
|
||||
}
|
||||
|
||||
if (args.options().found("stringList"))
|
||||
if (args.optionFound("reList"))
|
||||
{
|
||||
sLst = readList<string>(IStringStream(args.options()["stringList"])());
|
||||
reLst = args.optionReadList<wordRe>("reList");
|
||||
}
|
||||
|
||||
if (args.optionFound("wordList"))
|
||||
{
|
||||
wLst = args.optionReadList<word>("wordList");
|
||||
}
|
||||
|
||||
if (args.optionFound("stringList"))
|
||||
{
|
||||
sLst = args.optionReadList<string>("stringList");
|
||||
}
|
||||
|
||||
Info<< nl
|
||||
|
||||
@ -56,10 +56,10 @@ Description
|
||||
global case (same for serial and parallel jobs).
|
||||
|
||||
Note
|
||||
- Adjustment of the valid (mandatory) arguments by directly manipulating
|
||||
the static member argList::validArgs.
|
||||
- Adjustment of the valid options by directly manipulating
|
||||
the static member argList::validOptions.
|
||||
- Adjustment of the valid (mandatory) arguments
|
||||
by directly manipulating the static member argList::validArgs.
|
||||
- Adjustment of the valid options
|
||||
by directly manipulating the static member argList::validOptions.
|
||||
|
||||
SourceFiles
|
||||
argList.C
|
||||
@ -76,6 +76,7 @@ SourceFiles
|
||||
#include "word.H"
|
||||
#include "fileName.H"
|
||||
#include "parRun.H"
|
||||
#include "IStringStream.H"
|
||||
|
||||
#include "sigFpe.H"
|
||||
#include "sigInt.H"
|
||||
@ -165,22 +166,6 @@ public:
|
||||
|
||||
// Access
|
||||
|
||||
//- Return arguments
|
||||
const stringList& args() const
|
||||
{
|
||||
return args_;
|
||||
}
|
||||
|
||||
//- Return additionl arguments,
|
||||
// i.e. those additional to the executable itself
|
||||
stringList::subList additionalArgs() const;
|
||||
|
||||
//- Return options
|
||||
const Foam::HashTable<string>& options() const
|
||||
{
|
||||
return options_;
|
||||
}
|
||||
|
||||
//- Name of executable
|
||||
const word& executable() const
|
||||
{
|
||||
@ -211,6 +196,72 @@ public:
|
||||
return rootPath()/caseName();
|
||||
}
|
||||
|
||||
//- Return arguments
|
||||
const stringList& args() const
|
||||
{
|
||||
return args_;
|
||||
}
|
||||
|
||||
//- Return additionl arguments,
|
||||
// i.e. those additional to the executable itself
|
||||
stringList::subList additionalArgs() const;
|
||||
|
||||
//- Return options
|
||||
const Foam::HashTable<string>& options() const
|
||||
{
|
||||
return options_;
|
||||
}
|
||||
|
||||
//- Return the argument string associated with the named option
|
||||
const string& option(const word& opt) const
|
||||
{
|
||||
return options_.operator[](opt);
|
||||
}
|
||||
|
||||
//- Return true if the named option is found
|
||||
bool optionFound(const word& opt) const
|
||||
{
|
||||
return options_.found(opt);
|
||||
}
|
||||
|
||||
//- Return an IStringStream to the named option
|
||||
IStringStream optionLookup(const word& opt) const
|
||||
{
|
||||
return IStringStream(option(opt));
|
||||
}
|
||||
|
||||
//- Read a value from the named option
|
||||
template<class T>
|
||||
T optionRead(const word& opt) const
|
||||
{
|
||||
T val;
|
||||
optionLookup(opt)() >> val;
|
||||
return val;
|
||||
}
|
||||
|
||||
//- Read a value from the named option if present.
|
||||
// Return true if the named option was found.
|
||||
template<class T>
|
||||
bool optionReadIfPresent(const word& opt, T& val) const
|
||||
{
|
||||
if (optionFound(opt))
|
||||
{
|
||||
optionLookup(opt)() >> val;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//- Read a List of values from the named option
|
||||
template<class T>
|
||||
List<T> optionReadList(const word& opt) const
|
||||
{
|
||||
return readList<T>(optionLookup(opt)());
|
||||
}
|
||||
|
||||
|
||||
// Edit
|
||||
|
||||
|
||||
Reference in New Issue
Block a user