Numeric Module
Custom numeric input handlers for Parameters
- author:
Doug Skrypa
- class cli_command_parser.inputs.numeric.NumericInput(fix_default: bool | Any = True)[source]
-
- is_valid_type(value: str) bool [source]
Called during parsing when
ParamAction.would_accept()
is called to determine if the value would be accepted later for processing / conversion when called.- Parameters:
value¶ – The parsed argument to validate
- Returns:
True if this input would accept it for processing later (where it may still be rejected), False if it should be rejected before attempting to process / convert / store it.
- class cli_command_parser.inputs.numeric.Range(range: range | int | Sequence[int], snap: bool | Any = False, type: Callable[[str | float | int], NT] = None, fix_default: bool | Any = True)[source]
Bases:
NumericInput
[NT
]A range of integers that uses the builtin
range
. If a range object is passed to aParameter
as thetype=
value, it will automatically be wrapped by this class.- Parameters:
snap¶ – If True and a provided value is outside the allowed range, snap to the nearest bound. The min or max of the provided range (not necessarily the start/stop values) will be used, depending on which one the provided value is closer to.
type¶ – Callable that returns a numeric type, to be used on parsed values before validating whether they are in the allowed range. Defaults to
int
.fix_default¶ – Whether default values should be normalized using
fix_default()
.
- class cli_command_parser.inputs.numeric.NumRange(type: Callable[[str | float | int], NT] = None, snap: bool | Any = False, *, min: NT | None = None, max: NT | None = None, include_min: bool | Any = True, include_max: bool | Any = False, fix_default: bool | Any = True)[source]
Bases:
RangeMixin
,NumericInput
[NT
]A range of integers or floats, optionally only bounded on one side.
By default, the min and max behave like the builtin
range
- the min is inclusive, and the max is exclusive.- Parameters:
type¶ – The type for values, or any callable that returns an int/float
snap¶ – If True and a provided value is outside the allowed range, snap to the nearest bound. Respects inclusivity/exclusivity of the bound. Not supported for floats since there is not an obviously correct behavior for handling them in this context.
min¶ – The minimum allowed value, or None to have no lower bound
max¶ – The maximum allowed value, or None to have no upper bound
include_min¶ – Whether the minimum is inclusive (default: True)
include_max¶ – Whether the maximum is inclusive (default: False)
fix_default¶ – Whether default values should be normalized using
fix_default()
.
- type: NumType
- min: Number
- max: Number
- handle_invalid(bound: NT | None, inclusive: bool, snap_dir: int) NT | None [source]
Handle calculating / returning a snap value or raise an exception if snapping to the bound is not allowed.
May be overridden in a subclass to support snapping for float values or other behavior.
- Parameters:
- Returns:
The snap value if
snap
is True, otherwise aValueError
is raised