[ConfigArgumentMerger] Improve ConfigArgumentMerger compatibility with external callers (#17051)
This commit is contained in:
@@ -16,25 +16,37 @@ logger = logging.getLogger(__name__)
|
||||
class ConfigArgumentMerger:
|
||||
"""Handles merging of configuration file arguments with command-line arguments."""
|
||||
|
||||
def __init__(self, parser: argparse.ArgumentParser):
|
||||
def __init__(
|
||||
self,
|
||||
parser: argparse.ArgumentParser = None,
|
||||
boolean_actions: List[str] = None,
|
||||
):
|
||||
"""Initialize with list of store_true action names."""
|
||||
# NOTE: The current code does not support actions other than "store_true" and "store".
|
||||
self.parser = parser
|
||||
self.store_true_actions = [
|
||||
action.dest
|
||||
for action in parser._actions
|
||||
if isinstance(action, argparse._StoreTrueAction)
|
||||
]
|
||||
self.unsupported_actions = {
|
||||
a.dest: a
|
||||
for a in parser._actions
|
||||
if a.option_strings
|
||||
and not isinstance(a, argparse._StoreTrueAction)
|
||||
and not isinstance(a, argparse._StoreAction)
|
||||
and "--config" not in a.option_strings
|
||||
and "--help" not in a.option_strings
|
||||
and "-h" not in a.option_strings
|
||||
}
|
||||
if parser is not None:
|
||||
self.parser = parser
|
||||
self.store_true_actions = [
|
||||
action.dest
|
||||
for action in parser._actions
|
||||
if isinstance(action, argparse._StoreTrueAction)
|
||||
]
|
||||
self.unsupported_actions = {
|
||||
a.dest: a
|
||||
for a in parser._actions
|
||||
if a.option_strings
|
||||
and not isinstance(a, argparse._StoreTrueAction)
|
||||
and not isinstance(a, argparse._StoreAction)
|
||||
and "--config" not in a.option_strings
|
||||
and "--help" not in a.option_strings
|
||||
and "-h" not in a.option_strings
|
||||
}
|
||||
elif boolean_actions is not None:
|
||||
# Legacy interface for compatibility
|
||||
self.store_true_actions = boolean_actions
|
||||
self.unsupported_actions = {}
|
||||
else:
|
||||
self.store_true_actions = []
|
||||
self.unsupported_actions = {}
|
||||
|
||||
def merge_config_with_args(self, cli_args: List[str]) -> List[str]:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user