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:
Mark Olesen
2009-05-19 18:19:49 +02:00
parent 1d2a94c38b
commit 724b034cc7
2 changed files with 91 additions and 26 deletions

View File

@ -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