How to find out if optparse option was passed on the command line or by default

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

+7
python optparse
source share
3 answers

Instead of the following template code:

 opts, args = parser.parse_args() 

if you use the code below, you will have an opts_no_defaults extra value opts_no_defaults with parameters explicitly specified by the user:

 opts_no_defaults = optparse.Values() __, args = parser.parse_args(values=opts_no_defaults) opts = Values(parser.get_default_values().__dict__) opts._update_careful(opts_no_defaults.__dict__) 

In the end, the opts should be the same as in the original template.

 print opts_no_defaults.__dict__ print opts.__dict__ for opt in parser._get_all_options(): if opt.dest: print "Value of %s: %s" % (opt._long_opts[0], getattr(opts, opt.dest)) print "Is %s specified by user? %s" % (opt._long_opts[0], hasattr(opt_no_defaults, opt.dest)) 
+6
source share

Not knowing that your code cannot give a better answer, but ...

  • they just donโ€™t pass the default values โ€‹โ€‹to the parser and check the None values. The None value is the default value for optparse lib, so you can restore your own default and act as usual;

  • expand optparse to highlight it.

I do not know your program, but, as a rule, this is not a very good design change when the configuration is the same.

+3
source share
 def is_opt_provided (parser, dest): if any (opt.dest == dest and (opt._long_opts[0] in sys.argv[1:] or opt._short_opts[0] in sys.argv[1:]) for opt in parser._get_all_options()): return True return False 

Using:

 parser = OptionsParser() parser.add_option('-o', '--opt', dest='opt_var', ...) if is_opt_provided(parser, 'opt_var'): print "Option -o or --opt has been provided" 

It would be great if Python maintainers included the proposed function in the OptionParser class.

+1
source share

All Articles