Skip to content
Myr

Changing documentation in ParameterSource to indicate 'parameter' instead of 'option'. Removed emphasis of the word source in Context.get_parameter_source. Changed ParameterSource.VALUE to a set instead of list. Fixed bugs in ParameterSource.validate and added unit test for it. Changed Context.get_parameter_source to return a KeyError when an invalid parameter name is specified.

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 64bbbe4 · Browse files · 2 files+12 added−6 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+12 added−6 removed

  • tests:+7 added−1 removed
  • other:+5 added−5 removed

2 files+12 added−6 removed

modified:click/core.py+5 added-5 removed
128 unmodified lines
129130 return sorted(declaration_order, key=sort_key)131132class ParameterSource(object):133 """This is an enum that indicates the source of a command line option.133 """This is an enum that indicates the source of a command line parameter.134135 The enum has one of the following values: COMMANDLINE,136 ENVIRONMENT, DEFAULT, DEFAULT_MAP. The DEFAULT indicates that the137 default value in the decorator was used. This class should be
4 unmodified lines
142 ENVIRONMENT = "ENVIRONMENT"143 DEFAULT = "DEFAULT"144 DEFAULT_MAP = "DEFAULT_MAP"145 146 VALUES = [COMMANDLINE, ENVIRONMENT, DEFAULT, DEFAULT_MAP]146 VALUES = {COMMANDLINE, ENVIRONMENT, DEFAULT, DEFAULT_MAP}147 148 @classmethod149 def validate(clz, value):150 """Validate that the specified value is a valid enum.
5 unmodified lines
156 :param value: the string value to verify157 """158 if value not in clz.VALUES:159 raise ValueError("Invalid ParameterSource value: '{}'. Valid "160 "values are: {}".format(",".join(VALUES)))160 "values are: {}".format(value, ",".join(clz.VALUES)))161162163class Context(object):164 """The context is a special internal object that holds state relevant
451 unmodified lines
616 ParameterSource.validate(source)617 self._parameter_sources[name] = source618619 def get_parameter_source(self, name):620 """Get the `source` of a parameter.620 """Get the source of a parameter.621622 This indicates the location from which the value of the623 parameter was obtained. This can be useful for determining624 when a user specified an option on the command line that is
4 unmodified lines
629 :param name: the name of the command line parameter630 :returns: the source631 :rtype: ParameterSource632 """633 return self._parameter_sources.get(name)633 return self._parameter_sources[name]634635636class BaseCommand(object):637 """The base command implements the minimal API contract of commands.
1291 unmodified lines
modified:tests/test_context.py+7 added-1 removed
1# -*- coding: utf-8 -*-2import click3import pytest445def test_ensure_context_objects(runner):6 class Foo(object):7 def __init__(self):8 self.title = 'default'
290 unmodified lines
299 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 == 1302303304def test_parameter_source_environment_variable_specified(runner):305 @click.command()306 @click.pass_context307 @click.option("-o", "--option", default=1, envvar="NAME")
1 unmodified line
309 assert ctx.get_parameter_source("option") == click.ParameterSource.ENVIRONMENT310 sys.exit(1)311 312 assert runner.invoke(cli, [], prog_name="test_parameter_source_environment", env={"NAME": "1"}).exit_code == 1313314315def test_validate_parameter_source():316 with pytest.raises(ValueError):317 click.ParameterSource.validate("DEFAUL")