shell_completion.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. from __future__ import annotations
  2. import collections.abc as cabc
  3. import os
  4. import re
  5. import typing as t
  6. from gettext import gettext as _
  7. from .core import Argument
  8. from .core import Command
  9. from .core import Context
  10. from .core import Group
  11. from .core import Option
  12. from .core import Parameter
  13. from .core import ParameterSource
  14. from .utils import echo
  15. def shell_complete(
  16. cli: Command,
  17. ctx_args: cabc.MutableMapping[str, t.Any],
  18. prog_name: str,
  19. complete_var: str,
  20. instruction: str,
  21. ) -> int:
  22. """Perform shell completion for the given CLI program.
  23. :param cli: Command being called.
  24. :param ctx_args: Extra arguments to pass to
  25. ``cli.make_context``.
  26. :param prog_name: Name of the executable in the shell.
  27. :param complete_var: Name of the environment variable that holds
  28. the completion instruction.
  29. :param instruction: Value of ``complete_var`` with the completion
  30. instruction and shell, in the form ``instruction_shell``.
  31. :return: Status code to exit with.
  32. """
  33. shell, _, instruction = instruction.partition("_")
  34. comp_cls = get_completion_class(shell)
  35. if comp_cls is None:
  36. return 1
  37. comp = comp_cls(cli, ctx_args, prog_name, complete_var)
  38. if instruction == "source":
  39. echo(comp.source())
  40. return 0
  41. if instruction == "complete":
  42. echo(comp.complete())
  43. return 0
  44. return 1
  45. class CompletionItem:
  46. """Represents a completion value and metadata about the value. The
  47. default metadata is ``type`` to indicate special shell handling,
  48. and ``help`` if a shell supports showing a help string next to the
  49. value.
  50. Arbitrary parameters can be passed when creating the object, and
  51. accessed using ``item.attr``. If an attribute wasn't passed,
  52. accessing it returns ``None``.
  53. :param value: The completion suggestion.
  54. :param type: Tells the shell script to provide special completion
  55. support for the type. Click uses ``"dir"`` and ``"file"``.
  56. :param help: String shown next to the value if supported.
  57. :param kwargs: Arbitrary metadata. The built-in implementations
  58. don't use this, but custom type completions paired with custom
  59. shell support could use it.
  60. """
  61. __slots__ = ("value", "type", "help", "_info")
  62. def __init__(
  63. self,
  64. value: t.Any,
  65. type: str = "plain",
  66. help: str | None = None,
  67. **kwargs: t.Any,
  68. ) -> None:
  69. self.value: t.Any = value
  70. self.type: str = type
  71. self.help: str | None = help
  72. self._info = kwargs
  73. def __getattr__(self, name: str) -> t.Any:
  74. return self._info.get(name)
  75. # Only Bash >= 4.4 has the nosort option.
  76. _SOURCE_BASH = """\
  77. %(complete_func)s() {
  78. local IFS=$'\\n'
  79. local response
  80. response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD \
  81. %(complete_var)s=bash_complete $1)
  82. for completion in $response; do
  83. IFS=',' read type value <<< "$completion"
  84. if [[ $type == 'dir' ]]; then
  85. COMPREPLY=()
  86. compopt -o dirnames
  87. elif [[ $type == 'file' ]]; then
  88. COMPREPLY=()
  89. compopt -o default
  90. elif [[ $type == 'plain' ]]; then
  91. COMPREPLY+=($value)
  92. fi
  93. done
  94. return 0
  95. }
  96. %(complete_func)s_setup() {
  97. complete -o nosort -F %(complete_func)s %(prog_name)s
  98. }
  99. %(complete_func)s_setup;
  100. """
  101. # See ZshComplete.format_completion below, and issue #2703, before
  102. # changing this script.
  103. #
  104. # (TL;DR: _describe is picky about the format, but this Zsh script snippet
  105. # is already widely deployed. So freeze this script, and use clever-ish
  106. # handling of colons in ZshComplet.format_completion.)
  107. _SOURCE_ZSH = """\
  108. #compdef %(prog_name)s
  109. %(complete_func)s() {
  110. local -a completions
  111. local -a completions_with_descriptions
  112. local -a response
  113. (( ! $+commands[%(prog_name)s] )) && return 1
  114. response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) \
  115. %(complete_var)s=zsh_complete %(prog_name)s)}")
  116. for type key descr in ${response}; do
  117. if [[ "$type" == "plain" ]]; then
  118. if [[ "$descr" == "_" ]]; then
  119. completions+=("$key")
  120. else
  121. completions_with_descriptions+=("$key":"$descr")
  122. fi
  123. elif [[ "$type" == "dir" ]]; then
  124. _path_files -/
  125. elif [[ "$type" == "file" ]]; then
  126. _path_files -f
  127. fi
  128. done
  129. if [ -n "$completions_with_descriptions" ]; then
  130. _describe -V unsorted completions_with_descriptions -U
  131. fi
  132. if [ -n "$completions" ]; then
  133. compadd -U -V unsorted -a completions
  134. fi
  135. }
  136. if [[ $zsh_eval_context[-1] == loadautofunc ]]; then
  137. # autoload from fpath, call function directly
  138. %(complete_func)s "$@"
  139. else
  140. # eval/source/. command, register function for later
  141. compdef %(complete_func)s %(prog_name)s
  142. fi
  143. """
  144. _SOURCE_FISH = """\
  145. function %(complete_func)s;
  146. set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) \
  147. COMP_CWORD=(commandline -t) %(prog_name)s);
  148. for completion in $response;
  149. set -l metadata (string split "," $completion);
  150. if test $metadata[1] = "dir";
  151. __fish_complete_directories $metadata[2];
  152. else if test $metadata[1] = "file";
  153. __fish_complete_path $metadata[2];
  154. else if test $metadata[1] = "plain";
  155. echo $metadata[2];
  156. end;
  157. end;
  158. end;
  159. complete --no-files --command %(prog_name)s --arguments \
  160. "(%(complete_func)s)";
  161. """
  162. class ShellComplete:
  163. """Base class for providing shell completion support. A subclass for
  164. a given shell will override attributes and methods to implement the
  165. completion instructions (``source`` and ``complete``).
  166. :param cli: Command being called.
  167. :param prog_name: Name of the executable in the shell.
  168. :param complete_var: Name of the environment variable that holds
  169. the completion instruction.
  170. .. versionadded:: 8.0
  171. """
  172. name: t.ClassVar[str]
  173. """Name to register the shell as with :func:`add_completion_class`.
  174. This is used in completion instructions (``{name}_source`` and
  175. ``{name}_complete``).
  176. """
  177. source_template: t.ClassVar[str]
  178. """Completion script template formatted by :meth:`source`. This must
  179. be provided by subclasses.
  180. """
  181. def __init__(
  182. self,
  183. cli: Command,
  184. ctx_args: cabc.MutableMapping[str, t.Any],
  185. prog_name: str,
  186. complete_var: str,
  187. ) -> None:
  188. self.cli = cli
  189. self.ctx_args = ctx_args
  190. self.prog_name = prog_name
  191. self.complete_var = complete_var
  192. @property
  193. def func_name(self) -> str:
  194. """The name of the shell function defined by the completion
  195. script.
  196. """
  197. safe_name = re.sub(r"\W*", "", self.prog_name.replace("-", "_"), flags=re.ASCII)
  198. return f"_{safe_name}_completion"
  199. def source_vars(self) -> dict[str, t.Any]:
  200. """Vars for formatting :attr:`source_template`.
  201. By default this provides ``complete_func``, ``complete_var``,
  202. and ``prog_name``.
  203. """
  204. return {
  205. "complete_func": self.func_name,
  206. "complete_var": self.complete_var,
  207. "prog_name": self.prog_name,
  208. }
  209. def source(self) -> str:
  210. """Produce the shell script that defines the completion
  211. function. By default this ``%``-style formats
  212. :attr:`source_template` with the dict returned by
  213. :meth:`source_vars`.
  214. """
  215. return self.source_template % self.source_vars()
  216. def get_completion_args(self) -> tuple[list[str], str]:
  217. """Use the env vars defined by the shell script to return a
  218. tuple of ``args, incomplete``. This must be implemented by
  219. subclasses.
  220. """
  221. raise NotImplementedError
  222. def get_completions(self, args: list[str], incomplete: str) -> list[CompletionItem]:
  223. """Determine the context and last complete command or parameter
  224. from the complete args. Call that object's ``shell_complete``
  225. method to get the completions for the incomplete value.
  226. :param args: List of complete args before the incomplete value.
  227. :param incomplete: Value being completed. May be empty.
  228. """
  229. ctx = _resolve_context(self.cli, self.ctx_args, self.prog_name, args)
  230. obj, incomplete = _resolve_incomplete(ctx, args, incomplete)
  231. return obj.shell_complete(ctx, incomplete)
  232. def format_completion(self, item: CompletionItem) -> str:
  233. """Format a completion item into the form recognized by the
  234. shell script. This must be implemented by subclasses.
  235. :param item: Completion item to format.
  236. """
  237. raise NotImplementedError
  238. def complete(self) -> str:
  239. """Produce the completion data to send back to the shell.
  240. By default this calls :meth:`get_completion_args`, gets the
  241. completions, then calls :meth:`format_completion` for each
  242. completion.
  243. """
  244. args, incomplete = self.get_completion_args()
  245. completions = self.get_completions(args, incomplete)
  246. out = [self.format_completion(item) for item in completions]
  247. return "\n".join(out)
  248. class BashComplete(ShellComplete):
  249. """Shell completion for Bash."""
  250. name = "bash"
  251. source_template = _SOURCE_BASH
  252. @staticmethod
  253. def _check_version() -> None:
  254. import shutil
  255. import subprocess
  256. bash_exe = shutil.which("bash")
  257. if bash_exe is None:
  258. match = None
  259. else:
  260. output = subprocess.run(
  261. [bash_exe, "--norc", "-c", 'echo "${BASH_VERSION}"'],
  262. stdout=subprocess.PIPE,
  263. )
  264. match = re.search(r"^(\d+)\.(\d+)\.\d+", output.stdout.decode())
  265. if match is not None:
  266. major, minor = match.groups()
  267. if major < "4" or major == "4" and minor < "4":
  268. echo(
  269. _(
  270. "Shell completion is not supported for Bash"
  271. " versions older than 4.4."
  272. ),
  273. err=True,
  274. )
  275. else:
  276. echo(
  277. _("Couldn't detect Bash version, shell completion is not supported."),
  278. err=True,
  279. )
  280. def source(self) -> str:
  281. self._check_version()
  282. return super().source()
  283. def get_completion_args(self) -> tuple[list[str], str]:
  284. cwords = split_arg_string(os.environ["COMP_WORDS"])
  285. cword = int(os.environ["COMP_CWORD"])
  286. args = cwords[1:cword]
  287. try:
  288. incomplete = cwords[cword]
  289. except IndexError:
  290. incomplete = ""
  291. return args, incomplete
  292. def format_completion(self, item: CompletionItem) -> str:
  293. return f"{item.type},{item.value}"
  294. class ZshComplete(ShellComplete):
  295. """Shell completion for Zsh."""
  296. name = "zsh"
  297. source_template = _SOURCE_ZSH
  298. def get_completion_args(self) -> tuple[list[str], str]:
  299. cwords = split_arg_string(os.environ["COMP_WORDS"])
  300. cword = int(os.environ["COMP_CWORD"])
  301. args = cwords[1:cword]
  302. try:
  303. incomplete = cwords[cword]
  304. except IndexError:
  305. incomplete = ""
  306. return args, incomplete
  307. def format_completion(self, item: CompletionItem) -> str:
  308. help_ = item.help or "_"
  309. # The zsh completion script uses `_describe` on items with help
  310. # texts (which splits the item help from the item value at the
  311. # first unescaped colon) and `compadd` on items without help
  312. # text (which uses the item value as-is and does not support
  313. # colon escaping). So escape colons in the item value if and
  314. # only if the item help is not the sentinel "_" value, as used
  315. # by the completion script.
  316. #
  317. # (The zsh completion script is potentially widely deployed, and
  318. # thus harder to fix than this method.)
  319. #
  320. # See issue #1812 and issue #2703 for further context.
  321. value = item.value.replace(":", r"\:") if help_ != "_" else item.value
  322. return f"{item.type}\n{value}\n{help_}"
  323. class FishComplete(ShellComplete):
  324. """Shell completion for Fish."""
  325. name = "fish"
  326. source_template = _SOURCE_FISH
  327. def get_completion_args(self) -> tuple[list[str], str]:
  328. cwords = split_arg_string(os.environ["COMP_WORDS"])
  329. incomplete = os.environ["COMP_CWORD"]
  330. if incomplete:
  331. incomplete = split_arg_string(incomplete)[0]
  332. args = cwords[1:]
  333. # Fish stores the partial word in both COMP_WORDS and
  334. # COMP_CWORD, remove it from complete args.
  335. if incomplete and args and args[-1] == incomplete:
  336. args.pop()
  337. return args, incomplete
  338. def format_completion(self, item: CompletionItem) -> str:
  339. if item.help:
  340. return f"{item.type},{item.value}\t{item.help}"
  341. return f"{item.type},{item.value}"
  342. ShellCompleteType = t.TypeVar("ShellCompleteType", bound="type[ShellComplete]")
  343. _available_shells: dict[str, type[ShellComplete]] = {
  344. "bash": BashComplete,
  345. "fish": FishComplete,
  346. "zsh": ZshComplete,
  347. }
  348. def add_completion_class(
  349. cls: ShellCompleteType, name: str | None = None
  350. ) -> ShellCompleteType:
  351. """Register a :class:`ShellComplete` subclass under the given name.
  352. The name will be provided by the completion instruction environment
  353. variable during completion.
  354. :param cls: The completion class that will handle completion for the
  355. shell.
  356. :param name: Name to register the class under. Defaults to the
  357. class's ``name`` attribute.
  358. """
  359. if name is None:
  360. name = cls.name
  361. _available_shells[name] = cls
  362. return cls
  363. def get_completion_class(shell: str) -> type[ShellComplete] | None:
  364. """Look up a registered :class:`ShellComplete` subclass by the name
  365. provided by the completion instruction environment variable. If the
  366. name isn't registered, returns ``None``.
  367. :param shell: Name the class is registered under.
  368. """
  369. return _available_shells.get(shell)
  370. def split_arg_string(string: str) -> list[str]:
  371. """Split an argument string as with :func:`shlex.split`, but don't
  372. fail if the string is incomplete. Ignores a missing closing quote or
  373. incomplete escape sequence and uses the partial token as-is.
  374. .. code-block:: python
  375. split_arg_string("example 'my file")
  376. ["example", "my file"]
  377. split_arg_string("example my\\")
  378. ["example", "my"]
  379. :param string: String to split.
  380. .. versionchanged:: 8.2
  381. Moved to ``shell_completion`` from ``parser``.
  382. """
  383. import shlex
  384. lex = shlex.shlex(string, posix=True)
  385. lex.whitespace_split = True
  386. lex.commenters = ""
  387. out = []
  388. try:
  389. for token in lex:
  390. out.append(token)
  391. except ValueError:
  392. # Raised when end-of-string is reached in an invalid state. Use
  393. # the partial token as-is. The quote or escape character is in
  394. # lex.state, not lex.token.
  395. out.append(lex.token)
  396. return out
  397. def _is_incomplete_argument(ctx: Context, param: Parameter) -> bool:
  398. """Determine if the given parameter is an argument that can still
  399. accept values.
  400. :param ctx: Invocation context for the command represented by the
  401. parsed complete args.
  402. :param param: Argument object being checked.
  403. """
  404. if not isinstance(param, Argument):
  405. return False
  406. assert param.name is not None
  407. # Will be None if expose_value is False.
  408. value = ctx.params.get(param.name)
  409. return (
  410. param.nargs == -1
  411. or ctx.get_parameter_source(param.name) is not ParameterSource.COMMANDLINE
  412. or (
  413. param.nargs > 1
  414. and isinstance(value, (tuple, list))
  415. and len(value) < param.nargs
  416. )
  417. )
  418. def _start_of_option(ctx: Context, value: str) -> bool:
  419. """Check if the value looks like the start of an option."""
  420. if not value:
  421. return False
  422. c = value[0]
  423. return c in ctx._opt_prefixes
  424. def _is_incomplete_option(ctx: Context, args: list[str], param: Parameter) -> bool:
  425. """Determine if the given parameter is an option that needs a value.
  426. :param args: List of complete args before the incomplete value.
  427. :param param: Option object being checked.
  428. """
  429. if not isinstance(param, Option):
  430. return False
  431. if param.is_flag or param.count:
  432. return False
  433. last_option = None
  434. for index, arg in enumerate(reversed(args)):
  435. if index + 1 > param.nargs:
  436. break
  437. if _start_of_option(ctx, arg):
  438. last_option = arg
  439. break
  440. return last_option is not None and last_option in param.opts
  441. def _resolve_context(
  442. cli: Command,
  443. ctx_args: cabc.MutableMapping[str, t.Any],
  444. prog_name: str,
  445. args: list[str],
  446. ) -> Context:
  447. """Produce the context hierarchy starting with the command and
  448. traversing the complete arguments. This only follows the commands,
  449. it doesn't trigger input prompts or callbacks.
  450. :param cli: Command being called.
  451. :param prog_name: Name of the executable in the shell.
  452. :param args: List of complete args before the incomplete value.
  453. """
  454. ctx_args["resilient_parsing"] = True
  455. with cli.make_context(prog_name, args.copy(), **ctx_args) as ctx:
  456. args = ctx._protected_args + ctx.args
  457. while args:
  458. command = ctx.command
  459. if isinstance(command, Group):
  460. if not command.chain:
  461. name, cmd, args = command.resolve_command(ctx, args)
  462. if cmd is None:
  463. return ctx
  464. with cmd.make_context(
  465. name, args, parent=ctx, resilient_parsing=True
  466. ) as sub_ctx:
  467. ctx = sub_ctx
  468. args = ctx._protected_args + ctx.args
  469. else:
  470. sub_ctx = ctx
  471. while args:
  472. name, cmd, args = command.resolve_command(ctx, args)
  473. if cmd is None:
  474. return ctx
  475. with cmd.make_context(
  476. name,
  477. args,
  478. parent=ctx,
  479. allow_extra_args=True,
  480. allow_interspersed_args=False,
  481. resilient_parsing=True,
  482. ) as sub_sub_ctx:
  483. sub_ctx = sub_sub_ctx
  484. args = sub_ctx.args
  485. ctx = sub_ctx
  486. args = [*sub_ctx._protected_args, *sub_ctx.args]
  487. else:
  488. break
  489. return ctx
  490. def _resolve_incomplete(
  491. ctx: Context, args: list[str], incomplete: str
  492. ) -> tuple[Command | Parameter, str]:
  493. """Find the Click object that will handle the completion of the
  494. incomplete value. Return the object and the incomplete value.
  495. :param ctx: Invocation context for the command represented by
  496. the parsed complete args.
  497. :param args: List of complete args before the incomplete value.
  498. :param incomplete: Value being completed. May be empty.
  499. """
  500. # Different shells treat an "=" between a long option name and
  501. # value differently. Might keep the value joined, return the "="
  502. # as a separate item, or return the split name and value. Always
  503. # split and discard the "=" to make completion easier.
  504. if incomplete == "=":
  505. incomplete = ""
  506. elif "=" in incomplete and _start_of_option(ctx, incomplete):
  507. name, _, incomplete = incomplete.partition("=")
  508. args.append(name)
  509. # The "--" marker tells Click to stop treating values as options
  510. # even if they start with the option character. If it hasn't been
  511. # given and the incomplete arg looks like an option, the current
  512. # command will provide option name completions.
  513. if "--" not in args and _start_of_option(ctx, incomplete):
  514. return ctx.command, incomplete
  515. params = ctx.command.get_params(ctx)
  516. # If the last complete arg is an option name with an incomplete
  517. # value, the option will provide value completions.
  518. for param in params:
  519. if _is_incomplete_option(ctx, args, param):
  520. return param, incomplete
  521. # It's not an option name or value. The first argument without a
  522. # parsed value will provide value completions.
  523. for param in params:
  524. if _is_incomplete_argument(ctx, param):
  525. return param, incomplete
  526. # There were no unparsed arguments, the command may be a group that
  527. # will provide command name completions.
  528. return ctx.command, incomplete