Skip to content
Snippets Groups Projects
Commit 724b034c authored by Mark Olesen's avatar Mark Olesen
Browse files

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.
parent 1d2a94c3
Branches
Tags
No related merge requests found
......@@ -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"))
{
Info<<"-flag:" << args.option("flag") << endl;
}
if (args.optionReadIfPresent<scalar>("float", xxx))
{
Info<<"read float " << xxx << endl;
}
if (args.optionFound("reList"))
{
reLst = readList<wordRe>(IStringStream(args.options()["reList"])());
reLst = args.optionReadList<wordRe>("reList");
}
if (args.options().found("wordList"))
if (args.optionFound("wordList"))
{
wLst = readList<word>(IStringStream(args.options()["wordList"])());
wLst = args.optionReadList<word>("wordList");
}
if (args.options().found("stringList"))
if (args.optionFound("stringList"))
{
sLst = readList<string>(IStringStream(args.options()["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,6 +166,36 @@ public:
// Access
//- Name of executable
const word& executable() const
{
return executable_;
}
//- Return root path
const fileName& rootPath() const
{
return rootPath_;
}
//- Return case name
const fileName& globalCaseName() const
{
return globalCase_;
}
//- Return case name (parallel run) or global case (serial run)
const fileName& caseName() const
{
return case_;
}
//- Return the path
fileName path() const
{
return rootPath()/caseName();
}
//- Return arguments
const stringList& args() const
{
......@@ -181,34 +212,54 @@ public:
return options_;
}
//- Name of executable
const word& executable() const
//- Return the argument string associated with the named option
const string& option(const word& opt) const
{
return executable_;
return options_.operator[](opt);
}
//- Return root path
const fileName& rootPath() const
//- Return true if the named option is found
bool optionFound(const word& opt) const
{
return rootPath_;
return options_.found(opt);
}
//- Return case name
const fileName& globalCaseName() const
//- Return an IStringStream to the named option
IStringStream optionLookup(const word& opt) const
{
return globalCase_;
return IStringStream(option(opt));
}
//- Return case name (parallel run) or global case (serial run)
const fileName& caseName() const
//- Read a value from the named option
template<class T>
T optionRead(const word& opt) const
{
return case_;
T val;
optionLookup(opt)() >> val;
return val;
}
//- Return the path
fileName path() const
//- 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
{
return rootPath()/caseName();
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)());
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment