diff --git a/src/OpenFOAM/global/argList/argList.C b/src/OpenFOAM/global/argList/argList.C index a020f3e5bdca03111bb057e80410af55fec0d0f8..fba0cefe17f0ae0d80b90e03fd5047cf7dd8dc5c 100644 --- a/src/OpenFOAM/global/argList/argList.C +++ b/src/OpenFOAM/global/argList/argList.C @@ -414,11 +414,11 @@ Foam::argList::argList ( ( validOptions.found(optionName) - && validOptions[optionName] != "" + && !validOptions[optionName].empty() ) || ( validParOptions.found(optionName) - && validParOptions[optionName] != "" + && !validParOptions[optionName].empty() ) ) { @@ -833,6 +833,116 @@ Foam::argList::~argList() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +bool Foam::argList::setOption(const word& opt, const string& param) +{ + bool changed = false; + + // only allow valid options + if (validOptions.found(opt)) + { + // some options are to be protected + if + ( + opt == "case" + || opt == "parallel" + || opt == "roots" + ) + { + FatalError + <<"used argList::setOption on a protected option: '" + << opt << "'" << endl; + FatalError.exit(); + } + + if (validOptions[opt].empty()) + { + // bool option + if (!param.empty()) + { + // disallow change of type + FatalError + <<"used argList::setOption to change bool to non-bool: '" + << opt << "'" << endl; + FatalError.exit(); + } + else + { + // did not previously exist + changed = !options_.found(opt); + } + } + else + { + // non-bool option + if (param.empty()) + { + // disallow change of type + FatalError + <<"used argList::setOption to change non-bool to bool: '" + << opt << "'" << endl; + FatalError.exit(); + } + else + { + // existing value needs changing, or did not previously exist + changed = options_.found(opt) ? options_[opt] != param : true; + } + } + } + else + { + FatalError + <<"used argList::setOption on an invalid option: '" + << opt << "'" << nl << "allowed are the following:" + << validOptions << endl; + FatalError.exit(); + } + + // set/change the option as required + if (changed) + { + options_.set(opt, param); + } + + return changed; +} + + +bool Foam::argList::unsetOption(const word& opt) +{ + // only allow valid options + if (validOptions.found(opt)) + { + // some options are to be protected + if + ( + opt == "case" + || opt == "parallel" + || opt == "roots" + ) + { + FatalError + <<"used argList::unsetOption on a protected option: '" + << opt << "'" << endl; + FatalError.exit(); + } + + // remove the option, return true if state changed + return options_.erase(opt); + } + else + { + FatalError + <<"used argList::unsetOption on an invalid option: '" + << opt << "'" << nl << "allowed are the following:" + << validOptions << endl; + FatalError.exit(); + } + + return false; +} + + void Foam::argList::printNotes() const { // output notes directly - no automatic text wrapping diff --git a/src/OpenFOAM/global/argList/argList.H b/src/OpenFOAM/global/argList/argList.H index db5c620451826355083dc8bcf80b0c36833bfcf8..4f2d236338f7719ea19969f29d09e940206300ed 100644 --- a/src/OpenFOAM/global/argList/argList.H +++ b/src/OpenFOAM/global/argList/argList.H @@ -334,6 +334,19 @@ public: static void noParallel(); + //- Set option directly (use with caution) + // An option with an empty param is a bool option. + // Not all valid options can also be set: eg, -case, -roots, ... + // Return true if the existing option value needed changing, + // or if the option did not previously exist. + bool setOption(const word& opt, const string& param = ""); + + //- Unset option directly (use with caution) + // Not all valid options can also be unset: eg, -case, -roots ... + // Return true if the option existed before being unset. + bool unsetOption(const word& opt); + + // Print //- Print notes (if any)