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
Provenance
- Commit
af24c8d- Tree
bc3a7f4- Author
Joshua Storck <joshua.storck@twosigma.com>- Committer
Joshua Storck <joshua.storck@twosigma.com>- Parent
0b1e32b
3 files changed+95 added−2 removed
modified:click/__init__.py+1 added-1 removed
12 unmodified lines13 """14 15 # Core classes16 from .core import Context, BaseCommand, Command, MultiCommand, Group, \17- CommandCollection, Parameter, Option, Argument17+ CommandCollection, Parameter, Option, Argument, ParameterSource18 19 # Globals20 from .globals import get_current_context21 76 unmodified linesmodified:click/core.py+52 added-1 removed
127 unmodified lines128 return (not item.is_eager, idx)129 130 return sorted(declaration_order, key=sort_key)131 132+class 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"143 144+ 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)))151+152+153 class 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 lines358 self.color = color359 360 self._close_callbacks = []361 self._depth = 0362+ self._parameter_sources = {}342-363+ 364 def __enter__(self):365 self._depth += 1366 push_context(self)367 return self224 unmodified lines592 kwargs[param] = self.params[param]593 594 return self.invoke(cmd, **kwargs)595 596+ 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.599 600+ :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] = source606+607+ 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.615+616+ :param name: the name of the command line parameter617+ :returns: the source618+ :rtype: ParameterSource619+ """620+ return self._parameter_sources.get(name)621+ 622+623 class 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 alternative813 unmodified lines1440 pass1441 1442 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 value1453 1454 def type_cast_value(self, ctx, value):1455 """Given a value this runs it properly through the type system.457 unmodified linesmodified:tests/test_context.py+42 added
254 unmodified lines255 def cli(ctx):256 ctx.exit(0)257 258 assert cli.main([], 'test_exit_not_standalone', standalone_mode=False) == 0259+260+def 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.DEFAULT266+267+ cli.main([], "test_parameter_source_default", standalone_mode=False) == 0268+269+270+def 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.COMMANDLINE276+277+ cli.main(["-o", "1"], "test_parameter_source_commandline", standalone_mode=False) == 0278+ cli.main(["--option", "1"], "test_parameter_source_default", standalone_mode=False) == 0279+280+281+def 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.ENVIRONMENT287+288+ runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"CLI_OPTION": "1"},289+ standalone_mode=False)290+291+def 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.ENVIRONMENT297+298+ runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"NAME": "1"},299+ standalone_mode=False)300+