From 6940848f0bc9820d8f32667e4d0f9b73b68d2003 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 4 Apr 2019 13:51:26 -0700 Subject: [PATCH] coredata: Move argparse action getterin into BuiltinOption class --- mesonbuild/coredata.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 347af142a..180f7e5c4 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -672,14 +672,6 @@ def get_builtin_option_description(optname): else: raise RuntimeError('Tried to get the description for an unknown builtin option \'%s\'.' % optname) -def get_builtin_option_action(optname): - default = builtin_options[optname].default - if default is True: - return 'store_false' - elif default is False: - return 'store_true' - return None - def get_builtin_option_default(optname, prefix=''): if optname in builtin_options: o = builtin_options[optname] @@ -700,9 +692,15 @@ def get_builtin_option_cmdline_name(name): return '--' + name.replace('_', '-') def add_builtin_argument(p, name): + try: + builtin = builtin_options[name] + except KeyError: + raise RuntimeError('Tried to get attribute of unknown builtin option "{}"'.format(name)) + kwargs = {} + c = get_builtin_option_choices(name) - b = get_builtin_option_action(name) + b = builtin.argparse_action() h = get_builtin_option_description(name) if not b: h = h.rstrip('.') + ' (default: %s).' % get_builtin_option_default(name) @@ -771,6 +769,13 @@ class BuiltinOption(Generic[_U]): keywords['choices'] = self.choices return self.opt_type(name, self.description, **keywords) + def argparse_action(self) -> Optional[str]: + if self.default is True: + return 'store_false' + elif self.default is False: + return 'store_true' + return None + builtin_options = { 'buildtype': BuiltinOption(UserComboOption, 'Build type to use', 'debug',