Skip to content
Myr

document values passed to types

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

authored by David Lord · committed by David Lord · parent ba9bb3c · Browse files · 2 files+10 added−7 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+10 added−7 removed

  • other:+10 added−7 removed

2 files+10 added−7 removed

modified:docs/parameters.rst+8 added-7 removed
116 unmodified lines
117 class BasedIntParamType(click.ParamType):118 name = "integer"119120 def convert(self, value, param, ctx):121 if isinstance(value, int):122 return value123124 try:125 if value[:2].lower() == "0x":126 return int(value[2:], 16)127 elif value[:1] == "0":128 return int(value, 8)129 return int(value, 10)127 except TypeError:128 self.fail(129 "expected string for int() conversion, got "130 f"{value!r} of type {type(value).__name__}",131 param,132 ctx,133 )130 except ValueError:131 self.fail(f"{value!r} is not a valid integer", param, ctx)132133 BASED_INT = BasedIntParamType()
1 unmodified line
135The :attr:`~ParamType.name` attribute is optional and is used for136documentation. Call :meth:`~ParamType.fail` if conversion fails. The137``param`` and ``ctx`` arguments may be ``None`` in some cases such as138prompts.139140Values from user input or the command line will be strings, but default141values and Python arguments may already be the correct type. The custom142type should check at the top if the value is already valid and pass it143through to support those cases.
modified:CHANGES.rst+2 added
16 unmodified lines
17- Arguments with ``nargs=-1`` only use env var value if no command18 line values are given. :issue:`1903`19- Flag options guess their type from ``flag_value`` if given, like20 regular options do from ``default``. :issue:`1886`21- Added documentation that custom parameter types may be passed22 already valid values in addition to strings. :issue:`1898`232425Version 8.0.026-------------
856 unmodified lines