Patterns for parsing command line arguments

I’m wondering if there are recommended ways of taking command line arguments and sending them to various user classes. For instance, if I want a flag like -o outputFileName, I have to parse it in, then ultimately send it to the Run class. However, to do that, I currently send outputFileName to the ActionInitialization, which can send it to RunAction, which can then send it to Run. At each step I need to write getters and setters and create new private variables. This is a lot of work to handle a single input, let alone multiple! Is there a better way to send around these command line arguments?

I realized a G4RunManager-like class accessible from anywhere in the simulation would be useful for this, if it could store arbitrary variables. As such, I wrote my own lightweight RunManager class inheriting from G4RunManager, based on the CexmcRunManager.hh example. This seems to work (and very flexibly!), but if there’s a better or more recommended way, please let me know.

Hi Jayson,

You might be interested in taking a look at some of the Geant4DNA examples (such as the extended/medical/dna/microdosimetry application for instance). In a few of them there is a helpful command line parser included. The parser class essentially has global scope because after it has been initialized in main you can access it through CommandLineParser::GetParser() from any of your user actions. I thought it was a nice way of solving that problem.



1 Like