1111import click
1212
1313from rich import box
14- from rich .console import Console
1514from rich .highlighter import RegexHighlighter
1615from rich .table import Table
1716from rich .text import Text
18- from rich .theme import Theme
1917
2018from SoftLayer .CLI import environment
2119
@@ -26,68 +24,58 @@ class OptionHighlighter(RegexHighlighter):
2624 r"(?P<switch>^\-\w)" , # single options like -v
2725 r"(?P<option>\-\-[\w\-]+)" , # long options like --verbose
2826 r"(?P<default_option>\[[^\]]+\])" , # anything between [], usually default options
29-
3027 ]
3128
3229
33- # Colors defined in https://rich.readthedocs.io/en/latest/_modules/rich/color.html#ColorType
34- SLCLI_THEME = Theme (
35- {
36- "option" : "bold cyan" ,
37- "switch" : "bold green" ,
38- "default_option" : "light_pink1" ,
39- "option_keyword" : "bold cyan" ,
40- "args_keyword" : "bold green"
41- }
42- )
43-
44-
4530class CommandLoader (click .MultiCommand ):
4631 """Loads module for click."""
4732
4833 def __init__ (self , * path , ** attrs ):
4934 click .MultiCommand .__init__ (self , ** attrs )
5035 self .path = path
51-
5236 self .highlighter = OptionHighlighter ()
53- self .console = Console (
54- theme = SLCLI_THEME
55- )
37+ self .env = None
38+ self .console = None
39+
40+ def ensure_env (self , ctx ):
41+ """ensures self.env is set"""
42+ if self .env is None :
43+ self .env = ctx .ensure_object (environment .Environment )
44+ self .env .load ()
45+ if self .console is None :
46+ self .console = self .env .console
5647
5748 def list_commands (self , ctx ):
5849 """List all sub-commands."""
59- env = ctx .ensure_object (environment .Environment )
60- env .load ()
61-
62- return sorted (env .list_commands (* self .path ))
50+ self .ensure_env (ctx )
51+ return sorted (self .env .list_commands (* self .path ))
6352
6453 def get_command (self , ctx , cmd_name ):
6554 """Get command for click."""
66- env = ctx .ensure_object (environment .Environment )
67- env .load ()
68-
55+ self .ensure_env (ctx )
6956 # Do alias lookup (only available for root commands)
7057 if len (self .path ) == 0 :
71- cmd_name = env .resolve_alias (cmd_name )
58+ cmd_name = self . env .resolve_alias (cmd_name )
7259
7360 new_path = list (self .path )
7461 new_path .append (cmd_name )
75- module = env .get_command (* new_path )
62+ module = self . env .get_command (* new_path )
7663 if isinstance (module , types .ModuleType ):
7764 return CommandLoader (* new_path , help = module .__doc__ or '' )
7865 else :
7966 return module
8067
8168 def format_usage (self , ctx : click .Context , formatter : click .formatting .HelpFormatter ) -> None :
8269 """Formats and colorizes the usage information."""
70+ self .ensure_env (ctx )
8371 pieces = self .collect_usage_pieces (ctx )
8472 for index , piece in enumerate (pieces ):
8573 if piece == "[OPTIONS]" :
86- pieces [index ] = "[bold cyan ][OPTIONS][/bold cyan ]"
74+ pieces [index ] = "[options ][OPTIONS][/]"
8775 elif piece == "COMMAND [ARGS]..." :
88- pieces [index ] = "[orange1 ]COMMAND[/orange1 ] [bold cyan ][ARGS][/bold cyan ] ..."
76+ pieces [index ] = "[command ]COMMAND[/] [args ][ARGS][/] ..."
8977
90- self .console .print (f"Usage: [bold red ]{ ctx .command_path } [/bold red ] { ' ' .join (pieces )} " )
78+ self .console .print (f"Usage: [path ]{ ctx .command_path } [/] { ' ' .join (pieces )} " )
9179
9280 def format_help_text (self , ctx : click .Context , formatter : click .formatting .HelpFormatter ) -> None :
9381 """Writes the help text"""
@@ -153,9 +141,9 @@ def format_commands(self, ctx, formatter):
153141 if len (commands ):
154142 for subcommand , cmd in commands :
155143 help_text = cmd .get_short_help_str (120 )
156- command_style = Text (f" { subcommand } " , style = "orange1 " )
144+ command_style = Text (f" { subcommand } " , style = "sub_command " )
157145 command_table .add_row (command_style , help_text )
158- self .console .print ("\n [bold orange1 ]Commands:[/]" )
146+ self .console .print ("\n [name_sub_command ]Commands:[/]" )
159147 self .console .print (command_table )
160148
161149
@@ -165,20 +153,28 @@ class SLCommand(click.Command):
165153 def __init__ (self , ** attrs ):
166154 click .Command .__init__ (self , ** attrs )
167155 self .highlighter = OptionHighlighter ()
168- self .console = Console (
169- theme = SLCLI_THEME
170- )
156+ self .env = None
157+ self .console = None
158+
159+ def ensure_env (self , ctx ):
160+ """ensures self.env is set"""
161+ if self .env is None :
162+ self .env = ctx .ensure_object (environment .Environment )
163+ self .env .load ()
164+ if self .console is None :
165+ self .console = self .env .console
171166
172167 def format_usage (self , ctx : click .Context , formatter : click .formatting .HelpFormatter ) -> None :
173168 """Formats and colorizes the usage information."""
169+ self .ensure_env (ctx )
174170 pieces = self .collect_usage_pieces (ctx )
175171 for index , piece in enumerate (pieces ):
176172 if piece == "[OPTIONS]" :
177- pieces [index ] = "[bold cyan ][OPTIONS][/bold cyan ]"
173+ pieces [index ] = "[options ][OPTIONS][/]"
178174 elif piece == "COMMAND [ARGS]..." :
179- pieces [index ] = "[orange1 ]COMMAND[/orange1 ] [bold cyan ][ARGS][/bold cyan ] ..."
175+ pieces [index ] = "[command ]COMMAND[/] [args ][ARGS][/] ..."
180176
181- self .console .print (f"Usage: [bold red ]{ ctx .command_path } [/bold red ] { ' ' .join (pieces )} " )
177+ self .console .print (f"Usage: [path ]{ ctx .command_path } [/] { ' ' .join (pieces )} " )
182178
183179 def format_help_text (self , ctx : click .Context , formatter : click .formatting .HelpFormatter ) -> None :
184180 """Writes the help text"""
0 commit comments