@@ -486,11 +486,12 @@ def get_all_options(
486486 drop_default = False ,
487487 add_extra_args_fn : Optional [Callable [[_BeamArgumentParser ], None ]] = None ,
488488 retain_unknown_options = False ,
489- display_warnings = False ) -> Dict [str , Any ]:
489+ display_warnings = False ,
490+ hierarchy_only = False ,
491+ ) -> Dict [str , Any ]:
490492 """Returns a dictionary of all defined arguments.
491493
492- Returns a dictionary of all defined arguments (arguments that are defined in
493- any subclass of PipelineOptions) into a dictionary.
494+ Returns a dictionary of all defined arguments into a dictionary.
494495
495496 Args:
496497 drop_default: If set to true, options that are equal to their default
@@ -500,6 +501,9 @@ def get_all_options(
500501 retain_unknown_options: If set to true, options not recognized by any
501502 known pipeline options class will still be included in the result. If
502503 set to false, they will be discarded.
504+ hierarchy_only: If set to true, only returns options defined in this class
505+ and its super classes only. Otherwise, arguments that are defined in
506+ any subclass of PipelineOptions are returned (default).
503507
504508 Returns:
505509 Dictionary of all args and values.
@@ -510,8 +514,13 @@ def get_all_options(
510514 # instance of each subclass to avoid conflicts.
511515 subset = {}
512516 parser = _BeamArgumentParser (allow_abbrev = False )
513- for cls in PipelineOptions .__subclasses__ ():
514- subset .setdefault (str (cls ), cls )
517+ if not hierarchy_only :
518+ for cls in PipelineOptions .__subclasses__ ():
519+ subset .setdefault (str (cls ), cls )
520+ else :
521+ for cls in self .__class__ .__mro__ :
522+ if issubclass (cls , PipelineOptions ):
523+ subset .setdefault (str (cls ), cls )
515524 for cls in subset .values ():
516525 cls ._add_argparse_args (parser ) # pylint: disable=protected-access
517526 if add_extra_args_fn :
@@ -562,7 +571,7 @@ def add_new_arg(arg, **kwargs):
562571 continue
563572 parsed_args , _ = parser .parse_known_args (self ._flags )
564573 else :
565- if unknown_args :
574+ if unknown_args and not hierarchy_only :
566575 _LOGGER .warning ("Discarding unparseable args: %s" , unknown_args )
567576 parsed_args = known_args
568577 result = vars (parsed_args )
@@ -580,7 +589,7 @@ def add_new_arg(arg, **kwargs):
580589 if overrides :
581590 if retain_unknown_options :
582591 result .update (overrides )
583- else :
592+ elif not hierarchy_only :
584593 _LOGGER .warning ("Discarding invalid overrides: %s" , overrides )
585594
586595 return result
0 commit comments