Skip to content

Commit d38b362

Browse files
authored
Merge pull request #89 from Paperspace/PS-10557-highlight-required-in-command-help-message
Color option's extra tags (required, default, etc.)
2 parents bfc2de2 + 843bd72 commit d38b362

File tree

10 files changed

+238
-207
lines changed

10 files changed

+238
-207
lines changed

gradient/cli/common.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import functools
22
import json
3+
import re
34

45
import click
6+
import colorama
7+
import termcolor
58
import yaml
69
from click.exceptions import Exit
710
from click_didyoumean import DYMMixin
811
from click_help_colors import HelpColorsGroup
912

1013
from gradient.cli import cli_types
14+
from gradient.config import config
1115

1216
OPTIONS_FILE_OPTION_NAME = "optionsFile"
1317
OPTIONS_FILE_PARAMETER_NAME = "options_file"
@@ -78,19 +82,46 @@ def handle_parse_result(self, ctx, opts, args):
7882
ctx, opts, args)
7983

8084

81-
class ArgumentReadValueFromConfigFile(ReadValueFromConfigFile, click.Argument):
85+
class ColorExtrasInCommandHelpMixin(object):
86+
def get_help_record(self, *args, **kwargs):
87+
rv = super(ColorExtrasInCommandHelpMixin, self).get_help_record(*args, **kwargs)
88+
if not config.USE_CONSOLE_COLORS:
89+
return rv
90+
91+
help_str = rv[1]
92+
if help_str:
93+
help_str = self._color_extras(help_str)
94+
rv = rv[0], help_str
95+
return rv
96+
97+
def _color_extras(self, s):
98+
pattern = re.compile(r"^.*(\[.*\])$")
99+
found = re.findall(pattern, s)
100+
if found:
101+
extras_str = found[-1]
102+
coloured_extras_str = self._color_str(extras_str)
103+
s = s.replace(extras_str, coloured_extras_str)
104+
105+
return s
106+
107+
def _color_str(self, s):
108+
s = termcolor.colored(s, config.HELP_HEADERS_COLOR)
109+
return s
110+
111+
112+
class GradientArgument(ColorExtrasInCommandHelpMixin, ReadValueFromConfigFile, click.Argument):
82113
pass
83114

84115

85-
class OptionReadValueFromConfigFile(ReadValueFromConfigFile, click.Option):
116+
class GradientOption(ColorExtrasInCommandHelpMixin, ReadValueFromConfigFile, click.Option):
86117
pass
87118

88119

89120
api_key_option = click.option(
90121
"--apiKey",
91122
"api_key",
92123
help="API key to use this time only",
93-
cls=OptionReadValueFromConfigFile,
124+
cls=GradientOption,
94125
)
95126

96127

gradient/cli/deployments.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,56 +44,56 @@ def get_deployment_client(api_key):
4444
type=ChoiceType(DEPLOYMENT_TYPES_MAP, case_sensitive=False),
4545
required=True,
4646
help="Model deployment type. Only TensorFlow models can currently be deployed",
47-
cls=common.OptionReadValueFromConfigFile,
47+
cls=common.GradientOption,
4848
)
4949
@click.option(
5050
"--modelId",
5151
"model_id",
5252
required=True,
5353
help="ID of a trained model",
54-
cls=common.OptionReadValueFromConfigFile,
54+
cls=common.GradientOption,
5555
)
5656
@click.option(
5757
"--name",
5858
"name",
5959
required=True,
6060
help="Human-friendly name for new model deployment",
61-
cls=common.OptionReadValueFromConfigFile,
61+
cls=common.GradientOption,
6262
)
6363
@click.option(
6464
"--machineType",
6565
"machine_type",
6666
required=True,
6767
help="Type of machine for new deployment",
68-
cls=common.OptionReadValueFromConfigFile,
68+
cls=common.GradientOption,
6969
)
7070
@click.option(
7171
"--imageUrl",
7272
"image_url",
7373
required=True,
7474
help="Docker image for model serving",
75-
cls=common.OptionReadValueFromConfigFile,
75+
cls=common.GradientOption,
7676
)
7777
@click.option(
7878
"--instanceCount",
7979
"instance_count",
8080
type=int,
8181
required=True,
8282
help="Number of machine instances",
83-
cls=common.OptionReadValueFromConfigFile,
83+
cls=common.GradientOption,
8484
)
8585
@click.option(
8686
"--clusterId",
8787
"cluster_id",
8888
help="Cluster ID",
89-
cls=common.OptionReadValueFromConfigFile,
89+
cls=common.GradientOption,
9090
)
9191
@click.option(
9292
"--vpc",
9393
"use_vpc",
9494
type=bool,
9595
is_flag=True,
96-
cls=common.OptionReadValueFromConfigFile,
96+
cls=common.GradientOption,
9797
)
9898
@api_key_option
9999
@common.options_file
@@ -123,19 +123,19 @@ def create_deployment(api_key, use_vpc, options_file, **kwargs):
123123
"state",
124124
type=ChoiceType(DEPLOYMENT_STATES_MAP, case_sensitive=False),
125125
help="Filter by deployment state",
126-
cls=common.OptionReadValueFromConfigFile,
126+
cls=common.GradientOption,
127127
)
128128
@click.option(
129129
"--projectId",
130130
"project_id",
131131
help="Use to filter by project ID",
132-
cls=common.OptionReadValueFromConfigFile,
132+
cls=common.GradientOption,
133133
)
134134
@click.option(
135135
"--modelId",
136136
"model_id",
137137
help="Use to filter by model ID",
138-
cls=common.OptionReadValueFromConfigFile,
138+
cls=common.GradientOption,
139139
)
140140
@api_key_option
141141
@common.options_file
@@ -155,14 +155,14 @@ def get_deployments_list(api_key, options_file, **filters):
155155
"id_",
156156
required=True,
157157
help="Deployment ID",
158-
cls=common.OptionReadValueFromConfigFile,
158+
cls=common.GradientOption,
159159
)
160160
@click.option(
161161
"--vpc",
162162
"use_vpc",
163163
type=bool,
164164
is_flag=True,
165-
cls=common.OptionReadValueFromConfigFile,
165+
cls=common.GradientOption,
166166
)
167167
@api_key_option
168168
@common.options_file
@@ -178,14 +178,14 @@ def start_deployment(id_, use_vpc, options_file, api_key=None):
178178
"id_",
179179
required=True,
180180
help="Deployment ID",
181-
cls=common.OptionReadValueFromConfigFile,
181+
cls=common.GradientOption,
182182
)
183183
@click.option(
184184
"--vpc",
185185
"use_vpc",
186186
type=bool,
187187
is_flag=True,
188-
cls=common.OptionReadValueFromConfigFile,
188+
cls=common.GradientOption,
189189
)
190190
@api_key_option
191191
@common.options_file

0 commit comments

Comments
 (0)