Skip to content

Commit

Permalink
Improve auto completion for an arbitrary name using a given external …
Browse files Browse the repository at this point in the history
…script (#296)

* Register auto completion for an arbitrary name using a given external script

* Add a space to indent with a multiple of four

* Add documentation of auto completion for an arbitrary name using a given external script

* Generate dynamically name of completion function in bash if necessary
  • Loading branch information
msc42 authored Apr 8, 2020
1 parent e43a7b6 commit 5093b1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,17 @@ or create new completion file, e.g::

register-python-argcomplete --shell fish ~/.config/fish/completions/my-awesome-script.fish

External argcomplete script
---------------------------
To register an argcomplete script for an arbitrary name, the ``--external-argcomplete-script`` argument of the ``register-python-argcomplete`` script can be used::

eval "$(register-python-argcomplete --external-argcomplete-script /path/to/script arbitrary-name)"

This allows, for example, to use the auto completion functionality of argcomplete for an application not written in Python.
The command line interface of this program must be additionally implemented in a Python script with argparse and argcomplete and whenever the application is called the registered external argcomplete script is used for auto completion.

This option can also be used in combination with the other supported shells.

Python Support
--------------
Argcomplete requires Python 2.7 or 3.5+.
Expand Down
14 changes: 9 additions & 5 deletions argcomplete/shell_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
fi
}
_python_argcomplete() {
_python_argcomplete%(function_suffix)s() {
local IFS=$'\013'
local SUPPRESS_SPACE=0
if compopt +o nospace 2> /dev/null; then
Expand All @@ -36,7 +36,7 @@
compopt -o nospace
fi
}
complete %(complete_opts)s -F _python_argcomplete %(executables)s
complete %(complete_opts)s -F _python_argcomplete%(function_suffix)s %(executables)s
'''

tcshcode = '''\
Expand Down Expand Up @@ -86,10 +86,14 @@ def shellcode(executables, use_defaults=True, shell='bash', complete_arguments=N
if shell == 'bash':
quoted_executables = [quote(i) for i in executables]
executables_list = " ".join(quoted_executables)
if not argcomplete_script:
argcomplete_script = '$1'
script = argcomplete_script
if script:
function_suffix = '_' + script
else:
script = '$1'
function_suffix = ''
code = bashcode % dict(complete_opts=complete_options, executables=executables_list,
argcomplete_script=argcomplete_script)
argcomplete_script=script, function_suffix=function_suffix)
else:
code = ""
for executable in executables:
Expand Down

0 comments on commit 5093b1d

Please sign in to comment.