I have a Python script a.py and b.py, I’d like to call the main function of b.py and pass arguments from a.py. How to add option in a.py from inside the code and pass it to the main of b.py ?. I tried adding the dictionary my additional option but for some unknown reason the b.py doesn’t get the right value. The add_option only works for command line option.
I’ve imported b inside a.py, I’m trying to pass readOnly value to main in b.py. Basically I don’t want to pass readOnly from a.py as command line but pass the readOnly through the code inside a.py.
a.py
import b
def main()
usage = "usage: %prog [options]"
parser = OptionParser(usage)
(options, _ ) = parser.parse_args()
varsOptions = vars(options)
varsOptions['readOnly'] = True
b.main(varsOptions)
b.py
def main(argv):
usage = "usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option("-r", action="store_true", dest="readOnly")
(options, _ ) = parser.parse_args()
varsOptions = vars(options)
print(varsOptions)
if __name__ == "__main__":
main(sys.argv[1:])
Why this code doesn’t work ?.
TIA,
John
It does not work because
b.maindoes not use theargvparameter.You could pass
argvtoparse_argsas a parameter, but it should be a list of strings, not a dictionary.Try this in
a: