I'm building out a set of scripts and modules for managing our infrastructure. To keep things organized I'd like to consolidate as much effort as possible and minimize boiler plate for newer scripts.
In particular the issue here is to consolidate the ArgumentParser module.
An example structure is to have scripts and libraries organized something like this:
|-- bin
|-- script1
|-- script2
|-- lib
|-- logger
|-- lib1
|-- lib2
In this scenario script1
may only make use of logger
and lib1
, while script2
would make use of logger
and lib2
. In both cases I want the logger to accept '-v' and '-d', while script1
may also accept additional args and lib2
other args. I'm aware this can lead to collisions and will manage that manually.
script1
#!/usr/bin/env python
import logger
import lib1
argp = argparse.ArgumentParser("logger", parent=[logger.argp])
script2
#!/usr/bin/env python
import logger
import lib2
logger
#!/usr/bin/env python
import argparse
argp = argparse.ArgumentParser("logger")
argp.add_argument('-v', '--verbose', action="store_true", help="Verbose output")
argp.add_argument('-d', '--debug', action="store_true", help="Debug output. Assumes verbose output.")
Each script and lib could potentially have it's own arguments, however these would all have to be consolidated into one final arg_parse()
My attempts so far have resulted in failing to inherit/extend the argp setup. How can this be done in between a library file and the script?