From 07f76fd8ea662491b86b5681c7cfd38cecc37930 Mon Sep 17 00:00:00 2001 From: Zhiwen Zhao Date: Sat, 13 Jun 2026 00:01:33 -0400 Subject: [PATCH] Allow turning a switch off from the CLI (-switch=false) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switches were only recognized in the no-"=" branch, where they are unconditionally turned on. Any token with "=" went to the option path, so -gui=false was parsed as an option named "gui" — which doesn't exist (it's a switch) — and exited with "The option gui is not known to this system." A switch enabled in a YAML file therefore could not be overridden off on the command line, contradicting the documented CLI-over-YAML precedence. Intercept -= before the option branch and route true/1 to turnOn() and false/0 to turnOff() (both already exist in GSwitch); reject other values. The bare -switch form still works via the existing branch. Verified: -printSystemsMaterials=true prints materials, =false suppresses them (the off override takes effect), and =maybe is rejected with "accepts only true/false" (Geant4 11.4.1 dev container). Fixes #131 Co-Authored-By: Claude Fable 5 --- gemc/goptions/goptions.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gemc/goptions/goptions.cc b/gemc/goptions/goptions.cc index 50304bef..5a57235f 100644 --- a/gemc/goptions/goptions.cc +++ b/gemc/goptions/goptions.cc @@ -129,6 +129,18 @@ GOptions::GOptions(int argc, char* argv[], const GOptions& user_defined_options) string keyPart = argStr.substr(0, eqPos); string valuePart = argStr.substr(eqPos + 1); + // Switch with an explicit boolean value: -gui=false / -gui=true. This gives the + // CLI a way to turn a switch off, honoring the documented CLI-over-YAML precedence. + if (switches.find(keyPart) != switches.end()) { + if (valuePart == "true" || valuePart == "1") { switches[keyPart].turnOn(); } + else if (valuePart == "false" || valuePart == "0") { switches[keyPart].turnOff(); } + else { + cerr << "The switch " << keyPart << " accepts only true/false." << endl; + exit(EC__NOOPTIONFOUND); + } + continue; + } + // Strip outer quotes if present (e.g., -gstreamer="[...]") if (!valuePart.empty() && valuePart.front() == '"' && valuePart.back() == '"') { valuePart = valuePart.substr(1, valuePart.length() - 2);