Skip to content

Commit

Permalink
add SuppressCompleter to skip completion for specific arguments while…
Browse files Browse the repository at this point in the history
… allowing help text (#224)
  • Loading branch information
dirk-thomas authored and kislyuk committed Aug 23, 2017
1 parent e72c5fb commit aee9b74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
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

0 comments on commit aee9b74

Please sign in to comment.