build/parser: adding a method to search default value for an argument into ArgParse._actions

This commit is contained in:
Gwenhael Goavec-Merou 2023-01-11 18:31:31 +01:00
parent c834387965
commit 35f4913588
1 changed files with 22 additions and 0 deletions

View File

@ -174,6 +174,8 @@ class LiteXArgumentParser(argparse.ArgumentParser):
# When platform is None try to search for a user input
if self._platform is None:
platform = self.get_value_from_key("--platform", None)
if platform is None: # no user selection: try default
platform = self.get_default_value_from_actions("platform", None)
if platform is not None:
self.set_platform(importlib.import_module(platform).Platform)
self.add_target_group()
@ -244,3 +246,23 @@ class LiteXArgumentParser(argparse.ArgumentParser):
except IndexError:
value = default
return value
def get_default_value_from_actions(self, key, default=None):
"""
search key into ArgumentParser _actions list
Parameters
==========
key: str
key to search
default: str
default value when key is not in _actions list
Return
======
default value or default when key is not present
"""
for act in self._actions:
if act.dest == key:
return act.default
return default