@@ -1084,7 +1084,15 @@ def error(self, message: str, ex: Optional[Exception] = None) -> NoReturn:
10841084 elif debug_mode_active ():
10851085 self ._logger .debug ("Debug enabled, thus raising exception instead of exit." )
10861086 raise argument_error (message ) from ex
1087- self .print_usage (sys .stderr )
1087+
1088+ parser = getattr (ex , "subcommand_parser" , None ) or self
1089+ parser .print_usage (sys .stderr )
1090+
1091+ help_action = next ((a for a in parser ._actions if isinstance (a , argparse ._HelpAction )), None )
1092+ if help_action :
1093+ prog = parser .prog .replace (" [options]" , "" )
1094+ sys .stderr .write (f"tip: For details of accepted options run: { prog } { help_action .option_strings [- 1 ]} \n " )
1095+
10881096 sys .stderr .write (f"error: { message } \n " )
10891097 self .exit (2 )
10901098
@@ -1123,7 +1131,7 @@ def check_required(cfg, parser, prefix):
11231131 raise TypeError
11241132 except (KeyError , TypeError ) as ex :
11251133 raise TypeError (
1126- f'Key " { prefix } { reqkey } " is required but not included in config object or its value is None.'
1134+ f"Option ' { prefix } { reqkey } ' is required but not provided or its value is None."
11271135 ) from ex
11281136 subcommand , subparser = _ActionSubCommands .get_subcommand (parser , cfg , fail_no_subcommand = False )
11291137 if subcommand is not None and subparser is not None :
@@ -1156,24 +1164,25 @@ def check_values(cfg):
11561164 else :
11571165 if isinstance (parent_action , _ActionSubCommands ) and "." in key :
11581166 subcommand , subkey = split_key_root (key )
1159- raise NSKeyError (f"Subcommand '{ subcommand } ' does not accept nested key '{ subkey } '" )
1167+ ex = NSKeyError (f"Subcommand '{ subcommand } ' does not accept option '{ subkey } '" )
1168+ ex .subcommand_parser = parent_action ._name_parser_map [subcommand ]
1169+ raise ex
11601170 group_key = next ((g for g in self .groups if key .startswith (g + "." )), None )
11611171 if group_key :
11621172 subkey = key [len (group_key ) + 1 :]
1163- raise NSKeyError (f"Group '{ group_key } ' does not accept nested key '{ subkey } '" )
1164- raise NSKeyError (f"Key '{ key } ' is not expected" )
1173+ raise NSKeyError (f"Group '{ group_key } ' does not accept option '{ subkey } '" )
1174+ if self ._subcommands_action :
1175+ if cfg .get (self ._subcommands_action .dest ):
1176+ subcommand = f"'{ cfg [self ._subcommands_action .dest ]} '"
1177+ else :
1178+ subcommand = f"{{{ list (self ._subcommands_action .choices )[0 ]} ,...}}"
1179+ raise NSKeyError (f"Option '{ key } ' is not accepted before subcommand { subcommand } " )
1180+ raise NSKeyError (f"Option '{ key } ' is not accepted" )
11651181
1166- try :
1167- with parser_context (load_value_mode = self .parser_mode ):
1168- check_values (cfg )
1169- if not skip_required and not lenient_check .get ():
1170- check_required (cfg , self , prefix )
1171- except (TypeError , KeyError ) as ex :
1172- prefix = "Validation failed: "
1173- message = ex .args [0 ]
1174- if prefix not in message :
1175- message = prefix + message
1176- raise type (ex )(message ) from ex
1182+ with parser_context (load_value_mode = self .parser_mode ):
1183+ check_values (cfg )
1184+ if not skip_required and not lenient_check .get ():
1185+ check_required (cfg , self , prefix )
11771186
11781187 def add_instantiator (
11791188 self ,
0 commit comments