Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SuppressCompleter to skip completion for specific arguments while allowing help text #224

Merged
merged 4 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions argcomplete/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os, sys, argparse, contextlib
from . import completers, my_shlex as shlex
from .compat import USING_PYTHON2, str, sys_encoding, ensure_str, ensure_bytes
from .completers import FilesCompleter
from .completers import FilesCompleter, SuppressCompleter
from .my_argparse import IntrospectiveArgumentParser, action_is_satisfied, action_is_open, action_is_greedy

_DEBUG = "_ARC_DEBUG" in os.environ
Expand Down Expand Up @@ -346,8 +346,12 @@ def _get_option_completions(self, parser, cword_prefix):

option_completions = []
for action in parser._actions:
if action.help == argparse.SUPPRESS and not self.print_suppressed:
continue
if not self.print_suppressed:
completer = getattr(action, "completer", None)
if isinstance(completer, SuppressCompleter) and completer.suppress():
continue
if action.help == argparse.SUPPRESS:
continue
if not self._action_allowed(action, parser):
continue
if not isinstance(action, argparse._SubParsersAction):
Expand Down
14 changes: 14 additions & 0 deletions argcomplete/completers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,17 @@ def __call__(self, prefix, **kwargs):
class DirectoriesCompleter(_FilteredFilesCompleter):
def __init__(self):
_FilteredFilesCompleter.__init__(self, predicate=os.path.isdir)

class SuppressCompleter(object):
"""
A completer used to suppress the completion of specific arguments
"""

def __init__(self):
pass

def suppress(self):
"""
Decide if the completion should be suppressed
"""
return True
26 changes: 25 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
ExclusiveCompletionFinder,
_check_module
)
from argcomplete.completers import FilesCompleter, DirectoriesCompleter
from argcomplete.completers import FilesCompleter, DirectoriesCompleter, SuppressCompleter
from argcomplete.compat import USING_PYTHON2, str, sys_encoding, ensure_str, ensure_bytes

IFS = "\013"
Expand Down Expand Up @@ -153,6 +153,30 @@ def make_parser():
for cmd, output in expected_outputs:
self.assertEqual(set(self.run_completer(make_parser(), cmd, print_suppressed=True)), set(output))

def test_suppress_completer(self):
def make_parser():
parser = ArgumentParser()
parser.add_argument("--foo")
arg = parser.add_argument("--bar")
arg.completer = SuppressCompleter()
return parser

expected_outputs = (
("prog ", ["--foo", "-h", "--help"]),
("prog --b", [""])
)

for cmd, output in expected_outputs:
self.assertEqual(set(self.run_completer(make_parser(), cmd)), set(output))

expected_outputs = (
("prog ", ["--foo", "--bar", "-h", "--help"]),
("prog --b", ["--bar "])
)

for cmd, output in expected_outputs:
self.assertEqual(set(self.run_completer(make_parser(), cmd, print_suppressed=True)), set(output))

def test_action_activation(self):
def make_parser():
parser = ArgumentParser()
Expand Down