diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2019-08-17 10:00:30 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2019-08-17 11:10:37 +0100 |
commit | d8d1429ec3daf8f2a67ffccbc2aa1d54eb3639c6 (patch) | |
tree | 3f13fd449bacd97432e18ba4fb0fb4b19ac0b5ed /alot/completion/argparse.py | |
parent | b5e612c69b625271424b626da24d941ddbe39391 (diff) |
refactor prompt completion
This just splits the file completion.py into several files, one for each
Completer subclass.
Diffstat (limited to 'alot/completion/argparse.py')
-rw-r--r-- | alot/completion/argparse.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/alot/completion/argparse.py b/alot/completion/argparse.py new file mode 100644 index 00000000..ac15b2c6 --- /dev/null +++ b/alot/completion/argparse.py @@ -0,0 +1,41 @@ +# Copyright (C) 2011-2019 Patrick Totzke <patricktotzke@gmail.com> +# This file is released under the GNU GPL, version 3 or a later revision. +# For further details see the COPYING file + +import argparse +from .completer import Completer +from ..utils import argparse as cargparse + + +class ArgparseOptionCompleter(Completer): + """completes option parameters for a given argparse.Parser""" + def __init__(self, parser): + """ + :param parser: the option parser we look up parameter and choices from + :type parser: `argparse.ArgumentParser` + """ + self.parser = parser + self.actions = parser._optionals._actions + + def complete(self, original, pos): + pref = original[:pos] + + res = [] + for act in self.actions: + if '=' in pref: + optionstring = pref[:pref.rfind('=') + 1] + # get choices + if 'choices' in act.__dict__: + # TODO: respect prefix + choices = act.choices or [] + res = res + [optionstring + a for a in choices] + else: + for optionstring in act.option_strings: + if optionstring.startswith(pref): + # append '=' for options that await a string value + if isinstance(act, (argparse._StoreAction, + cargparse.BooleanAction)): + optionstring += '=' + res.append(optionstring) + + return [(a, len(a)) for a in res] |