@@ -922,6 +922,10 @@ def format_usage(self):
922922 def __call__ (self , parser , namespace , values , option_string = None ):
923923 raise NotImplementedError ('.__call__() not defined' )
924924
925+ def _check_compatibility (self , container ):
926+ """Check whether the action is compatible with the container."""
927+ pass
928+
925929
926930class BooleanOptionalAction (Action ):
927931 def __init__ (self ,
@@ -954,15 +958,16 @@ def __init__(self,
954958
955959
956960 def __call__ (self , parser , namespace , values , option_string = None ):
957- if '-' not in parser .prefix_chars :
958- raise ValueError ("BooleanOptionalAction requires '-' in parser's prefix_chars" )
959-
960961 if option_string in self .option_strings :
961962 setattr (namespace , self .dest , not option_string .startswith ('--no-' ))
962963
963964 def format_usage (self ):
964965 return ' | ' .join (self .option_strings )
965966
967+ def _check_compatibility (self , container ):
968+ if '-' not in container .prefix_chars :
969+ raise ValueError (f"BooleanOptionalAction requires '-' in prefix_chars." )
970+
966971
967972class _StoreAction (Action ):
968973
@@ -1545,6 +1550,9 @@ def add_argument(self, *args, **kwargs):
15451550 if not action .option_strings and action .nargs == 0 :
15461551 raise ValueError (f'action { action_name !r} is not valid for positional arguments' )
15471552
1553+ # raise an error if the action is not compatible with the parser
1554+ action ._check_compatibility (self )
1555+
15481556 # raise an error if the action type is not callable
15491557 type_func = self ._registry_get ('type' , action .type , action .type )
15501558 if not callable (type_func ):
0 commit comments