How to design CLI argument parsing with variable arguments in Python for modular application?

  argparse, command-line-arguments, modularity, python

I am making a modularly extendable application in Python that has some common abstract code and can have multiple implementations that in the end achieve same goal. Implementation will be added by creating new python modules in the root. Implementations will be selected by a CLI argument --impl <implementation-name>.

Implementations will share some general argument options like --debug, –-version or --log. However, each implementation may also ask for some arguments very specific for that implementation.

For example, the application has to automate some web function and that can be done by implementing Selenium or Pypeteer. While all of them share general arguments like the compulsory url argument or --debug, --log etc., Seleniumm implementation may need to take additional arguments such as --chromedriver_address that Pypeteer does not need.

Since I cannot predict how much implementation extensions there will be, I can’t just add all of the implementation specific arguments (like --chromedriver_address) in the common code argument parser. I need to design it in such way that the user, in addition to the general arguments, will be able to provide implementation specific arguments that are unknown to the common argument parser.

For example, the script could be ran something like this for one implementation with specific implementation arguments (there can be more than one extra):

python --log --debug --impl selenium --chromedriver_address drivers/chromedriver.exe

For another implementation, without specific arguments:

python --log --debug --impl pypeteer

Important part here is that the common parser will not explicitly expect the argument –chromedriver_address and this argument will only be understood by the code written in the selenium implementation code file.

How can I achieve such argument parsing using Python’s argparse library? Not only am I lost how should I make my example functional, but also is this a good overall way of dealing with implementation specific options.

Source: Python Questions