Skip to content

Commit fdea981

Browse files
#2046 fixed secondary options not showing up in the help message, colored the vs placementgroup and vs capacity commands
1 parent bd95421 commit fdea981

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

SoftLayer/CLI/command.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class OptionHighlighter(RegexHighlighter):
2828
r"(?P<option>\-\-[\w\-]+)", # long options like --verbose
2929
r"(?P<default_option>\[[^\]]+\])", # anything between [], usually default options
3030
r"(?P<option_choices>Choices: )",
31+
r"(?P<args_keyword>^[A-Z]+$)",
3132
]
3233

3334

@@ -177,6 +178,8 @@ def format_usage(self, ctx: click.Context, formatter: click.formatting.HelpForma
177178
pieces[index] = "[options][OPTIONS][/]"
178179
elif piece == "COMMAND [ARGS]...":
179180
pieces[index] = "[command]COMMAND[/] [args][ARGS][/] ..."
181+
else:
182+
pieces[index] = f"[args_keyword]{piece}[/]"
180183

181184
self.console.print(f"Usage: [path]{ctx.command_path}[/] {' '.join(pieces)}")
182185

@@ -202,16 +205,23 @@ def format_epilog(self, ctx: click.Context, formatter: click.formatting.HelpForm
202205
def format_options(self, ctx, formatter):
203206
"""Prints out the options in a table format"""
204207

205-
# NEXT support binary options --yes/--no
206-
# NEXT SUPPORT color for IDENTIFIER and such
207208
options_table = Table(highlight=True, box=box.SQUARE, show_header=False)
208209

209210
for param in self.get_params(ctx):
211+
# useful for showing whats in a param
212+
# print(param.to_info_dict())
213+
214+
# Set Arguments to all uppercase
215+
if param.param_type_name == 'argument':
216+
param.opts[0] = param.opts[0].upper()
217+
218+
# This option has a short (-v) and long (--verbose) options
210219
if len(param.opts) == 2:
211220
opt1 = self.highlighter(param.opts[1])
212221
opt2 = self.highlighter(param.opts[0])
213222
else:
214223
opt2 = self.highlighter(param.opts[0])
224+
# Needs to be the Text() type because rich.Text doesn't mesh with string
215225
opt1 = Text("")
216226

217227
# Ensures the short option is always in opt1.
@@ -221,19 +231,21 @@ def format_options(self, ctx, formatter):
221231
if param.metavar:
222232
opt2 += Text(f" {param.metavar}", style="bold yellow")
223233

224-
options = Text(" ".join(reversed(param.opts)))
234+
# secondary_opts are usually for flags --enable/--disable
235+
if len(param.secondary_opts) == 1:
236+
opt2 += Text(f"|") + self.highlighter(param.secondary_opts[0])
237+
225238
help_record = param.get_help_record(ctx)
226239
help_message = ""
227240
if help_record:
228-
help_message = param.get_help_record(ctx)[-1]
241+
help_message = Text(param.get_help_record(ctx)[-1])
229242

230243
# Add Click choices to help message
231244
if isinstance(param.type, click.Choice):
232245
choices = ", ".join(param.type.choices)
233246
help_message += f" Choices: {choices}"
234247

235-
if param.metavar:
236-
options += f" {param.metavar}"
248+
237249
options_table.add_row(opt1, opt2, self.highlighter(help_message))
238250

239251
self.console.print(options_table)

SoftLayer/CLI/virt/capacity/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import os
66

77
import click
8+
from SoftLayer.CLI.command import CommandLoader
9+
from SoftLayer.CLI.command import OptionHighlighter
810

911
CONTEXT = {'help_option_names': ['-h', '--help'],
1012
'max_content_width': 999}
1113

1214

13-
class CapacityCommands(click.MultiCommand):
15+
class CapacityCommands(CommandLoader):
1416
"""Loads module for capacity related commands.
1517
1618
Will automatically replace _ with - where appropriate.
@@ -21,6 +23,9 @@ class CapacityCommands(click.MultiCommand):
2123
def __init__(self, **attrs):
2224
click.MultiCommand.__init__(self, **attrs)
2325
self.path = os.path.dirname(__file__)
26+
self.highlighter = OptionHighlighter()
27+
self.env = None
28+
self.console = None
2429

2530
def list_commands(self, ctx):
2631
"""List all sub-commands."""

SoftLayer/CLI/virt/placementgroup/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import os
66

77
import click
8+
from SoftLayer.CLI.command import CommandLoader
9+
from SoftLayer.CLI.command import OptionHighlighter
810

911
CONTEXT = {'help_option_names': ['-h', '--help'],
1012
'max_content_width': 999}
1113

1214

13-
class PlacementGroupCommands(click.MultiCommand):
15+
class PlacementGroupCommands(CommandLoader):
1416
"""Loads module for placement group related commands.
1517
1618
Currently the base command loader only supports going two commands deep.
@@ -20,6 +22,9 @@ class PlacementGroupCommands(click.MultiCommand):
2022
def __init__(self, **attrs):
2123
click.MultiCommand.__init__(self, **attrs)
2224
self.path = os.path.dirname(__file__)
25+
self.highlighter = OptionHighlighter()
26+
self.env = None
27+
self.console = None
2328

2429
def list_commands(self, ctx):
2530
"""List all sub-commands."""

SoftLayer/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def decode_stacked(document, pos=0, decoder=JSONDecoder()):
474474

475475

476476
def console_color_themes(theme):
477-
"""Colors in https://rich.readthedocs.io/en/stable/appendix/colors.html?highlight=light_pink1#standard-colors"""
477+
"""Colors in https://rich.readthedocs.io/en/stable/appendix/colors.html#standard-colors"""
478478

479479
if theme == 'light':
480480
return Console(theme=Theme(
@@ -490,7 +490,7 @@ def console_color_themes(theme):
490490
"switch": "bold green4",
491491
"default_option": "light_coral",
492492
"option_keyword": "bold dark_cyan",
493-
"args_keyword": "bold green4",
493+
"args_keyword": "underline orange4",
494494
"option_choices": "gold3",
495495
})
496496
)
@@ -507,7 +507,7 @@ def console_color_themes(theme):
507507
"switch": "bold green",
508508
"default_option": "light_pink1",
509509
"option_keyword": "bold cyan",
510-
"args_keyword": "bold green",
510+
"args_keyword": "underline yellow",
511511
"option_choices": "gold3",
512512
})
513513
)

0 commit comments

Comments
 (0)