Skip to content
Myr

Adding support to the context to distinguish the source of a command line parameter

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Joshua Storck · committed by Joshua Storck · parent 0b1e32b · Browse files · 3 files+95 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

3 files+95 added−2 removed

  • tests:+42 added−0 removed
  • other:+53 added−2 removed

3 files+95 added−2 removed

modified:click/__init__.py+1 added-1 removed
12 unmodified lines
13"""1415# Core classes16from .core import Context, BaseCommand, Command, MultiCommand, Group, \17 CommandCollection, Parameter, Option, Argument17 CommandCollection, Parameter, Option, Argument, ParameterSource1819# Globals20from .globals import get_current_context21
76 unmodified lines
modified:click/core.py+52 added-1 removed
127 unmodified lines
128 return (not item.is_eager, idx)129130 return sorted(declaration_order, key=sort_key)131132class ParameterSource(object):133 """134 This is an enum that indicates the source of a command line option,135 which is one of the following: COMMANDLINE, ENVIRONMENT, DEFAULT.136 The DEFAULT indicates that the default value in the decorator was used.137 This class should be converted to an enum when Python 2 support is138 dropped.139 """140 COMMANDLINE = "COMMANDLINE"141 ENVIRONMENT = "ENVIRONMENT"142 DEFAULT = "DEFAULT"143144 VALUES = [COMMANDLINE, ENVIRONMENT, DEFAULT]145 146 @classmethod147 def validate(clz, value):148 if value not in clz.VALUES:149 raise ValueError("Invalid ParameterSource value: '{}'. Valid "150 "values are: {}".format(",".join(VALUES)))151152153class Context(object):154 """The context is a special internal object that holds state relevant155 for the script execution at every single level. It's normally invisible156 to commands unless they opt-in to getting access to it.
201 unmodified lines
358 self.color = color359360 self._close_callbacks = []361 self._depth = 0362 self._parameter_sources = {}342363 364 def __enter__(self):365 self._depth += 1366 push_context(self)367 return self
224 unmodified lines
592 kwargs[param] = self.params[param]593594 return self.invoke(cmd, **kwargs)595596 def set_parameter_source(self, name, source):597 """Sets the `source` of a parameter. This indicates the 598 location from which the value of the parameter was obtained.599600 :param name: the name of the command line parameter601 :param source: the source of the the command line parameter,602 which should be a valid ParameterSource value603 """604 ParameterSource.validate(source)605 self._parameter_sources[name] = source606607 def get_parameter_source(self, name):608 """Get the `source` of a parameter. This indicates the 609 location from which the value of the parameter was obtained.610 This can be useful for determining when a user specified611 an option on the command line that is the same as the default.612 In that case, the source would be ParameterSource.COMMANDLINE,613 even though the value of the parameter was equivalent to the614 default.615616 :param name: the name of the command line parameter617 :returns: the source618 :rtype: ParameterSource619 """620 return self._parameter_sources.get(name)621 622623class BaseCommand(object):624 """The base command implements the minimal API contract of commands.625 Most code will never use this as it does not implement a lot of useful626 functionality but it can act as the direct subclass of alternative
813 unmodified lines
1440 pass14411442 def consume_value(self, ctx, opts):1443 value = opts.get(self.name)1444 source = ParameterSource.COMMANDLINE1445 if value is None:1446 value = self.value_from_envvar(ctx)1447 source = ParameterSource.ENVIRONMENT1448 if value is None:1449 value = ctx.lookup_default(self.name)1450 source = ParameterSource.DEFAULT1451 ctx.set_parameter_source(self.name, source)1452 return value14531454 def type_cast_value(self, ctx, value):1455 """Given a value this runs it properly through the type system.
457 unmodified lines
modified:tests/test_context.py+42 added
254 unmodified lines
255 def cli(ctx):256 ctx.exit(0)257258 assert cli.main([], 'test_exit_not_standalone', standalone_mode=False) == 0259260def test_parameter_source_default():261 @click.command()262 @click.pass_context263 @click.option("-o", "--option", default=1)264 def cli(ctx, option):265 assert ctx.get_parameter_source("option") == click.ParameterSource.DEFAULT266267 cli.main([], "test_parameter_source_default", standalone_mode=False) == 0268269270def test_parameter_source_commandline():271 @click.command()272 @click.pass_context273 @click.option("-o", "--option", default=1)274 def cli(ctx, option):275 assert ctx.get_parameter_source("option") == click.ParameterSource.COMMANDLINE276277 cli.main(["-o", "1"], "test_parameter_source_commandline", standalone_mode=False) == 0278 cli.main(["--option", "1"], "test_parameter_source_default", standalone_mode=False) == 0279280281def test_parameter_source_environment(runner):282 @click.command()283 @click.pass_context284 @click.option("-o", "--option", default=1)285 def cli(ctx, option):286 assert ctx.get_parameter_source("option") == click.ParameterSource.ENVIRONMENT287288 runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"CLI_OPTION": "1"},289 standalone_mode=False)290291def test_parameter_source_environment_variable_specified(runner):292 @click.command()293 @click.pass_context294 @click.option("-o", "--option", default=1, envvar="NAME")295 def cli(ctx, option):296 assert ctx.get_parameter_source("option") == click.ParameterSource.ENVIRONMENT297298 runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"NAME": "1"},299 standalone_mode=False)300