Added documentation on how to use get_parameter_source. Added DEFAULT_MAP as a source and added unit tests for it.
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 17e6f3b · Browse files · 4 files·+59 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+59 added−17 removed
- tests:+22 added−11 removed
- other:+37 added−6 removed
Provenance
- Commit
73f470e- Tree
c7e5d10- Author
Joshua Storck <joshua.storck@twosigma.com>- Committer
Joshua Storck <joshua.storck@twosigma.com>- Parent
17e6f3b
4 files changed+59 added−17 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+9 added-5 removed
130 unmodified lines131 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.135+ which is one of the following: COMMANDLINE, ENVIRONMENT, DEFAULT, DEFAULT_MAP.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]143+ DEFAULT_MAP = "DEFAULT_MAP"144 145+ VALUES = [COMMANDLINE, ENVIRONMENT, DEFAULT, DEFAULT_MAP]146+ 147 @classmethod148 def validate(clz, value):149 if value not in clz.VALUES:150 raise ValueError("Invalid ParameterSource value: '{}'. Valid "1296 unmodified lines1447 value = self.value_from_envvar(ctx)1448 source = ParameterSource.ENVIRONMENT1449 if value is None:1450 value = ctx.lookup_default(self.name)1450- source = ParameterSource.DEFAULT1451- ctx.set_parameter_source(self.name, source)1451+ source = ParameterSource.DEFAULT_MAP1452+ if value is not None:1453+ ctx.set_parameter_source(self.name, source)1454 return value1455 1456 def type_cast_value(self, ctx, value):1457 """Given a value this runs it properly through the type system.38 unmodified lines1496 value = self.process_value(ctx, value)1497 1498 if value is None and not ctx.resilient_parsing:1499 value = self.get_default(ctx)1500+ if value is not None:1501+ ctx.set_parameter_source(self.name, ParameterSource.DEFAULT)1502 1503 if self.required and self.value_is_missing(value):1504 raise MissingParameter(ctx=ctx, param=self)1505 411 unmodified linesmodified:docs/advanced.rst+27 added
375 unmodified lines376 do. However if you do use this for threading you need to be very careful377 as the vast majority of the context is not thread safe! You are only378 allowed to read from the context, but not to perform any modifications on379 it.380+381+382+Detecting the Source of a Parameter383+-----------------------------------384+385+In some situations it's helpful to understand whether or not an option386+or parameter came from the command line, the environment, the default387+value, or the default_map. The :meth:`Context.get_parameter_source`388+method can be used to find this out.389+390+.. click:example::391+392+ @click.command()393+ @click.argument('port', nargs=1, default=8080, envvar="PORT")394+ @click.pass_context395+ def cli(ctx, port):396+ source = ctx.get_parameter_source("port")397+ click.echo("Port came from {}".format(source))398+399+.. click:run::400+401+ invoke(cli, prog_name='cli', args=['8080'])402+ println()403+ invoke(cli, prog_name='cli', args=[], env={"PORT": "8080"})404+ println()405+ invoke(cli, prog_name='cli', args=[])406+ println()modified:tests/test_context.py+22 added-11 removed
261 unmodified lines262 @click.pass_context263 @click.option("-o", "--option", default=1)264 def cli(ctx, option):265 assert ctx.get_parameter_source("option") == click.ParameterSource.DEFAULT266+ ctx.exit(1)267 267- cli.main([], "test_parameter_source_default", standalone_mode=False) == 0268+ assert cli.main([], "test_parameter_source_default", standalone_mode=False) == 1269 270+def test_parameter_source_default_map():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.DEFAULT_MAP276+ ctx.exit(1)277 278+ assert cli.main([], "test_parameter_source_default", standalone_mode=False, default_map={ "option": 1}) == 1279+ 280+281 def test_parameter_source_commandline():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.COMMANDLINE287+ ctx.exit(1)288+ 289+ assert cli.main(["-o", "1"], "test_parameter_source_commandline", standalone_mode=False) == 1290+ assert cli.main(["--option", "1"], "test_parameter_source_default", standalone_mode=False) == 1291 277- cli.main(["-o", "1"], "test_parameter_source_commandline", standalone_mode=False) == 0278- cli.main(["--option", "1"], "test_parameter_source_default", standalone_mode=False) == 0292 280-293 def test_parameter_source_environment(runner):294 @click.command()295 @click.pass_context296 @click.option("-o", "--option", default=1)297 def cli(ctx, option):298 assert ctx.get_parameter_source("option") == click.ParameterSource.ENVIRONMENT299+ sys.exit(1)300+ 301+ assert runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"TEST_OPTION": "1"}, auto_envvar_prefix="TEST").exit_code == 1302 288- runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"CLI_OPTION": "1"},289- standalone_mode=False)290-303 def test_parameter_source_environment_variable_specified(runner):304 @click.command()305 @click.pass_context306 @click.option("-o", "--option", default=1, envvar="NAME")307 def cli(ctx, option):308 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- 309+ sys.exit(1)310+ 311+ assert runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"NAME": "1"}).exit_code == 1