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

Register completion for multiple commands #246

Merged
merged 1 commit into from
May 8, 2018
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
23 changes: 16 additions & 7 deletions argcomplete/shellintegration.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/usr/bin/env python

try:
from shlex import quote
except ImportError:
from pipes import quote

bashcode = r'''
# Run something, muting output or redirecting it to the debug stream
# depending on the value of _ARC_DEBUG.
Expand Down Expand Up @@ -31,18 +36,18 @@
compopt -o nospace
fi
}
complete %(complete_opts)s -F _python_argcomplete "%(executable)s"
complete %(complete_opts)s -F _python_argcomplete %(executables)s
'''

tcshcode = '''\
complete "%(executable)s" 'p@*@`python-argcomplete-tcsh "%(executable)s"`@'
complete "%(executable)s" 'p@*@`python-argcomplete-tcsh "%(executable)s"`@' ;
'''

def shellcode(executable, use_defaults=True, shell='bash', complete_arguments=None):
def shellcode(executables, use_defaults=True, shell='bash', complete_arguments=None):
'''
Provide the shell code required to register a python executable for use with the argcomplete module.

:param str executable: Executable to be completed (when invoked exactly with this name
:param str executables: Executables to be completed (when invoked exactly with this name
:param bool use_defaults: Whether to fallback to readline's default completion when no matches are generated.
:param str shell: Name of the shell to output code for (bash or tcsh)
:param complete_arguments: Arguments to call complete with
Expand All @@ -55,8 +60,12 @@ def shellcode(executable, use_defaults=True, shell='bash', complete_arguments=No
complete_options = " ".join(complete_arguments)

if shell == 'bash':
code = bashcode
quoted_executables = [quote(i) for i in executables]
executables_list = " ".join(quoted_executables)
code = bashcode % dict(complete_opts=complete_options, executables=executables_list)
else:
code = tcshcode
code = ""
for executable in executables:
code += tcshcode % dict(executable=executable)

return code % dict(complete_opts=complete_options, executable=executable)
return code
1 change: 1 addition & 0 deletions scripts/register-python-argcomplete
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ parser.add_argument(

parser.add_argument(
'executable',
nargs='+',
help='executable to completed (when invoked by exactly this name)')

if len(sys.argv) == 1:
Expand Down
8 changes: 4 additions & 4 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,17 +740,17 @@ def make_parser():

def test_shellcode_utility(self):
with NamedTemporaryFile() as fh:
sc = shellcode("prog", use_defaults=True, shell="bash", complete_arguments=None)
sc = shellcode(["prog"], use_defaults=True, shell="bash", complete_arguments=None)
fh.write(sc.encode())
fh.flush()
subprocess.check_call(['bash', '-n', fh.name])
with NamedTemporaryFile() as fh:
sc = shellcode("prog", use_defaults=False, shell="bash", complete_arguments=["-o", "nospace"])
sc = shellcode(["prog", "prog2"], use_defaults=False, shell="bash", complete_arguments=["-o", "nospace"])
fh.write(sc.encode())
fh.flush()
subprocess.check_call(['bash', '-n', fh.name])
sc = shellcode("prog", use_defaults=False, shell="tcsh", complete_arguments=["-o", "nospace"])
sc = shellcode("prog", use_defaults=False, shell="woosh", complete_arguments=["-o", "nospace"])
sc = shellcode(["prog"], use_defaults=False, shell="tcsh", complete_arguments=["-o", "nospace"])
sc = shellcode(["prog"], use_defaults=False, shell="woosh", complete_arguments=["-o", "nospace"])

class TestArgcompleteREPL(unittest.TestCase):
def setUp(self):
Expand Down