2424)
2525USAGE = (
2626 "aws [options] <command> <subcommand> [<subcommand> ...] [parameters]\n "
27- "%s" % HELP_BLURB
27+ f" { HELP_BLURB } "
2828)
2929
3030
@@ -42,9 +42,7 @@ class CommandAction(argparse.Action):
4242
4343 def __init__ (self , option_strings , dest , command_table , ** kwargs ):
4444 self .command_table = command_table
45- super (CommandAction , self ).__init__ (
46- option_strings , dest , choices = self .choices , ** kwargs
47- )
45+ super ().__init__ (option_strings , dest , choices = self .choices , ** kwargs )
4846
4947 def __call__ (self , parser , namespace , values , option_string = None ):
5048 setattr (namespace , self .dest , values )
@@ -65,10 +63,6 @@ def choices(self, val):
6563class CLIArgParser (argparse .ArgumentParser ):
6664 Formatter = argparse .RawTextHelpFormatter
6765
68- # When displaying invalid choice error messages,
69- # this controls how many options to show per line.
70- ChoicesPerLine = 2
71-
7266 def _check_value (self , action , value ):
7367 """
7468 It's probably not a great idea to override a "hidden" method
@@ -77,24 +71,17 @@ def _check_value(self, action, value):
7771 """
7872 # converted value must be one of the choices (if specified)
7973 if action .choices is not None and value not in action .choices :
80- msg = ['Invalid choice, valid choices are:\n ' ]
81- for i in range (len (action .choices ))[:: self .ChoicesPerLine ]:
82- current = []
83- for choice in action .choices [i : i + self .ChoicesPerLine ]:
84- current .append ('%-40s' % choice )
85- msg .append (' | ' .join (current ))
74+ msg = [f"Found invalid choice '{ value } '\n " ]
8675 possible = get_close_matches (value , action .choices , cutoff = 0.8 )
8776 if possible :
88- extra = ['\n \n Invalid choice: %r, maybe you meant:\n ' % value ]
77+ extra = ['Maybe you meant:\n ' ]
8978 for word in possible :
90- extra .append (' * %s' % word )
79+ extra .append (f ' * { word } ' )
9180 msg .extend (extra )
9281 raise argparse .ArgumentError (action , '\n ' .join (msg ))
9382
9483 def parse_known_args (self , args , namespace = None ):
95- parsed , remaining = super (CLIArgParser , self ).parse_known_args (
96- args , namespace
97- )
84+ parsed , remaining = super ().parse_known_args (args , namespace )
9885 terminal_encoding = getattr (sys .stdin , 'encoding' , 'utf-8' )
9986 if terminal_encoding is None :
10087 # In some cases, sys.stdin won't have an encoding set,
@@ -126,8 +113,8 @@ def error(self, message):
126113 should raise an exception.
127114 """
128115 usage_message = self .format_usage ()
129- error_message = f'{ self .prog } : error : { message } \n '
130- raise ArgParseException (f'{ usage_message } \n { error_message } ' )
116+ error_message = f'{ self .prog } : [ERROR] : { message } '
117+ raise ArgParseException (f'{ error_message } \n \n { usage_message } ' )
131118
132119
133120class MainArgParser (CLIArgParser ):
@@ -141,7 +128,7 @@ def __init__(
141128 argument_table ,
142129 prog = None ,
143130 ):
144- super (MainArgParser , self ).__init__ (
131+ super ().__init__ (
145132 formatter_class = self .Formatter ,
146133 add_help = False ,
147134 conflict_handler = 'resolve' ,
@@ -154,7 +141,7 @@ def __init__(
154141 def _create_choice_help (self , choices ):
155142 help_str = ''
156143 for choice in sorted (choices ):
157- help_str += '* %s \n ' % choice
144+ help_str += f '* { choice } \n '
158145 return help_str
159146
160147 def _build (self , command_table , version_string , argument_table ):
@@ -174,7 +161,7 @@ def _build(self, command_table, version_string, argument_table):
174161
175162class ServiceArgParser (CLIArgParser ):
176163 def __init__ (self , operations_table , service_name ):
177- super (ServiceArgParser , self ).__init__ (
164+ super ().__init__ (
178165 formatter_class = argparse .RawTextHelpFormatter ,
179166 add_help = False ,
180167 conflict_handler = 'resolve' ,
@@ -196,7 +183,7 @@ def __init__(self, argument_table, command_table=None):
196183 # command_table is an optional subcommand_table. If it's passed
197184 # in, then we'll update the argparse to parse a 'subcommand' argument
198185 # and populate the choices field with the command table keys.
199- super (ArgTableArgParser , self ).__init__ (
186+ super ().__init__ (
200187 formatter_class = self .Formatter ,
201188 add_help = False ,
202189 usage = USAGE ,
@@ -224,9 +211,7 @@ def parse_known_args(self, args, namespace=None):
224211 namespace .help = 'help'
225212 return namespace , []
226213 else :
227- return super (ArgTableArgParser , self ).parse_known_args (
228- args , namespace
229- )
214+ return super ().parse_known_args (args , namespace )
230215
231216
232217class SubCommandArgParser (ArgTableArgParser ):
@@ -239,9 +224,7 @@ class SubCommandArgParser(ArgTableArgParser):
239224 """
240225
241226 def parse_known_args (self , args , namespace = None ):
242- parsed_args , remaining = super (
243- SubCommandArgParser , self
244- ).parse_known_args (args , namespace )
227+ parsed_args , remaining = super ().parse_known_args (args , namespace )
245228 if getattr (parsed_args , 'subcommand' , None ) is not None :
246229 new_args = self ._remove_subcommand (args , parsed_args )
247230 return new_args , parsed_args .subcommand
0 commit comments