question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide information on whether a value was defaulted

See original GitHub issue

Consider:

"""Usage:
    foo.py [--knob=<n>]

Options:
    --knob=<n>   Set the knob to n. [default: 14]
"""
import pprint

from docopt import docopt


pprint.pprint(docopt(__doc__))

The following two command line invocations give the same result:

$ python foo.py
{'--knob': '14'}
$ python foo.py --knob 14
{'--knob': '14'}

This means there is no way to tell right now whether a parameter was explicitly passed by the user, or if it was not passed and a default was used.

I propose adding a '[defaulted]' key to the returned dict, which contains a set of all the values whose defaults were used. The new result would be:

$ python foo.py
{'--knob': '14', '[defaulted]': set(['--knob'])}
$ python foo.py --knob 14
{'--knob': '14', '[defaulted]': set([])}

The rationale is this will make it much easier to handle config files where you always want the command-line option to trump the config file option. As it is now, it’s impossible to do cleanly using the docopt API.

Alternative API:

args = docopt(__doc__)
pprint.pprint(args), args.defaulted('--knob')

Result:

$ python foo.py
{'--knob': '14'}, True
$ python foo.py --knob 14
{'--knob': '14'}, False

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

3reactions
joellcommented, May 3, 2017

As a further motivating example for resolving this ambiguity, consider merging command-line options with configuration file data loaded into a compatible dict object. Without any defaults specified in the docopt text, this is easy: Just merge the cli options and configuration file dictionaries by using the configuration-file value for every option that docopt assigned None to.

However, once default options are introduced, there’s a problem. If the user passed an explicit option with the same value as the default, then the user’s value should be used, not the one in the configuration file. But if the user did not specify a value and the configuration file holds a different value than the default, the configuration file’s value should override. Because it is impossible to determine if the option’s value was set by default or not, this cannot be resolved.

2reactions
csaftoiucommented, May 3, 2017

cli default value makes sense semantically. It means: “If no value is specified anywhere, this is the value used.” If the program can run meaningfully with a default value, then it should. With your approach the program wouldn’t be able to run unless there exists a config file with a value for the given parameter. But it can make sense where a user can download a program and just run it without needing to set up any config file.

To recap, the semantics are sensible:

  • If the value is not specified in either command line or config file, the default value is used.
  • If the value is specified in the config file, then the config value is used.
  • If the value is specified on the command line, the command line value is used.
  • If the value is specified in both, the command line value should be used.

This last case is impossible to implement with docopt as it is now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What It Means, What Happens When You Default, Examples
A default happens when a borrower fails to make required payments on a debt, whether of interest or principal.
Read more >
Set default values for fields or controls - Microsoft Support
Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save...
Read more >
What is a default in information technology? - TechTarget
In IT, a default is a pre-designed value or setting that is used by a computer software or mobile application when a value...
Read more >
Are there hidden problems with default values in software?
Virtually all information systems have "default values." We put them in our systems to make things easier for the system and the end-users....
Read more >
Using Spring @Value with Defaults - Baeldung
A quick and practical guide to setting default values when using the @Value annotation in Spring.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found