Skip to content
Myr

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

4 files+59 added−17 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+9 added-5 removed
130 unmodified lines
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.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"143144 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 lines
1447 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 value14551456 def type_cast_value(self, ctx, value):1457 """Given a value this runs it properly through the type system.
38 unmodified lines
1496 value = self.process_value(ctx, value)14971498 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)15021503 if self.required and self.value_is_missing(value):1504 raise MissingParameter(ctx=ctx, param=self)1505
411 unmodified lines
modified:docs/advanced.rst+27 added
375 unmodified lines
376do. However if you do use this for threading you need to be very careful377as the vast majority of the context is not thread safe! You are only378allowed to read from the context, but not to perform any modifications on379it.380381382Detecting the Source of a Parameter383-----------------------------------384385In some situations it's helpful to understand whether or not an option386or parameter came from the command line, the environment, the default387value, or the default_map. The :meth:`Context.get_parameter_source`388method can be used to find this out.389390.. click:example::391392 @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))398399.. click:run::400401 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 lines
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 ctx.exit(1)267267 cli.main([], "test_parameter_source_default", standalone_mode=False) == 0268 assert cli.main([], "test_parameter_source_default", standalone_mode=False) == 1269270def 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)277278 assert cli.main([], "test_parameter_source_default", standalone_mode=False, default_map={ "option": 1}) == 1279 280281def 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) == 1291277 cli.main(["-o", "1"], "test_parameter_source_commandline", standalone_mode=False) == 0278 cli.main(["--option", "1"], "test_parameter_source_default", standalone_mode=False) == 0292280293def 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 == 1302288 runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"CLI_OPTION": "1"},289 standalone_mode=False)290303def 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.ENVIRONMENT297298 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