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