diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2017-11-05 16:59:43 +0000 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2017-11-05 17:38:01 +0000 |
commit | cceec453ae54b19606a7e4ad743ff13f750e57f8 (patch) | |
tree | 969440839551708f9321060484ae0ee9fe6755a2 | |
parent | e696ae1f2ee608bc6e3188e1b5c802726067c35f (diff) |
sanitize parameter for thread mode command 'indent'
this introduces a new argparse validation check that makes sure a
parameter is '+', '-', or an integer, and uses this check for the
'indent' thread mode command.
-rw-r--r-- | alot/commands/thread.py | 7 | ||||
-rw-r--r-- | alot/utils/argparse.py | 10 |
2 files changed, 14 insertions, 3 deletions
diff --git a/alot/commands/thread.py b/alot/commands/thread.py index fc6a4273..33230197 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -537,7 +537,8 @@ class EditNewCommand(Command): 'nargs': '*'})]) @registerCommand( MODE, 'indent', help='change message/reply indentation', - arguments=[(['indent'], {})]) + arguments=[(['indent'], {'action': cargparse.ValidatedStoreAction, + 'validator': cargparse.is_int_or_pm})]) class ChangeDisplaymodeCommand(Command): """fold or unfold messages""" @@ -576,8 +577,8 @@ class ChangeDisplaymodeCommand(Command): elif self.indent == '-': tbuffer._indent_width -= 1 else: - # TODO: I'm dangerous - tbuffer._indent_width = int(self.indent) + # argparse validation guarantees that self.indent is an integer + tbuffer._indent_width = self.indent tbuffer.rebuild() tbuffer.collapse_all() ui.update() diff --git a/alot/utils/argparse.py b/alot/utils/argparse.py index adacdd6e..ff19030c 100644 --- a/alot/utils/argparse.py +++ b/alot/utils/argparse.py @@ -97,6 +97,16 @@ def require_dir(path): raise ValidationFailed('{} is not a valid directory.'.format(path)) +def is_int_or_pm(value): + """Validator to assert that value is '+', '-', or an integer""" + if value not in ['+', '-']: + try: + value = int(value) + except ValueError: + raise ValidationFailed('value must be an integer or "+" or "-".') + return value + + class BooleanAction(argparse.Action): """Argparse action that can be used to store boolean values.""" def __init__(self, *args, **kwargs): |