@@ -411,11 +411,23 @@ def test_subcommand_attachment() -> None:
411411 assert "child" not in root_subparsers ._name_parser_map
412412 assert "child_alias" not in root_subparsers ._name_parser_map
413413
414- ###############################
415- # Test error handling
416- ###############################
417414
418- # Verify ValueError when path is invalid (find_parser fails)
415+ def test_subcommand_attachment_errors () -> None :
416+ root_parser = Cmd2ArgumentParser (prog = "root" , description = "root command" )
417+ child_parser = Cmd2ArgumentParser (prog = "child" , description = "child command" )
418+
419+ # Verify ValueError when subcommands are not supported
420+ with pytest .raises (ValueError , match = "Command 'root' does not support subcommands" ):
421+ root_parser .attach_subcommand ([], "anything" , child_parser )
422+ with pytest .raises (ValueError , match = "Command 'root' does not support subcommands" ):
423+ root_parser .detach_subcommand ([], "anything" )
424+
425+ # Allow subcommands for the next tests
426+ root_parser .add_subparsers ()
427+
428+ # Verify ValueError when path is invalid (_find_parser() fails)
429+ with pytest .raises (ValueError , match = "Subcommand 'nonexistent' not found" ):
430+ root_parser .attach_subcommand (["nonexistent" ], "anything" , child_parser )
419431 with pytest .raises (ValueError , match = "Subcommand 'nonexistent' not found" ):
420432 root_parser .detach_subcommand (["nonexistent" ], "anything" )
421433
0 commit comments