Added show_default parameter to Context enabling it globally.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yevgen Antymyrov · committed by Stephen Rosen · parent 30ce8ff · Browse files · 2 files·+24 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+24 added−2 removed
- tests:+16 added−0 removed
- other:+8 added−2 removed
Provenance
- Commit
9df9b07- Tree
a87c63c- Author
Yevgen Antymyrov <me@antimirov.net>- Committer
Stephen Rosen <sirosen@globus.org>- Parent
30ce8ff
2 files changed+24 added−2 removed
modified:click/core.py+8 added-2 removed
206 unmodified lines207 default is autodetection. This is only needed if ANSI208 codes are used in texts that Click prints which is by209 default not the case. This for instance would affect210 help output.211+ :param show_default: if True, shows defaults for all options.212+ Even if an option is later created with show_default=False,213+ this command-level setting overrides it.214 """215 216 def __init__(self, command, parent=None, info_name=None, obj=None,217 auto_envvar_prefix=None, default_map=None,218 terminal_width=None, max_content_width=None,219 resilient_parsing=False, allow_extra_args=None,220 allow_interspersed_args=None,221 ignore_unknown_options=None, help_option_names=None,219- token_normalize_func=None, color=None):222+ token_normalize_func=None, color=None, show_default=None):223 #: the parent context or `None` if none exists.224 self.parent = parent225 #: the :class:`Command` for this context.226 self.command = command112 unmodified lines339 340 #: Controls if styling output is wanted or not.341 self.color = color342 343+ self.show_default = show_default344+345 self._close_callbacks = []346 self._depth = 0347 348 def __enter__(self):1378 unmodified lines1727 extra.append('env var: %s' % (1728 ', '.join('%s' % d for d in envvar)1729 if isinstance(envvar, (list, tuple))1730 else envvar, ))1726- if self.default is not None and self.show_default:1731+ if self.default is not None and \1732+ (self.show_default or ctx.show_default):1733 if isinstance(self.show_default, string_types):1734 default_string = '({})'.format(self.show_default)1735 elif isinstance(self.default, (list, tuple)):1736 default_string = ', '.join('%s' % d for d in self.default)135 unmodified linesmodified:tests/test_formatting.py+16 added
295 unmodified lines296 '',297 'Options:',298 ' --help Show this message and exit.',299 ]300+301+302+def test_global_show_default(runner):303+ @click.command(context_settings=dict(show_default=True))304+ @click.option("-f", "in_file", default="out.txt", help="Output file name")305+ def cli():306+ pass307+308+ result = runner.invoke(cli, ['--help'],)309+ assert result.output.splitlines() == [310+ 'Usage: cli [OPTIONS]',311+ '',312+ 'Options:',313+ ' -f TEXT Output file name [default: out.txt]',314+ ' --help Show this message and exit. [default: False]'315+ ]