Skip to content

Commit 186d2d5

Browse files
Merge pull request #2106 from allmightyspiff/issue2046
Show both binary flag options
2 parents 931e72d + fdeae2c commit 186d2d5

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

Diff for: SoftLayer/CLI/command.py

+17-6
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,20 @@ 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("|") + 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}"
237248
options_table.add_row(opt1, opt2, self.highlighter(help_message))
238249

239250
self.console.print(options_table)

Diff for: SoftLayer/CLI/virt/capacity/__init__.py

+7-2
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.
@@ -19,8 +21,11 @@ class CapacityCommands(click.MultiCommand):
1921
"""
2022

2123
def __init__(self, **attrs):
22-
click.MultiCommand.__init__(self, **attrs)
24+
CommandLoader.__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."""

Diff for: SoftLayer/CLI/virt/placementgroup/__init__.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,26 @@
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.
1719
So this small loader is required for going that third level.
1820
"""
1921

2022
def __init__(self, **attrs):
21-
click.MultiCommand.__init__(self, **attrs)
23+
CommandLoader.__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."""

Diff for: SoftLayer/utils.py

+3-3
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)