How to autocomplete one argument in argument parser with cmd2.
from cmd2 import Cmd, Cmd2ArgumentParser
import cmd2
numbers = ['0', '1', '2', '3', '4']
alphabet = ['a', 'b', 'c', 'd']
class Complete(Cmd):
parser = Cmd2ArgumentParser()
parser.add_argument("type", choices=['numbers', 'alphabet'])
parser.add_argument("value")
@cmd2.with_argparser(parser)
def do_list(self, args):
self.poutput(args.value)
if __name__ == "__main__":
app = Complete()
app.cmdloop()
With this code i can autocomplete the 'type' argument (with choices in add_argument). I want to autocomplete the 'value' argument depending on the 'type' argument. If value is 'numbers', I complete it with the numbers list. if value is 'alphabet', I complete it with the alphabet list.
Is there any way to properly implement this behaviour? or should I implement my own complete_list method?
Thanks,
I found a solution using the keyword
completer_method
in theCmd2ArgumentParser
. It's not yet documented https://github.com/python-cmd2/cmd2/issues/748.The complete solution