METADATA 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. Metadata-Version: 2.4
  2. Name: argcomplete
  3. Version: 3.6.2
  4. Summary: Bash tab completion for argparse
  5. Project-URL: Homepage, https://github.com/kislyuk/argcomplete
  6. Project-URL: Documentation, https://kislyuk.github.io/argcomplete
  7. Project-URL: Source Code, https://github.com/kislyuk/argcomplete
  8. Project-URL: Issue Tracker, https://github.com/kislyuk/argcomplete/issues
  9. Project-URL: Change Log, https://github.com/kislyuk/argcomplete/blob/develop/Changes.rst
  10. Author: Andrey Kislyuk
  11. Author-email: kislyuk@gmail.com
  12. Maintainer: Andrey Kislyuk
  13. Maintainer-email: kislyuk@gmail.com
  14. License: Apache Software License
  15. License-File: LICENSE.rst
  16. License-File: NOTICE
  17. Classifier: Development Status :: 5 - Production/Stable
  18. Classifier: Environment :: Console
  19. Classifier: Intended Audience :: Developers
  20. Classifier: License :: OSI Approved :: Apache Software License
  21. Classifier: Operating System :: MacOS :: MacOS X
  22. Classifier: Operating System :: POSIX
  23. Classifier: Programming Language :: Python
  24. Classifier: Programming Language :: Python :: 3
  25. Classifier: Programming Language :: Python :: 3.8
  26. Classifier: Programming Language :: Python :: 3.9
  27. Classifier: Programming Language :: Python :: 3.10
  28. Classifier: Programming Language :: Python :: 3.11
  29. Classifier: Programming Language :: Python :: 3.12
  30. Classifier: Programming Language :: Python :: 3.13
  31. Classifier: Programming Language :: Python :: Implementation :: CPython
  32. Classifier: Programming Language :: Python :: Implementation :: PyPy
  33. Classifier: Topic :: Software Development
  34. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  35. Classifier: Topic :: System :: Shells
  36. Classifier: Topic :: Terminals
  37. Requires-Python: >=3.8
  38. Provides-Extra: test
  39. Requires-Dist: coverage; extra == 'test'
  40. Requires-Dist: mypy; extra == 'test'
  41. Requires-Dist: pexpect; extra == 'test'
  42. Requires-Dist: ruff; extra == 'test'
  43. Requires-Dist: wheel; extra == 'test'
  44. Description-Content-Type: text/x-rst
  45. argcomplete - Bash/zsh tab completion for argparse
  46. ==================================================
  47. *Tab complete all the things!*
  48. Argcomplete provides easy, extensible command line tab completion of arguments for your Python application.
  49. It makes two assumptions:
  50. * You're using bash or zsh as your shell
  51. * You're using `argparse <http://docs.python.org/3/library/argparse.html>`_ to manage your command line arguments/options
  52. Argcomplete is particularly useful if your program has lots of options or subparsers, and if your program can
  53. dynamically suggest completions for your argument/option values (for example, if the user is browsing resources over
  54. the network).
  55. Installation
  56. ------------
  57. ::
  58. pip install argcomplete
  59. activate-global-python-argcomplete
  60. See `Activating global completion`_ below for details about the second step.
  61. Refresh your shell environment (start a new shell).
  62. Synopsis
  63. --------
  64. Add the ``PYTHON_ARGCOMPLETE_OK`` marker and a call to ``argcomplete.autocomplete()`` to your Python application as
  65. follows:
  66. .. code-block:: python
  67. #!/usr/bin/env python
  68. # PYTHON_ARGCOMPLETE_OK
  69. import argcomplete, argparse
  70. parser = argparse.ArgumentParser()
  71. ...
  72. argcomplete.autocomplete(parser)
  73. args = parser.parse_args()
  74. ...
  75. If using ``pyproject.toml`` ``[project.scripts]`` entry points, the ``PYTHON_ARGCOMPLETE_OK`` marker should appear
  76. at the beginning of the file that contains the entry point.
  77. Register your Python application with your shell's completion framework by running ``register-python-argcomplete``::
  78. eval "$(register-python-argcomplete my-python-app)"
  79. Quotes are significant; the registration will fail without them. See `Global completion`_ below for a way to enable
  80. argcomplete generally without registering each application individually.
  81. argcomplete.autocomplete(*parser*)
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. This method is the entry point to the module. It must be called **after** ArgumentParser construction is complete, but
  84. **before** the ``ArgumentParser.parse_args()`` method is called. The method looks for an environment variable that the
  85. completion hook shellcode sets, and if it's there, collects completions, prints them to the output stream (fd 8 by
  86. default), and exits. Otherwise, it returns to the caller immediately.
  87. .. admonition:: Side effects
  88. Argcomplete gets completions by running your program. It intercepts the execution flow at the moment
  89. ``argcomplete.autocomplete()`` is called. After sending completions, it exits using ``exit_method`` (``os._exit``
  90. by default). This means if your program has any side effects that happen before ``argcomplete`` is called, those
  91. side effects will happen every time the user presses ``<TAB>`` (although anything your program prints to stdout or
  92. stderr will be suppressed). For this reason it's best to construct the argument parser and call
  93. ``argcomplete.autocomplete()`` as early as possible in your execution flow.
  94. .. admonition:: Performance
  95. If the program takes a long time to get to the point where ``argcomplete.autocomplete()`` is called, the tab completion
  96. process will feel sluggish, and the user may lose confidence in it. So it's also important to minimize the startup time
  97. of the program up to that point (for example, by deferring initialization or importing of large modules until after
  98. parsing options).
  99. Specifying completers
  100. ---------------------
  101. You can specify custom completion functions for your options and arguments. Two styles are supported: callable and
  102. readline-style. Callable completers are simpler. They are called with the following keyword arguments:
  103. * ``prefix``: The prefix text of the last word before the cursor on the command line.
  104. For dynamic completers, this can be used to reduce the work required to generate possible completions.
  105. * ``action``: The ``argparse.Action`` instance that this completer was called for.
  106. * ``parser``: The ``argparse.ArgumentParser`` instance that the action was taken by.
  107. * ``parsed_args``: The result of argument parsing so far (the ``argparse.Namespace`` args object normally returned by
  108. ``ArgumentParser.parse_args()``).
  109. Completers can return their completions as an iterable of strings or a mapping (dict) of strings to their
  110. descriptions (zsh will display the descriptions as context help alongside completions). An example completer for names
  111. of environment variables might look like this:
  112. .. code-block:: python
  113. def EnvironCompleter(**kwargs):
  114. return os.environ
  115. To specify a completer for an argument or option, set the ``completer`` attribute of its associated action. An easy
  116. way to do this at definition time is:
  117. .. code-block:: python
  118. from argcomplete.completers import EnvironCompleter
  119. parser = argparse.ArgumentParser()
  120. parser.add_argument("--env-var1").completer = EnvironCompleter
  121. parser.add_argument("--env-var2").completer = EnvironCompleter
  122. argcomplete.autocomplete(parser)
  123. If you specify the ``choices`` keyword for an argparse option or argument (and don't specify a completer), it will be
  124. used for completions.
  125. A completer that is initialized with a set of all possible choices of values for its action might look like this:
  126. .. code-block:: python
  127. class ChoicesCompleter(object):
  128. def __init__(self, choices):
  129. self.choices = choices
  130. def __call__(self, **kwargs):
  131. return self.choices
  132. The following two ways to specify a static set of choices are equivalent for completion purposes:
  133. .. code-block:: python
  134. from argcomplete.completers import ChoicesCompleter
  135. parser.add_argument("--protocol", choices=('http', 'https', 'ssh', 'rsync', 'wss'))
  136. parser.add_argument("--proto").completer=ChoicesCompleter(('http', 'https', 'ssh', 'rsync', 'wss'))
  137. Note that if you use the ``choices=<completions>`` option, argparse will show
  138. all these choices in the ``--help`` output by default. To prevent this, set
  139. ``metavar`` (like ``parser.add_argument("--protocol", metavar="PROTOCOL",
  140. choices=('http', 'https', 'ssh', 'rsync', 'wss'))``).
  141. The following `script <https://raw.github.com/kislyuk/argcomplete/master/docs/examples/describe_github_user.py>`_ uses
  142. ``parsed_args`` and `Requests <http://python-requests.org/>`_ to query GitHub for publicly known members of an
  143. organization and complete their names, then prints the member description:
  144. .. code-block:: python
  145. #!/usr/bin/env python
  146. # PYTHON_ARGCOMPLETE_OK
  147. import argcomplete, argparse, requests, pprint
  148. def github_org_members(prefix, parsed_args, **kwargs):
  149. resource = "https://api.github.com/orgs/{org}/members".format(org=parsed_args.organization)
  150. return (member['login'] for member in requests.get(resource).json() if member['login'].startswith(prefix))
  151. parser = argparse.ArgumentParser()
  152. parser.add_argument("--organization", help="GitHub organization")
  153. parser.add_argument("--member", help="GitHub member").completer = github_org_members
  154. argcomplete.autocomplete(parser)
  155. args = parser.parse_args()
  156. pprint.pprint(requests.get("https://api.github.com/users/{m}".format(m=args.member)).json())
  157. `Try it <https://raw.github.com/kislyuk/argcomplete/master/docs/examples/describe_github_user.py>`_ like this::
  158. ./describe_github_user.py --organization heroku --member <TAB>
  159. If you have a useful completer to add to the `completer library
  160. <https://github.com/kislyuk/argcomplete/blob/master/argcomplete/completers.py>`_, send a pull request!
  161. Readline-style completers
  162. ~~~~~~~~~~~~~~~~~~~~~~~~~
  163. The readline_ module defines a completer protocol in rlcompleter_. Readline-style completers are also supported by
  164. argcomplete, so you can use the same completer object both in an interactive readline-powered shell and on the command
  165. line. For example, you can use the readline-style completer provided by IPython_ to get introspective completions like
  166. you would get in the IPython shell:
  167. .. _readline: http://docs.python.org/3/library/readline.html
  168. .. _rlcompleter: http://docs.python.org/3/library/rlcompleter.html#completer-objects
  169. .. _IPython: http://ipython.org/
  170. .. code-block:: python
  171. import IPython
  172. parser.add_argument("--python-name").completer = IPython.core.completer.Completer()
  173. ``argcomplete.CompletionFinder.rl_complete`` can also be used to plug in an argparse parser as a readline completer.
  174. Printing warnings in completers
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. Normal stdout/stderr output is suspended when argcomplete runs. Sometimes, though, when the user presses ``<TAB>``, it's
  177. appropriate to print information about why completions generation failed. To do this, use ``warn``:
  178. .. code-block:: python
  179. from argcomplete import warn
  180. def AwesomeWebServiceCompleter(prefix, **kwargs):
  181. if login_failed:
  182. warn("Please log in to Awesome Web Service to use autocompletion")
  183. return completions
  184. Using a custom completion validator
  185. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  186. By default, argcomplete validates your completions by checking if they start with the prefix given to the completer. You
  187. can override this validation check by supplying the ``validator`` keyword to ``argcomplete.autocomplete()``:
  188. .. code-block:: python
  189. def my_validator(completion_candidate, current_input):
  190. """Complete non-prefix substring matches."""
  191. return current_input in completion_candidate
  192. argcomplete.autocomplete(parser, validator=my_validator)
  193. Global completion
  194. -----------------
  195. In global completion mode, you don't have to register each argcomplete-capable executable separately. Instead, the shell
  196. will look for the string **PYTHON_ARGCOMPLETE_OK** in the first 1024 bytes of any executable that it's running
  197. completion for, and if it's found, follow the rest of the argcomplete protocol as described above.
  198. Additionally, completion is activated for scripts run as ``python <script>`` and ``python -m <module>``. If you're using
  199. multiple Python versions on the same system, the version being used to run the script must have argcomplete installed.
  200. .. admonition:: Bash version compatibility
  201. When using bash, global completion requires bash support for ``complete -D``, which was introduced in bash 4.2. Since
  202. Mac OS ships with an outdated version of Bash (3.2), you can either use zsh or install a newer version of bash using
  203. `Homebrew <http://brew.sh/>`_ (``brew install bash`` - you will also need to add ``/opt/homebrew/bin/bash`` to
  204. ``/etc/shells``, and run ``chsh`` to change your shell). You can check the version of the running copy of bash with
  205. ``echo $BASH_VERSION``.
  206. .. note:: If you use ``project.scripts`` directives to provide command line entry points to your package,
  207. argcomplete will follow the wrapper scripts to their destination and look for ``PYTHON_ARGCOMPLETE_OK`` in the
  208. first kilobyte of the file containing the destination code.
  209. If you choose not to use global completion, or ship a completion module that depends on argcomplete, you must register
  210. your script explicitly using ``eval "$(register-python-argcomplete my-python-app)"``. Standard completion module
  211. registration rules apply: namely, the script name is passed directly to ``complete``, meaning it is only tab completed
  212. when invoked exactly as it was registered. In the above example, ``my-python-app`` must be on the path, and the user
  213. must be attempting to complete it by that name. The above line alone would **not** allow you to complete
  214. ``./my-python-app``, or ``/path/to/my-python-app``.
  215. Activating global completion
  216. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  217. The script ``activate-global-python-argcomplete`` installs the global completion script
  218. `bash_completion.d/_python-argcomplete <https://github.com/kislyuk/argcomplete/blob/master/argcomplete/bash_completion.d/_python-argcomplete>`_
  219. into an appropriate location on your system for both bash and zsh. The specific location depends on your platform and
  220. whether you installed argcomplete system-wide using ``sudo`` or locally (into your user's home directory).
  221. Zsh Support
  222. -----------
  223. Argcomplete supports zsh. On top of plain completions like in bash, zsh allows you to see argparse help strings as
  224. completion descriptions. All shellcode included with argcomplete is compatible with both bash and zsh, so the same
  225. completer commands ``activate-global-python-argcomplete`` and ``eval "$(register-python-argcomplete my-python-app)"``
  226. work for zsh as well.
  227. Python Support
  228. --------------
  229. Argcomplete requires Python 3.9+.
  230. Support for other shells
  231. ------------------------
  232. Argcomplete maintainers provide support only for the bash and zsh shells on Linux and MacOS. For resources related to
  233. other shells and platforms, including fish, tcsh, xonsh, powershell, and Windows, please see the
  234. `contrib <https://github.com/kislyuk/argcomplete/tree/develop/contrib>`_ directory.
  235. Common Problems
  236. ---------------
  237. If global completion is not completing your script, bash may have registered a default completion function::
  238. $ complete | grep my-python-app
  239. complete -F _minimal my-python-app
  240. You can fix this by restarting your shell, or by running ``complete -r my-python-app``.
  241. Debugging
  242. ---------
  243. Set the ``_ARC_DEBUG`` variable in your shell to enable verbose debug output every time argcomplete runs. This will
  244. disrupt the command line composition state of your terminal, but make it possible to see the internal state of the
  245. completer if it encounters problems.
  246. Acknowledgments
  247. ---------------
  248. Inspired and informed by the optcomplete_ module by Martin Blais.
  249. .. _optcomplete: http://pypi.python.org/pypi/optcomplete
  250. Links
  251. -----
  252. * `Project home page (GitHub) <https://github.com/kislyuk/argcomplete>`_
  253. * `Documentation <https://kislyuk.github.io/argcomplete/>`_
  254. * `Package distribution (PyPI) <https://pypi.python.org/pypi/argcomplete>`_
  255. * `Change log <https://github.com/kislyuk/argcomplete/blob/master/Changes.rst>`_
  256. Bugs
  257. ~~~~
  258. Please report bugs, issues, feature requests, etc. on `GitHub <https://github.com/kislyuk/argcomplete/issues>`_.
  259. License
  260. -------
  261. Copyright 2012-2023, Andrey Kislyuk and argcomplete contributors. Licensed under the terms of the
  262. `Apache License, Version 2.0 <http://www.apache.org/licenses/LICENSE-2.0>`_. Distribution of the LICENSE and NOTICE
  263. files with source copies of this package and derivative works is **REQUIRED** as specified by the Apache License.
  264. .. image:: https://github.com/kislyuk/argcomplete/workflows/Python%20package/badge.svg
  265. :target: https://github.com/kislyuk/argcomplete/actions
  266. .. image:: https://codecov.io/github/kislyuk/argcomplete/coverage.svg?branch=master
  267. :target: https://codecov.io/github/kislyuk/argcomplete?branch=master
  268. .. image:: https://img.shields.io/pypi/v/argcomplete.svg
  269. :target: https://pypi.python.org/pypi/argcomplete
  270. .. image:: https://img.shields.io/pypi/l/argcomplete.svg
  271. :target: https://pypi.python.org/pypi/argcomplete