path_complete doesn't work with CommandSet
See original GitHub issueVersion 1.3.0.
When using path_complete for a CommandSet argparse argument the following exception occurs:
Traceback (most recent call last):
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1707, in complete
self._completion_for_command(text, line, begidx, endidx, shortcut_to_restore)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1592, in _completion_for_command
self.completion_matches = self._redirect_complete(text, line, begidx, endidx, compfunc)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1385, in _redirect_complete
return compfunc(text, line, begidx, endidx)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/cmd2.py", line 1757, in _complete_argparse_command
return completer.complete_command(tokens_to_parse, text, line, begidx, endidx, cmd_set=cmd_set)
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/argparse_completer.py", line 429, in complete_command
completion_results = self._complete_for_arg(pos_arg_state.action, text, line,
File "/home/martin/.virtualenvs/pyos/lib/python3.8/site-packages/cmd2/argparse_completer.py", line 596, in _complete_for_arg
results = arg_choices.to_call(*args, **kwargs)
TypeError: path_complete() takes 5 positional arguments but 6 were given
This happens because the CommandSet is being passed in as the second positional argument which isn’t expected by the completer.
A big benefit of completers is that they can be used by disparate commands (whether they be free functions later bound to a Cmd class, members of a Cmd class or members of a CommandSet). Having a different interface detracts from this as, now, something like a shim is required to make it work without redefining a new path_complete.
I think it’s worth considering ways to have a common interface, not only for the complete functions but for do and help. A couple of suggestions come to mind:
def path_complete(self, text: str, line: str, begidx: int, endidx: int, context)
where context is a dict or namedtuple that stores a reference to the Cmd instance and (optionally) the CommandSet.
Both Cmd and CommandSet conform to a common interface that specifies the subset of things that can be done by both (most methods/attributes from Cmd), and CommandSet just forwards these to its _cmd member.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (7 by maintainers)

Top Related StackOverflow Question
I think since that’s causing some confusion, I’m going to rename the directory to
tests_isolatedso it’ll be grouped together and be more noticeable.This was fixed in #968