You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

4 years ago
  1. # coding: utf-8
  2. #
  3. # Copyright © 2010—2014 Andrey Mikhaylenko and contributors
  4. #
  5. # This file is part of Argh.
  6. #
  7. # Argh is free software under terms of the GNU Lesser
  8. # General Public License version 3 (LGPLv3) as published by the Free
  9. # Software Foundation. See the file README.rst for copying conditions.
  10. #
  11. """
  12. Shell completion
  13. ~~~~~~~~~~~~~~~~
  14. Command and argument completion is a great way to reduce the number of
  15. keystrokes and improve user experience.
  16. To display suggestions when you press :kbd:`tab`, a shell must obtain choices
  17. from your program. It calls the program in a specific environment and expects
  18. it to return a list of relevant choices.
  19. `Argparse` does not support completion out of the box. However, there are
  20. 3rd-party apps that do the job, such as argcomplete_ and
  21. python-selfcompletion_.
  22. `Argh` supports only argcomplete_ which doesn't require subclassing
  23. the parser and monkey-patches it instead. Combining `Argh`
  24. with python-selfcompletion_ isn't much harder though: simply use
  25. `SelfCompletingArgumentParser` instead of vanilla `ArgumentParser`.
  26. See installation details and gotchas in the documentation of the 3rd-party app
  27. you've chosen for the completion backend.
  28. `Argh` automatically enables completion if argcomplete_ is available
  29. (see :attr:`COMPLETION_ENABLED`). If completion is undesirable in given app by
  30. design, it can be turned off by setting ``completion=False``
  31. in :func:`argh.dispatching.dispatch`.
  32. Note that you don't *have* to add completion via `Argh`; it doesn't matter
  33. whether you let it do it for you or use the underlying API.
  34. .. _argcomplete: https://github.com/kislyuk/argcomplete
  35. .. _python-selfcompletion: https://github.com/dbarnett/python-selfcompletion
  36. Argument-level completion
  37. -------------------------
  38. Argcomplete_ supports custom "completers". The documentation suggests adding
  39. the completer as an attribute of the argument parser action::
  40. parser.add_argument("--env-var1").completer = EnvironCompleter
  41. However, this doesn't fit the normal `Argh`-assisted workflow.
  42. It is recommended to use the :func:`~argh.decorators.arg` decorator::
  43. @arg('--env-var1', completer=EnvironCompleter)
  44. def func(...):
  45. ...
  46. """
  47. import logging
  48. import os
  49. COMPLETION_ENABLED = False
  50. """
  51. Dynamically set to `True` on load if argcomplete_ was successfully imported.
  52. """
  53. try:
  54. import argcomplete
  55. except ImportError:
  56. pass
  57. else:
  58. COMPLETION_ENABLED = True
  59. __all__ = ['autocomplete', 'COMPLETION_ENABLED']
  60. logger = logging.getLogger(__package__)
  61. def autocomplete(parser):
  62. """
  63. Adds support for shell completion via argcomplete_ by patching given
  64. `argparse.ArgumentParser` (sub)class.
  65. If completion is not enabled, logs a debug-level message.
  66. """
  67. if COMPLETION_ENABLED:
  68. argcomplete.autocomplete(parser)
  69. elif 'bash' in os.getenv('SHELL', ''):
  70. logger.debug('Bash completion not available. Install argcomplete.')