Open dmoody256 opened 6 years ago
Does the normal trick for this kind of thing work here? Make the default be None, and if the value is still None when processing, set the "real" default.
SCons "knows" which source a value came from, since it walks through the list (cli param, SetOption, defaults from option definitions) in SConsValues.__getattr__
, but it doesn't record or have a clean way to return that info. Maybe there's a way to solve that?
I see there was a kind of solution in the linked StackOverflow article. Is there anything from that we can bring back to SCons itself? Or alternatively add as a recipe for someone else with the same need at https://scons-cookbook.readthedocs.io/en/latest ?
Can't remember if this got mentioned somewhere else. How about creating an options object that holds the "real" defaults, and put dummy ones (some kind of sentinel) in the add_option
calls; then add a processing step where if any options have the dummy default you fill them in from the defaults object - that way there's a difference between an option not set, and an option set by the user to the default value.
Leaving a note here that argparse
has something for this, sort of... you can cause an attribute not to be added to the parsed object if the argument was not present in the parsed input (cli, usually). optparse
does not have this.
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', default=argparse.SUPPRESS)
>>> parser.add_argument('--bar', default=22)
>>> parser.parse_args([]) # input source is empty...
Namespace(bar=22) # 'bar' took its default, 'foo' omitted
I think the recipe in the original submission is about the only way to do this in optparse
. Given the relative complexity, is it actually worth putting this in and wrapping it up in something that can be queried? I almost lean to the "write it up in Cookbook" approach. The behavior of passing an existing Values
object to parse_args
to avoid it populating with defaults is documented in the optparse
"manpage". But the behavior of calling the parser's get_default_values
method does not seem to be, that I can find, nor is passing a dict of defaults to the Values
constructor. And _update_careful
is clearly intended to be an internal method, and is also not in the docs. Some maybe should have been, but the status of optparse
(deprecated but not planned for removal) means it won't get those kinds of doc updates.
I sent this to the user mailing list and also wrote a stack overflow question: https://stackoverflow.com/questions/50396802/checking-if-arg-was-passed-when-default-is-set-with-python-optparse
In my case I am interested in num_jobs options, but this could apply to any option.
I want to determine if the option was set by the user even in the default case. So far I can only figure out how to do this by looking through the sys.argv, but considering that the option parser is already doing this, maybe it should save this information at the time it is parsing the args.
Since Scons already extends pythons optparse class, this should be pretty easy to add.
Here is how I did it by manually checking sys.argv:
Here's how I did by using optparse: