build/parser: overrides set_defaults and applying default values just before args_parse()

This commit is contained in:
Gwenhael Goavec-Merou 2022-12-05 20:37:12 +01:00
parent c0f31dc843
commit 7eed962661
1 changed files with 20 additions and 0 deletions

View File

@ -32,6 +32,9 @@ class LiteXArgumentParser(argparse.ArgumentParser):
toolchain used at build time toolchain used at build time
_default_toolchain: str _default_toolchain: str
toolchain to use by default or when no selection is done by the user toolchain to use by default or when no selection is done by the user
_args_default: dict
couple argument name / default value to apply just before to call
parse_args()
""" """
def __init__(self, platform=None, **kwargs): def __init__(self, platform=None, **kwargs):
""" """
@ -55,6 +58,7 @@ class LiteXArgumentParser(argparse.ArgumentParser):
self._toolchain = None self._toolchain = None
self._target_group = None self._target_group = None
self._logging_group = None self._logging_group = None
self._args_default = {}
if platform is not None: if platform is not None:
self.set_platform(platform) self.set_platform(platform)
self.add_target_group() self.add_target_group()
@ -111,6 +115,18 @@ class LiteXArgumentParser(argparse.ArgumentParser):
self._logging_group.add_argument("--log-filename", default=None, help="Logging filename.") self._logging_group.add_argument("--log-filename", default=None, help="Logging filename.")
self._logging_group.add_argument("--log-level", default="info", help="Logging level: debug, info (default), warning error or critical.") self._logging_group.add_argument("--log-level", default="info", help="Logging level: debug, info (default), warning error or critical.")
def set_defaults(self, **kwargs):
"""
Overrides argparse.ArgumentParser.set_defaults. Used to delay default
values application
Parameters
==========
kwargs: dict
couple argument name / default value
"""
self._args_default.update(kwargs)
@property @property
def builder_argdict(self): def builder_argdict(self):
""" """
@ -178,6 +194,10 @@ class LiteXArgumentParser(argparse.ArgumentParser):
if cpu_cls is not None and hasattr(cpu_cls, "args_fill"): if cpu_cls is not None and hasattr(cpu_cls, "args_fill"):
cpu_cls.args_fill(self) cpu_cls.args_fill(self)
# Injects arguments default values
if len(self._args_default):
argparse.ArgumentParser.set_defaults(self, **self._args_default)
# Parse args. # Parse args.
self._args = argparse.ArgumentParser.parse_args(self, args, namespace) self._args = argparse.ArgumentParser.parse_args(self, args, namespace)