METADATA 16 KB

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