METADATA 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. Metadata-Version: 2.1
  2. Name: argcomplete
  3. Version: 1.12.3
  4. Summary: Bash tab completion for argparse
  5. Home-page: https://github.com/kislyuk/argcomplete
  6. Author: Andrey Kislyuk
  7. Author-email: kislyuk@gmail.com
  8. License: Apache Software License
  9. Project-URL: Documentation, https://kislyuk.github.io/argcomplete
  10. Project-URL: Source Code, https://github.com/kislyuk/argcomplete
  11. Project-URL: Issue Tracker, https://github.com/kislyuk/argcomplete/issues
  12. Project-URL: Change Log, https://github.com/kislyuk/argcomplete/blob/master/Changes.rst
  13. Platform: MacOS X
  14. Platform: Posix
  15. Classifier: Environment :: Console
  16. Classifier: Intended Audience :: Developers
  17. Classifier: License :: OSI Approved :: Apache Software License
  18. Classifier: Operating System :: MacOS :: MacOS X
  19. Classifier: Operating System :: POSIX
  20. Classifier: Programming Language :: Python
  21. Classifier: Programming Language :: Python :: 2
  22. Classifier: Programming Language :: Python :: 2.7
  23. Classifier: Programming Language :: Python :: 3
  24. Classifier: Programming Language :: Python :: 3.5
  25. Classifier: Programming Language :: Python :: 3.6
  26. Classifier: Programming Language :: Python :: 3.7
  27. Classifier: Programming Language :: Python :: 3.8
  28. Classifier: Programming Language :: Python :: Implementation :: CPython
  29. Classifier: Programming Language :: Python :: Implementation :: PyPy
  30. Classifier: Development Status :: 5 - Production/Stable
  31. Classifier: Topic :: Software Development
  32. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  33. Classifier: Topic :: System :: Shells
  34. Classifier: Topic :: Terminals
  35. Requires-Dist: importlib-metadata (<5,>=0.23) ; python_version == "2.7"
  36. Requires-Dist: importlib-metadata (<5,>=0.23) ; python_version == "3.5"
  37. Requires-Dist: importlib-metadata (<5,>=0.23) ; python_version == "3.6"
  38. Requires-Dist: importlib-metadata (<5,>=0.23) ; python_version == "3.7"
  39. Provides-Extra: test
  40. Requires-Dist: coverage ; extra == 'test'
  41. Requires-Dist: flake8 ; extra == 'test'
  42. Requires-Dist: pexpect ; extra == 'test'
  43. Requires-Dist: wheel ; extra == 'test'
  44. argcomplete - Bash tab completion for argparse
  45. ==============================================
  46. *Tab complete all the things!*
  47. Argcomplete provides easy, extensible command line tab completion of arguments for your Python script.
  48. It makes two assumptions:
  49. * You're using bash as your shell (limited support for zsh, fish, and tcsh is available)
  50. * You're using `argparse <http://docs.python.org/3/library/argparse.html>`_ to manage your command line arguments/options
  51. Argcomplete is particularly useful if your program has lots of options or subparsers, and if your program can
  52. dynamically suggest completions for your argument/option values (for example, if the user is browsing resources over
  53. the network).
  54. Installation
  55. ------------
  56. ::
  57. pip3 install argcomplete
  58. activate-global-python-argcomplete
  59. See `Activating global completion`_ below for details about the second step (or if it reports an error).
  60. Refresh your bash environment (start a new shell or ``source /etc/profile``).
  61. Synopsis
  62. --------
  63. Python code (e.g. ``my-awesome-script``):
  64. .. code-block:: python
  65. #!/usr/bin/env python
  66. # PYTHON_ARGCOMPLETE_OK
  67. import argcomplete, argparse
  68. parser = argparse.ArgumentParser()
  69. ...
  70. argcomplete.autocomplete(parser)
  71. args = parser.parse_args()
  72. ...
  73. Shellcode (only necessary if global completion is not activated - see `Global completion`_ below), to be put in e.g. ``.bashrc``::
  74. eval "$(register-python-argcomplete my-awesome-script)"
  75. argcomplete.autocomplete(*parser*)
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. This method is the entry point to the module. It must be called **after** ArgumentParser construction is complete, but
  78. **before** the ``ArgumentParser.parse_args()`` method is called. The method looks for an environment variable that the
  79. completion hook shellcode sets, and if it's there, collects completions, prints them to the output stream (fd 8 by
  80. default), and exits. Otherwise, it returns to the caller immediately.
  81. .. admonition:: Side effects
  82. Argcomplete gets completions by running your program. It intercepts the execution flow at the moment
  83. ``argcomplete.autocomplete()`` is called. After sending completions, it exits using ``exit_method`` (``os._exit``
  84. by default). This means if your program has any side effects that happen before ``argcomplete`` is called, those
  85. side effects will happen every time the user presses ``<TAB>`` (although anything your program prints to stdout or
  86. stderr will be suppressed). For this reason it's best to construct the argument parser and call
  87. ``argcomplete.autocomplete()`` as early as possible in your execution flow.
  88. .. admonition:: Performance
  89. If the program takes a long time to get to the point where ``argcomplete.autocomplete()`` is called, the tab completion
  90. process will feel sluggish, and the user may lose confidence in it. So it's also important to minimize the startup time
  91. of the program up to that point (for example, by deferring initialization or importing of large modules until after
  92. parsing options).
  93. Specifying completers
  94. ---------------------
  95. You can specify custom completion functions for your options and arguments. Two styles are supported: callable and
  96. readline-style. Callable completers are simpler. They are called with the following keyword arguments:
  97. * ``prefix``: The prefix text of the last word before the cursor on the command line.
  98. For dynamic completers, this can be used to reduce the work required to generate possible completions.
  99. * ``action``: The ``argparse.Action`` instance that this completer was called for.
  100. * ``parser``: The ``argparse.ArgumentParser`` instance that the action was taken by.
  101. * ``parsed_args``: The result of argument parsing so far (the ``argparse.Namespace`` args object normally returned by
  102. ``ArgumentParser.parse_args()``).
  103. Completers should return their completions as a list of strings. An example completer for names of environment
  104. variables might look like this:
  105. .. code-block:: python
  106. def EnvironCompleter(**kwargs):
  107. return os.environ
  108. To specify a completer for an argument or option, set the ``completer`` attribute of its associated action. An easy
  109. way to do this at definition time is:
  110. .. code-block:: python
  111. from argcomplete.completers import EnvironCompleter
  112. parser = argparse.ArgumentParser()
  113. parser.add_argument("--env-var1").completer = EnvironCompleter
  114. parser.add_argument("--env-var2").completer = EnvironCompleter
  115. argcomplete.autocomplete(parser)
  116. If you specify the ``choices`` keyword for an argparse option or argument (and don't specify a completer), it will be
  117. used for completions.
  118. A completer that is initialized with a set of all possible choices of values for its action might look like this:
  119. .. code-block:: python
  120. class ChoicesCompleter(object):
  121. def __init__(self, choices):
  122. self.choices = choices
  123. def __call__(self, **kwargs):
  124. return self.choices
  125. The following two ways to specify a static set of choices are equivalent for completion purposes:
  126. .. code-block:: python
  127. from argcomplete.completers import ChoicesCompleter
  128. parser.add_argument("--protocol", choices=('http', 'https', 'ssh', 'rsync', 'wss'))
  129. parser.add_argument("--proto").completer=ChoicesCompleter(('http', 'https', 'ssh', 'rsync', 'wss'))
  130. Note that if you use the ``choices=<completions>`` option, argparse will show
  131. all these choices in the ``--help`` output by default. To prevent this, set
  132. ``metavar`` (like ``parser.add_argument("--protocol", metavar="PROTOCOL",
  133. choices=('http', 'https', 'ssh', 'rsync', 'wss'))``).
  134. The following `script <https://raw.github.com/kislyuk/argcomplete/master/docs/examples/describe_github_user.py>`_ uses
  135. ``parsed_args`` and `Requests <http://python-requests.org/>`_ to query GitHub for publicly known members of an
  136. organization and complete their names, then prints the member description:
  137. .. code-block:: python
  138. #!/usr/bin/env python
  139. # PYTHON_ARGCOMPLETE_OK
  140. import argcomplete, argparse, requests, pprint
  141. def github_org_members(prefix, parsed_args, **kwargs):
  142. resource = "https://api.github.com/orgs/{org}/members".format(org=parsed_args.organization)
  143. return (member['login'] for member in requests.get(resource).json() if member['login'].startswith(prefix))
  144. parser = argparse.ArgumentParser()
  145. parser.add_argument("--organization", help="GitHub organization")
  146. parser.add_argument("--member", help="GitHub member").completer = github_org_members
  147. argcomplete.autocomplete(parser)
  148. args = parser.parse_args()
  149. pprint.pprint(requests.get("https://api.github.com/users/{m}".format(m=args.member)).json())
  150. `Try it <https://raw.github.com/kislyuk/argcomplete/master/docs/examples/describe_github_user.py>`_ like this::
  151. ./describe_github_user.py --organization heroku --member <TAB>
  152. If you have a useful completer to add to the `completer library
  153. <https://github.com/kislyuk/argcomplete/blob/master/argcomplete/completers.py>`_, send a pull request!
  154. Readline-style completers
  155. ~~~~~~~~~~~~~~~~~~~~~~~~~
  156. The readline_ module defines a completer protocol in rlcompleter_. Readline-style completers are also supported by
  157. argcomplete, so you can use the same completer object both in an interactive readline-powered shell and on the bash
  158. command line. For example, you can use the readline-style completer provided by IPython_ to get introspective
  159. completions like you would get in the IPython shell:
  160. .. _readline: http://docs.python.org/3/library/readline.html
  161. .. _rlcompleter: http://docs.python.org/3/library/rlcompleter.html#completer-objects
  162. .. _IPython: http://ipython.org/
  163. .. code-block:: python
  164. import IPython
  165. parser.add_argument("--python-name").completer = IPython.core.completer.Completer()
  166. ``argcomplete.CompletionFinder.rl_complete`` can also be used to plug in an argparse parser as a readline completer.
  167. Printing warnings in completers
  168. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. Normal stdout/stderr output is suspended when argcomplete runs. Sometimes, though, when the user presses ``<TAB>``, it's
  170. appropriate to print information about why completions generation failed. To do this, use ``warn``:
  171. .. code-block:: python
  172. from argcomplete import warn
  173. def AwesomeWebServiceCompleter(prefix, **kwargs):
  174. if login_failed:
  175. warn("Please log in to Awesome Web Service to use autocompletion")
  176. return completions
  177. Using a custom completion validator
  178. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  179. By default, argcomplete validates your completions by checking if they start with the prefix given to the completer. You
  180. can override this validation check by supplying the ``validator`` keyword to ``argcomplete.autocomplete()``:
  181. .. code-block:: python
  182. def my_validator(current_input, keyword_to_check_against):
  183. # Pass through ALL options even if they don't all start with 'current_input'
  184. return True
  185. argcomplete.autocomplete(parser, validator=my_validator)
  186. Global completion
  187. -----------------
  188. In global completion mode, you don't have to register each argcomplete-capable executable separately. Instead, bash
  189. will look for the string **PYTHON_ARGCOMPLETE_OK** in the first 1024 bytes of any executable that it's running
  190. completion for, and if it's found, follow the rest of the argcomplete protocol as described above.
  191. Additionally, completion is activated for scripts run as ``python <script>`` and ``python -m <module>``.
  192. This also works for alternate Python versions (e.g. ``python3`` and ``pypy``), as long as that version of Python has
  193. argcomplete installed.
  194. .. admonition:: Bash version compatibility
  195. Global completion requires bash support for ``complete -D``, which was introduced in bash 4.2. On OS X or older Linux
  196. systems, you will need to update bash to use this feature. Check the version of the running copy of bash with
  197. ``echo $BASH_VERSION``. On OS X, install bash via `Homebrew <http://brew.sh/>`_ (``brew install bash``), add
  198. ``/usr/local/bin/bash`` to ``/etc/shells``, and run ``chsh`` to change your shell.
  199. Global completion is not currently compatible with zsh.
  200. .. note:: If you use setuptools/distribute ``scripts`` or ``entry_points`` directives to package your module,
  201. argcomplete will follow the wrapper scripts to their destination and look for ``PYTHON_ARGCOMPLETE_OK`` in the
  202. destination code.
  203. If you choose not to use global completion, or ship a bash completion module that depends on argcomplete, you must
  204. register your script explicitly using ``eval "$(register-python-argcomplete my-awesome-script)"``. Standard bash
  205. completion registration roules apply: namely, the script name is passed directly to ``complete``, meaning it is only tab
  206. completed when invoked exactly as it was registered. In the above example, ``my-awesome-script`` must be on the path,
  207. and the user must be attempting to complete it by that name. The above line alone would **not** allow you to complete
  208. ``./my-awesome-script``, or ``/path/to/my-awesome-script``.
  209. Activating global completion
  210. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  211. The script ``activate-global-python-argcomplete`` will try to install the file
  212. ``bash_completion.d/python-argcomplete`` (`see on GitHub`_) into an appropriate location on your system
  213. (``/etc/bash_completion.d/`` or ``~/.bash_completion.d/``). If it
  214. fails, but you know the correct location of your bash completion scripts directory, you can specify it with ``--dest``::
  215. activate-global-python-argcomplete --dest=/path/to/bash_completion.d
  216. Otherwise, you can redirect its shellcode output into a file::
  217. activate-global-python-argcomplete --dest=- > file
  218. The file's contents should then be sourced in e.g. ``~/.bashrc``.
  219. .. _`see on GitHub`: https://github.com/kislyuk/argcomplete/blob/master/argcomplete/bash_completion.d/python-argcomplete
  220. Zsh Support
  221. ------------
  222. To activate completions for zsh you need to have ``bashcompinit`` enabled in zsh::
  223. autoload -U bashcompinit
  224. bashcompinit
  225. Afterwards you can enable completion for your scripts with ``register-python-argcomplete``::
  226. eval "$(register-python-argcomplete my-awesome-script)"
  227. Tcsh Support
  228. ------------
  229. To activate completions for tcsh use::
  230. eval `register-python-argcomplete --shell tcsh my-awesome-script`
  231. The ``python-argcomplete-tcsh`` script provides completions for tcsh.
  232. The following is an example of the tcsh completion syntax for
  233. ``my-awesome-script`` emitted by ``register-python-argcomplete``::
  234. complete my-awesome-script 'p@*@`python-argcomplete-tcsh my-awesome-script`@'
  235. Fish Support
  236. ------------
  237. To activate completions for fish use::
  238. register-python-argcomplete --shell fish my-awesome-script | source
  239. or create new completion file, e.g::
  240. register-python-argcomplete --shell fish my-awesome-script > ~/.config/fish/completions/my-awesome-script.fish
  241. Completion Description For Fish
  242. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  243. By default help string is added as completion description.
  244. .. image:: docs/fish_help_string.png
  245. You can disable this feature by removing ``_ARGCOMPLETE_DFS`` variable, e.g::
  246. register-python-argcomplete --shell fish my-awesome-script | grep -v _ARGCOMPLETE_DFS | .
  247. Git Bash Support
  248. ----------------
  249. Due to limitations of file descriptor inheritance on Windows,
  250. Git Bash not supported out of the box. You can opt in to using
  251. temporary files instead of file descriptors for for IPC
  252. by setting the environment variable ``ARGCOMPLETE_USE_TEMPFILES``,
  253. e.g. by adding ``export ARGCOMPLETE_USE_TEMPFILES=1`` to ``~/.bashrc``.
  254. For full support, consider using Bash with the
  255. Windows Subsystem for Linux (WSL).
  256. External argcomplete script
  257. ---------------------------
  258. To register an argcomplete script for an arbitrary name, the ``--external-argcomplete-script`` argument of the ``register-python-argcomplete`` script can be used::
  259. eval "$(register-python-argcomplete --external-argcomplete-script /path/to/script arbitrary-name)"
  260. This allows, for example, to use the auto completion functionality of argcomplete for an application not written in Python.
  261. 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.
  262. This option can also be used in combination with the other supported shells.
  263. Python Support
  264. --------------
  265. Argcomplete requires Python 2.7 or 3.5+.
  266. Common Problems
  267. ---------------
  268. If global completion is not completing your script, bash may have registered a
  269. default completion function::
  270. $ complete | grep my-awesome-script
  271. complete -F _minimal my-awesome-script
  272. You can fix this by restarting your shell, or by running
  273. ``complete -r my-awesome-script``.
  274. Debugging
  275. ---------
  276. Set the ``_ARC_DEBUG`` variable in your shell to enable verbose debug output every time argcomplete runs. This will
  277. disrupt the command line composition state of your terminal, but make it possible to see the internal state of the
  278. completer if it encounters problems.
  279. Acknowledgments
  280. ---------------
  281. Inspired and informed by the optcomplete_ module by Martin Blais.
  282. .. _optcomplete: http://pypi.python.org/pypi/optcomplete
  283. Links
  284. -----
  285. * `Project home page (GitHub) <https://github.com/kislyuk/argcomplete>`_
  286. * `Documentation <https://kislyuk.github.io/argcomplete/>`_
  287. * `Package distribution (PyPI) <https://pypi.python.org/pypi/argcomplete>`_
  288. * `Change log <https://github.com/kislyuk/argcomplete/blob/master/Changes.rst>`_
  289. * `xontrib-argcomplete <https://github.com/anki-code/xontrib-argcomplete>`_ - support argcomplete in `xonsh <https://github.com/xonsh/xonsh>`_ shell
  290. Bugs
  291. ~~~~
  292. Please report bugs, issues, feature requests, etc. on `GitHub <https://github.com/kislyuk/argcomplete/issues>`_.
  293. License
  294. -------
  295. Licensed under the terms of the `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_.
  296. .. image:: https://github.com/kislyuk/argcomplete/workflows/Python%20package/badge.svg
  297. :target: https://github.com/kislyuk/argcomplete/actions
  298. .. image:: https://codecov.io/github/kislyuk/argcomplete/coverage.svg?branch=master
  299. :target: https://codecov.io/github/kislyuk/argcomplete?branch=master
  300. .. image:: https://img.shields.io/pypi/v/argcomplete.svg
  301. :target: https://pypi.python.org/pypi/argcomplete
  302. .. image:: https://img.shields.io/pypi/l/argcomplete.svg
  303. :target: https://pypi.python.org/pypi/argcomplete