Using python optparse.py, is there a way to determine if a specific parameter value has been set from the command line or from the default value.
Ideally, I would like to have a dict the same as the default, but containing options actually provided from the command line
I know that you could compare the value for each option with the default values, but that would not mean that the value was passed through the command line, which corresponded to the default.
Thanks!
EDIT
Sorry, my original phrase was not very clear.
I have a large number of scripts called from batch files. For audit purposes, I would like to report the parameters that are transferred and whether they are transferred from the command line, by default, or in some other way to the log file.
Using the default values, you can determine if the parameter matches the default value, but this still does not tell you if it was actually provided from the command line. This can make a difference: if the option is passed from the command line and matches the default, if you then change the default in the code, the script will still get the same value.
It would be natural for me to have a default equivalent containing the actual values.
To make the question concrete, in this example:
>>> sys.argv = ['myscript.py','-a','xxx'] >>> import optparse >>> parser = optparse.OptionParser() >>> parser.add_option('-a', default = 'xxx') >>> parser.add_option('-b', default = 'yyy')
How to find out that parameter a was passed from the command line. The only way to parse the command line manually?
(I know this is a pretty minor point, but I thought it was worth considering if I lose smthing on optparse)
Thanks again