From 1dc877db5ded4908a3ee017ef211cd3250b16c51 Mon Sep 17 00:00:00 2001 From: jsteemann Date: Fri, 26 Feb 2016 22:31:44 +0100 Subject: [PATCH] allow or disallow overriding options --- lib/ProgramOptions2/ProgramOptions.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ProgramOptions2/ProgramOptions.h b/lib/ProgramOptions2/ProgramOptions.h index ee58cc4d99..7aa966a5bb 100644 --- a/lib/ProgramOptions2/ProgramOptions.h +++ b/lib/ProgramOptions2/ProgramOptions.h @@ -105,9 +105,15 @@ class ProgramOptions { ProcessingResult& processingResult() { return _processingResult; } // seal the options - // tryin to add an option or a section after sealing will throw an error + // trying to add an option or a section after sealing will throw an error void seal() { _sealed = true; } + // allow or disallow overriding already set options + bool allowOverride(bool value) { + checkIfSealed(); + _overrideOptions = value; + } + // set context for error reporting void setContext(std::string const& value) { _context = value; } @@ -260,6 +266,11 @@ class ProgramOptions { // sets a value for an option bool setValue(std::string const& name, std::string const& value) { + if (!_overrideOptions && _processingResult.touched(name)) { + // option already set. don't override it + return true; + } + auto parts = Option::splitName(name); auto it = _sections.find(parts.first); @@ -463,6 +474,8 @@ class ProgramOptions { ProcessingResult _processingResult; // whether or not the program options setup is still mutable bool _sealed; + // allow or disallow overriding already set options + bool _overrideOptions; }; } }