Skip to content

Commit 74fadaa

Browse files
authored
Merge pull request #9457 from hssyoo/reduce-invalid-choice-output
[v2] Reduce invalid choice output
2 parents 543760a + be26c8f commit 74fadaa

File tree

8 files changed

+13
-22
lines changed

8 files changed

+13
-22
lines changed

awscli/argparser.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ def choices(self, val):
6565
class CLIArgParser(argparse.ArgumentParser):
6666
Formatter = argparse.RawTextHelpFormatter
6767

68-
# When displaying invalid choice error messages,
69-
# this controls how many options to show per line.
70-
ChoicesPerLine = 2
71-
7268
def _check_value(self, action, value):
7369
"""
7470
It's probably not a great idea to override a "hidden" method
@@ -77,15 +73,10 @@ def _check_value(self, action, value):
7773
"""
7874
# converted value must be one of the choices (if specified)
7975
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))
76+
msg = [f"Found invalid choice '{value}'\n"]
8677
possible = get_close_matches(value, action.choices, cutoff=0.8)
8778
if possible:
88-
extra = ['\n\nInvalid choice: %r, maybe you meant:\n' % value]
79+
extra = ['Maybe you meant:\n']
8980
for word in possible:
9081
extra.append(' * %s' % word)
9182
msg.extend(extra)
@@ -126,8 +117,8 @@ def error(self, message):
126117
should raise an exception.
127118
"""
128119
usage_message = self.format_usage()
129-
error_message = f'{self.prog}: error: {message}\n'
130-
raise ArgParseException(f'{usage_message}\n{error_message}')
120+
error_message = f'{self.prog}: error: {message}'
121+
raise ArgParseException(f'{error_message}\n\n{usage_message}')
131122

132123

133124
class MainArgParser(CLIArgParser):

tests/functional/docs/test_help_output.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def assert_command_does_not_exist(self, service, command):
217217
self.assertEqual(cr, 252)
218218
# We should see an error message complaining about
219219
# an invalid choice because the operation has been removed.
220-
self.assertIn('argument operation: Invalid choice', stderr.getvalue())
220+
self.assertIn('argument operation: Found invalid choice', stderr.getvalue())
221221

222222
def test_ses_deprecated_commands(self):
223223
self.driver.main(['ses', 'help'])

tests/functional/ec2instanceconnect/test_ssh.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def test_command_fails_when_eice_connection_type_and_no_private_ip(
886886
"test",
887887
],
888888
252,
889-
"argument --connection-type: Invalid choice, valid choices are:",
889+
"argument --connection-type: Found invalid choice 'test'",
890890
id='Failure: Customer must provide connection-type when IP defined',
891891
),
892892
pytest.param(

tests/functional/kinesis/test_remove_operations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
def test_subscribe_to_shard_removed():
1717
result = CLIRunner().run(['kinesis', 'subscribe-to-shard', 'help'])
18-
expected_error = 'argument operation: Invalid choice, valid choices are:'
18+
expected_error = "argument operation: Found invalid choice 'subscribe-to-shard'"
1919
assert expected_error in result.stderr

tests/functional/lex/test_remove_operations.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
def test_start_conversation_removed():
1717
result = CLIRunner().run(['lexv2-runtime', 'start-conversation', 'help'])
18-
expected_error = 'argument operation: Invalid choice, valid choices are:'
18+
expected_error = "argument operation: Found invalid choice 'start-conversation'"
1919
assert expected_error in result.stderr

tests/functional/servicecatalog/test_generate_createproduct.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def test_invalid_product_type(self):
198198
self.assert_params_for_cmd(
199199
self.cmd_line,
200200
expected_rc=252,
201-
stderr_contains='--product-type: Invalid choice',
201+
stderr_contains='--product-type: Found invalid choice',
202202
)
203203

204204
def test_generate_product_invalid_provisioning_artifact_type(self):
@@ -208,5 +208,5 @@ def test_generate_product_invalid_provisioning_artifact_type(self):
208208
self.assert_params_for_cmd(
209209
self.cmd_line,
210210
expected_rc=252,
211-
stderr_contains='--provisioning-artifact-type: Invalid choice',
211+
stderr_contains='--provisioning-artifact-type: Found invalid choice',
212212
)

tests/functional/servicecatalog/test_generate_createprovisioningartifact.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_generate_provisioning_artifact_invalid_pa_type(self):
105105
self.assert_params_for_cmd(
106106
self.cmd_line,
107107
expected_rc=252,
108-
stderr_contains='--provisioning-artifact-type: Invalid choice',
108+
stderr_contains='--provisioning-artifact-type: Found invalid choice',
109109
)
110110

111111
def test_generate_provisioning_artifact_missing_file_path(self):

tests/unit/test_clidriver.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,10 @@ def test_unknown_command_suggests_help(self):
493493
)
494494
self.assertEqual(rc, 252)
495495
# Tell the user what went wrong.
496-
self.assertIn("Invalid choice: 'list-objecst'", self.stderr.getvalue())
496+
self.assertIn("Found invalid choice 'list-objecst'", self.stderr.getvalue())
497497
# Offer the user a suggestion.
498498
self.assertIn(
499-
"maybe you meant:\n\n * list-objects", self.stderr.getvalue()
499+
"Maybe you meant:\n\n * list-objects", self.stderr.getvalue()
500500
)
501501

502502

0 commit comments

Comments
 (0)