Disable invalid value for a setting #1284
-
I managed to create an autocompletable settable: env_settable = Settable('env', str, 'Your environment', self,
choices_provider=list_envs, onchange_cb=self._env_changed)
self.add_settable(env_settable)
def _env_changed(self, param_name, old, new):
if new not in list(config.get_config()['environments'].keys()):
self.env = old Almost there, but is there a way to correctly refuse to set an invalid setting? FTA> set env eee
env - was: 'dev'
now: 'eee'
FTA> set env
Name Value Description
==================================================================================================
env dev Your environment
FTA> |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To validate a
Here is
So in your case, instead of setting def validate_env(new_val: str) -> str:
valid_vals = list(config.get_config()['environments'].keys())
if new_val not in valid_vals:
raise ValueError(f"invalid choice: {new_val!r} (choose from {valid_vals})")
return new_val |
Beta Was this translation helpful? Give feedback.
To validate a
Settable's
value, useval_type
and notonchange_cb
.val_type
is a function which runs when setting the value. Whereasonchage_cb
, as you've already noticed , runs after theSettable
has been set to the new value.Here is
val_type's
docsctring.So in your case, instead of setting
val_type
tos…