MCPcopy Index your code
hub / github.com/aws/aws-cli / validate_cli_command

Method validate_cli_command

tests/functional/docs/test_examples.py:263–351  ·  view source on GitHub ↗

Validates the syntax of a CLI command by actually parsing it, which catches: - Invalid service names - Invalid operation names - Missing required arguments - Invalid argument names - Malformed argument syntax

(self, command, filename)

Source from the content-addressed store, hash-verified

261 )
262
263 def validate_cli_command(self, command, filename):
264 """
265 Validates the syntax of a CLI command by actually parsing it, which catches:
266 - Invalid service names
267 - Invalid operation names
268 - Missing required arguments
269 - Invalid argument names
270 - Malformed argument syntax
271 """
272 try:
273 command_parts = shlex.split(command)[1:]
274 # Strip newlines from arguments that may have been included due to
275 # backslash line continuations in the RST examples
276 command_parts = [part.lstrip('\n') for part in command_parts]
277 except Exception as e:
278 raise AssertionError(
279 "Failed to parse this example as shell command: %s\n\n"
280 "Error:\n%s\n" % (command, e)
281 )
282
283 # TODO - for now skip validation if command uses
284 # --cli-input-json, --cli-input-yaml, or --generate-cli-skeleton
285 if any(
286 arg.startswith('--cli-input-json')
287 or arg.startswith('--cli-input-yaml')
288 or arg == '--generate-cli-skeleton'
289 for arg in command_parts
290 ):
291 return
292
293 try:
294 # Parse global args
295 parsed_args, remaining = self._main_parser.parse_known_args(
296 command_parts
297 )
298 service_name = parsed_args.command
299
300 # Get the service command
301 service_cmd = self._service_command_table[service_name]
302
303 # Create the service parser
304 cmd_table = service_cmd.create_help_command().command_table
305 if not cmd_table:
306 # Top-level commands without sub-operations (e.g. login)
307 return
308 service_parser = ServiceArgParser(
309 operations_table=cmd_table, service_name=service_name
310 )
311
312 # Parse the operation
313 parsed_operation, remaining_args = service_parser.parse_known_args(
314 remaining
315 )
316
317 if not hasattr(parsed_operation, 'operation'):
318 # Not a standard operation (might be help, wait, etc.)
319 return
320

Callers 1

Calls 6

parse_known_argsMethod · 0.95
ServiceArgParserClass · 0.90
ArgTableArgParserClass · 0.90
lstripMethod · 0.45
parse_known_argsMethod · 0.45
create_help_commandMethod · 0.45

Tested by

no test coverage detected