MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / Settable

Class Settable

cmd2/utils.py:67–161  ·  view source on GitHub ↗

Used to configure an attribute to be settable via the set command in the CLI.

Source from the content-addressed store, hash-verified

65
66
67class Settable:
68 """Used to configure an attribute to be settable via the set command in the CLI."""
69
70 def __init__(
71 self,
72 name: str,
73 val_type: type[Any] | Callable[[Any], Any],
74 description: str,
75 settable_object: object,
76 *,
77 settable_attrib_name: str | None = None,
78 onchange_cb: Callable[[str, Any, Any], Any] | None = None,
79 choices: Iterable[Any] | None = None,
80 choices_provider: UnboundChoicesProvider[CmdOrSetT] | None = None,
81 completer: UnboundCompleter[CmdOrSetT] | None = None,
82 ) -> None:
83 """Settable Initializer.
84
85 :param name: The user-facing name for this setting in the CLI.
86 :param val_type: A callable used to cast the string value from the CLI into its
87 proper type and validate it. This function should raise an
88 exception (like ValueError or TypeError) if the conversion or
89 validation fails, which will be caught and displayed to the user
90 by the set command. For example, setting this to int ensures the
91 input is a valid integer. Specifying bool automatically provides
92 completion for 'true' and 'false' and uses a built-in function
93 for conversion and validation.
94 :param description: A concise string that describes the purpose of this setting.
95 :param settable_object: The object that owns the attribute being made settable (e.g. self).
96 :param settable_attrib_name: The name of the attribute on the settable_object that
97 will be modified. This defaults to the value of the name
98 parameter if not specified.
99 :param onchange_cb: An optional function or method to call when the value of this
100 setting is altered by the set command. The callback is invoked
101 only if the new value is different from the old one.
102
103 It receives three arguments:
104 param_name: str - name of the parameter
105 old_value: Any - the parameter's old value
106 new_value: Any - the parameter's new value
107
108 The following optional settings provide completion for a parameter's values.
109 They correspond to the same settings in argparse-based completion. A maximum
110 of one of these should be provided.
111
112 :param choices: iterable of accepted values
113 :param choices_provider: function that provides choices for this argument
114 :param completer: completion function that provides choices for this argument
115 """
116 if val_type is bool:
117 from .completion import Choices
118
119 def get_bool_choices(_cmd2_self: CmdOrSet) -> Choices:
120 """Tab complete lowercase boolean values."""
121 return Choices.from_values(["true", "false"])
122
123 val_type = to_bool
124 choices_provider = get_bool_choices

Callers 5

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
build_settablesMethod · 0.85

Calls

no outgoing calls

Tested by 4

__init__Method · 0.72
__init__Method · 0.72
__init__Method · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…