Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

93 строки
2.3 KiB

  1. from typing import List
  2. import click
  3. from click.core import _check_multicommand
  4. def print_bench_version(ctx, param, value):
  5. """Prints current bench version"""
  6. if not value or ctx.resilient_parsing:
  7. return
  8. import bench
  9. click.echo(bench.VERSION)
  10. ctx.exit()
  11. class MultiCommandGroup(click.Group):
  12. def add_command(self, cmd, name=None):
  13. """Registers another :class:`Command` with this group. If the name
  14. is not provided, the name of the command is used.
  15. Note: This is a custom Group that allows passing a list of names for
  16. the command name.
  17. """
  18. name = name or cmd.name
  19. if name is None:
  20. raise TypeError("Command has no name.")
  21. _check_multicommand(self, name, cmd, register=True)
  22. try:
  23. self.commands[name] = cmd
  24. except TypeError:
  25. if isinstance(name, list):
  26. for _name in name:
  27. self.commands[_name] = cmd
  28. class SugaredOption(click.Option):
  29. def __init__(self, *args, **kwargs):
  30. self.only_if_set: List = kwargs.pop("only_if_set")
  31. kwargs["help"] = (
  32. kwargs.get("help", "")
  33. + f". Option is acceptable only if {', '.join(self.only_if_set)} is used."
  34. )
  35. super().__init__(*args, **kwargs)
  36. def handle_parse_result(self, ctx, opts, args):
  37. current_opt = self.name in opts
  38. if current_opt and self.only_if_set:
  39. for opt in self.only_if_set:
  40. if opt not in opts:
  41. deafaults_set = [x.default for x in ctx.command.params if x.name == opt]
  42. if not deafaults_set:
  43. raise click.UsageError(f"Illegal Usage: Set '{opt}' before '{self.name}'.")
  44. return super().handle_parse_result(ctx, opts, args)
  45. def use_experimental_feature(ctx, param, value):
  46. if not value:
  47. return
  48. if value == "dynamic-feed":
  49. import bench.cli
  50. bench.cli.dynamic_feed = True
  51. bench.cli.verbose = True
  52. else:
  53. from bench.exceptions import FeatureDoesNotExistError
  54. raise FeatureDoesNotExistError(f"Feature {value} does not exist")
  55. from bench.cli import is_envvar_warn_set
  56. if is_envvar_warn_set:
  57. return
  58. click.secho(
  59. "WARNING: bench is using it's new CLI rendering engine. This behaviour has"
  60. f" been enabled by passing --{value} in the command. This feature is"
  61. " experimental and may not be implemented for all commands yet.",
  62. fg="yellow",
  63. )
  64. def setup_verbosity(ctx, param, value):
  65. if not value:
  66. return
  67. import bench.cli
  68. bench.cli.verbose = True