Skip to content

Commit

Permalink
Merge pull request #2106 from allmightyspiff/issue2046
Browse files Browse the repository at this point in the history
Show both binary flag options
  • Loading branch information
allmightyspiff authored Oct 10, 2023
2 parents 931e72d + fdeae2c commit 186d2d5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
23 changes: 17 additions & 6 deletions SoftLayer/CLI/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class OptionHighlighter(RegexHighlighter):
r"(?P<option>\-\-[\w\-]+)", # long options like --verbose
r"(?P<default_option>\[[^\]]+\])", # anything between [], usually default options
r"(?P<option_choices>Choices: )",
r"(?P<args_keyword>^[A-Z]+$)",
]


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

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

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

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

for param in self.get_params(ctx):
# useful for showing whats in a param
# print(param.to_info_dict())

# Set Arguments to all uppercase
if param.param_type_name == 'argument':
param.opts[0] = param.opts[0].upper()

# This option has a short (-v) and long (--verbose) options
if len(param.opts) == 2:
opt1 = self.highlighter(param.opts[1])
opt2 = self.highlighter(param.opts[0])
else:
opt2 = self.highlighter(param.opts[0])
# Needs to be the Text() type because rich.Text doesn't mesh with string
opt1 = Text("")

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

options = Text(" ".join(reversed(param.opts)))
# secondary_opts are usually for flags --enable/--disable
if len(param.secondary_opts) == 1:
opt2 += Text("|") + self.highlighter(param.secondary_opts[0])

help_record = param.get_help_record(ctx)
help_message = ""
if help_record:
help_message = param.get_help_record(ctx)[-1]
help_message = Text(param.get_help_record(ctx)[-1])

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

if param.metavar:
options += f" {param.metavar}"
options_table.add_row(opt1, opt2, self.highlighter(help_message))

self.console.print(options_table)
9 changes: 7 additions & 2 deletions SoftLayer/CLI/virt/capacity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import os

import click
from SoftLayer.CLI.command import CommandLoader
from SoftLayer.CLI.command import OptionHighlighter

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


class CapacityCommands(click.MultiCommand):
class CapacityCommands(CommandLoader):
"""Loads module for capacity related commands.
Will automatically replace _ with - where appropriate.
Expand All @@ -19,8 +21,11 @@ class CapacityCommands(click.MultiCommand):
"""

def __init__(self, **attrs):
click.MultiCommand.__init__(self, **attrs)
CommandLoader.__init__(self, **attrs)
self.path = os.path.dirname(__file__)
self.highlighter = OptionHighlighter()
self.env = None
self.console = None

def list_commands(self, ctx):
"""List all sub-commands."""
Expand Down
9 changes: 7 additions & 2 deletions SoftLayer/CLI/virt/placementgroup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
import os

import click
from SoftLayer.CLI.command import CommandLoader
from SoftLayer.CLI.command import OptionHighlighter

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


class PlacementGroupCommands(click.MultiCommand):
class PlacementGroupCommands(CommandLoader):
"""Loads module for placement group related commands.
Currently the base command loader only supports going two commands deep.
So this small loader is required for going that third level.
"""

def __init__(self, **attrs):
click.MultiCommand.__init__(self, **attrs)
CommandLoader.__init__(self, **attrs)
self.path = os.path.dirname(__file__)
self.highlighter = OptionHighlighter()
self.env = None
self.console = None

def list_commands(self, ctx):
"""List all sub-commands."""
Expand Down
6 changes: 3 additions & 3 deletions SoftLayer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ def decode_stacked(document, pos=0, decoder=JSONDecoder()):


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

if theme == 'light':
return Console(theme=Theme(
Expand All @@ -490,7 +490,7 @@ def console_color_themes(theme):
"switch": "bold green4",
"default_option": "light_coral",
"option_keyword": "bold dark_cyan",
"args_keyword": "bold green4",
"args_keyword": "underline orange4",
"option_choices": "gold3",
})
)
Expand All @@ -507,7 +507,7 @@ def console_color_themes(theme):
"switch": "bold green",
"default_option": "light_pink1",
"option_keyword": "bold cyan",
"args_keyword": "bold green",
"args_keyword": "underline yellow",
"option_choices": "gold3",
})
)
Expand Down

0 comments on commit 186d2d5

Please sign in to comment.