Commit 31b17221 authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: support predicate checks for argList (similar to dictionary methods)

- Favour use of argList methods that are more similar to dictionary
  method names with the aim of reducing the cognitive load.

  * Silently deprecate two-parameter get() method in favour of the
    more familiar getOrDefault.
  * Silently deprecate opt() method in favour of get()

  These may be verbosely deprecated in future versions.
parent 695766af
......@@ -40,7 +40,7 @@ if (args.found("initialiseUBCs"))
// converting fixed-value BCs to zero-gradient and vice versa.
// Allow override from command-line -pName option
const word pName = args.get<word>("pName", "p");
const word pName = args.getOrDefault<word>("pName", "p");
// Infer the pressure BCs from the velocity
wordList pBCTypes
......
......@@ -40,7 +40,7 @@ if (args.found("initialiseUBCs"))
// converting fixed-value BCs to zero-gradient and vice versa.
// Allow override from command-line -pName option
const word pName = args.get<word>("pName", "p");
const word pName = args.getOrDefault<word>("pName", "p");
// Infer the pressure BCs from the velocity
wordList pBCTypes
......
......@@ -121,7 +121,7 @@ volScalarField alphac
const word kinematicCloudName
(
args.get<word>("cloud", "kinematicCloud")
args.getOrDefault<word>("cloud", "kinematicCloud")
);
Info<< "Constructing kinematicCloud " << kinematicCloudName << endl;
......
......@@ -59,7 +59,7 @@ volScalarField mu
const word kinematicCloudName
(
args.get<word>("cloud", "kinematicCloud")
args.getOrDefault<word>("cloud", "kinematicCloud")
);
Info<< "Constructing kinematicCloud " << kinematicCloudName << endl;
......
......@@ -51,7 +51,7 @@ autoPtr<compressible::turbulenceModel> turbulence
const word kinematicCloudName
(
args.get<word>("cloud", "kinematicCloud")
args.getOrDefault<word>("cloud", "kinematicCloud")
);
Info<< "Constructing kinematicCloud " << kinematicCloudName << endl;
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2019 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,11 +29,53 @@ Description
#include "argList.H"
#include "IOstreams.H"
#include "MinMax.H"
#include "Switch.H"
#include "StringStream.H"
using namespace Foam;
void predicateTests_label(const word& optName, const argList& args)
{
Info<< "predicate tests for " << optName << nl;
const bool throwingError = FatalError.throwExceptions();
try
{
label val;
val = args.getCheck<label>(optName, labelMinMax::ge(0));
}
catch (const Foam::error& err)
{
Info<< "Caught FatalError "
<< err << nl << endl;
}
FatalError.throwExceptions(throwingError);
}
void predicateTests_scalar(const word& optName, const argList& args)
{
Info<< "predicate tests for " << optName << nl;
const bool throwingError = FatalError.throwExceptions();
try
{
scalar val;
val = args.getCheck<scalar>(optName, scalarMinMax::ge(0));
}
catch (const Foam::error& err)
{
Info<< "Caught FatalError "
<< err << nl << endl;
}
FatalError.throwExceptions(throwingError);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -47,6 +89,11 @@ int main(int argc, char *argv[])
argList::addOption("scalar", "value", "Test parsing of scalar");
argList::addOption("string", "value", "Test string lookup");
argList::addOption("relative", "PATH", "Test relativePath");
argList::addBoolOption
(
"predicates",
"Apply some predicate tests (for label and scalar)"
);
// These are actually lies (never had -parseLabel, -parseScalar etc),
// but good for testing...
......@@ -105,6 +152,11 @@ int main(int argc, char *argv[])
if (args.readIfPresent("label", ival))
{
Info<< ival << nl;
if (args.found("predicates"))
{
predicateTests_label("label", args);
}
}
else
{
......@@ -115,6 +167,11 @@ int main(int argc, char *argv[])
if (args.readIfPresent("scalar", sval))
{
Info<< sval << nl;
if (args.found("predicates"))
{
predicateTests_scalar("scalar", args);
}
}
else
{
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -86,7 +86,8 @@ int main(int argc, char *argv[])
instantList times = timeSelector::selectIfPresent(runTime, args);
// Allow override of decomposeParDict location
const fileName decompDictFile = args.get<fileName>("decomposeParDict", "");
const fileName decompDictFile =
args.getOrDefault<fileName>("decomposeParDict", "");
wordList regionNames;
wordList regionDirs;
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017-2018 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -100,8 +100,8 @@ int main(int argc, char *argv[])
const bool allRegions = args.found("allRegions");
const bool verbose = args.found("verbose");
const label numSubdomains = args.get<label>("domains", 0);
const word methodName = args.get<word>("method", word::null);
const label numSubdomains = args.getOrDefault<label>("domains", 0);
const word methodName = args.getOrDefault<word>("method", word::null);
// Set time from database
#include "createTime.H"
......@@ -109,7 +109,8 @@ int main(int argc, char *argv[])
instantList times = timeSelector::selectIfPresent(runTime, args);
// Allow override of decomposeParDict location
const fileName decompDictFile = args.get<fileName>("decomposeParDict", "");
const fileName decompDictFile =
args.getOrDefault<fileName>("decomposeParDict", "");
// Get all region names
wordList regionNames;
......@@ -123,7 +124,8 @@ int main(int argc, char *argv[])
else
{
regionNames.resize(1);
regionNames.first() = args.get<word>("region", fvMesh::defaultRegion);
regionNames.first() =
args.getOrDefault<word>("region", fvMesh::defaultRegion);
}
forAll(regionNames, regioni)
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
#include "setRootCase.H"
const label maxCount = args.get<label>("max", 1000);
const label maxCount = args.getOrDefault<label>("max", 1000);
externalFileCoupler coupler;
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -198,7 +199,7 @@ int main(int argc, char *argv[])
}
{
const label celli = args.get<label>("cell", 0);
const label celli = args.getOrDefault<label>("cell", 0);
tensorField mI(momentOfInertia::meshInertia(mesh));
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2013 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -67,7 +68,7 @@ int main(int argc, char *argv[])
bool useBSpline = args.found("B");
bool useCatmullRom = args.found("CMR");
const label nSeg = args.get<label>("n", 20);
const label nSeg = args.getOrDefault<label>("n", 20);
if (!useCatmullRom && !useBSpline)
{
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenCFD Ltd.
Copyright (C) 2017-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -130,7 +130,7 @@ int main(int argc, char *argv[])
#include "setRootCase.H"
#include "createTime.H"
const scalar scaleFactor = args.get<scalar>("scale", -1);
const scalar scaleFactor = args.getOrDefault<scalar>("scale", -1);
const word outputFile(args.executable() + ".obj");
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2016 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -141,7 +141,7 @@ int main(int argc, char *argv[])
#include "setRootCase.H"
const bool optStdout = args.found("stdout");
const scalar scaleFactor = args.get<scalar>("scale", 0);
const scalar scaleFactor = args.getOrDefault<scalar>("scale", 0);
const fileName importName = args[1];
const fileName exportName = optStdout ? "-stdout" : args[2];
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2019 OpenCFD Ltd.
Copyright (C) 2019-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -84,8 +84,7 @@ int main(int argc, char *argv[])
return 1;
}
const word writerType =
args.lookupOrDefault<word>("type", exportName.ext());
const word writerType = args.getOrDefault<word>("type", exportName.ext());
auto surfWriter = surfaceWriter::New(writerType);
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -53,7 +54,7 @@ int main(int argc, char *argv[])
argList args(argc, argv, false, true);
const label repeat = args.get<label>("repeat", 1);
const label repeat = args.getOrDefault<label>("repeat", 1);
const bool optVerbose = args.found("verbose");
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -47,7 +47,7 @@ int main(int argc, char *argv[])
argList args(argc, argv, false, true);
const scalar currTime = args.get<scalar>("time", GREAT);
const scalar currTime = args.getOrDefault<scalar>("time", GREAT);
Info<< "Using currTime = " << currTime << nl
<< "when loading " << (args.size()-1) << " files" << nl << nl;
......
......@@ -125,7 +125,7 @@ int main(int argc, char *argv[])
}
{
const int prec = args.lookupOrDefault<int>("precision", 0u);
const int prec = args.getOrDefault<int>("precision", 0u);
if (prec > 0)
{
IOstream::defaultPrecision(prec);
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2016-2018 OpenCFD Ltd.
Copyright (C) 2016-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -383,7 +383,7 @@ int main(int argc, char *argv[])
// Sin of angle between two consecutive edges on a face.
// If sin(angle) larger than this the face will be considered concave.
const scalar concaveAngle = args.get<scalar>("concaveAngle", 30);
const scalar concaveAngle = args.getOrDefault<scalar>("concaveAngle", 30);
const scalar concaveSin = Foam::sin(degToRad(concaveAngle));
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -561,7 +562,7 @@ int main(int argc, char *argv[])
const bool geometry = args.found("geometry");
const bool overwrite = args.found("overwrite");
const scalar edgeTol = args.get<scalar>("tol", 0.2);
const scalar edgeTol = args.getOrDefault<scalar>("tol", 0.2);
Info<< "Trying to split cells with internal angles > feature angle\n" << nl
<< "featureAngle : " << featureAngle << nl
......
......@@ -323,7 +323,7 @@ int main(int argc, char *argv[])
FatalError.exit();
}
const scalar scaleFactor = args.get<scalar>("scale", 1);
const scalar scaleFactor = args.getOrDefault<scalar>("scale", 1);
#include "createTime.H"
......
Markdown is supported
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