diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2019-04-04 14:17:52 -0700 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2019-04-04 15:02:20 -0700 |
commit | 863de725990a944380df6ecf79b43f1a51718489 (patch) | |
tree | 213bfc5b2e78a74407813f197147e5235688876f /mesonbuild/coredata.py | |
parent | 1d7eda5fc8aef2938c9f66058fe706d98c32fa62 (diff) | |
download | meson-863de725990a944380df6ecf79b43f1a51718489.zip meson-863de725990a944380df6ecf79b43f1a51718489.tar.gz meson-863de725990a944380df6ecf79b43f1a51718489.tar.bz2 |
coredata: Fold add_builtin_argument into BuiltinOption
It really is a method of the BuiltinOption class, not a standalone
function.
Diffstat (limited to 'mesonbuild/coredata.py')
-rw-r--r-- | mesonbuild/coredata.py | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 84e6136..fc2ab88 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -654,32 +654,10 @@ def save(obj, build_dir): os.replace(tempfilename, filename) return filename -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 = builtin.argparse_choices() - b = builtin.argparse_action() - h = builtin.description - if not b: - h = '{} (default: {}).'.format(h.rstrip('.'), builtin.prefixed_default(name)) - else: - kwargs['action'] = b - if c and not b: - kwargs['choices'] = c - kwargs['default'] = argparse.SUPPRESS - kwargs['dest'] = name - - cmdline_name = builtin.argparse_name_to_arg(name) - p.add_argument(cmdline_name, help=h, **kwargs) def register_builtin_arguments(parser): - for n in builtin_options: - add_builtin_argument(parser, n) + for n, b in builtin_options.items(): + b.add_to_argparse(n, parser) parser.add_argument('-D', action='append', dest='projectoptions', default=[], metavar="option", help='Set the value of an option, can be used several times to set multiple options.') @@ -732,14 +710,14 @@ class BuiltinOption(Generic[_U]): keywords['choices'] = self.choices return self.opt_type(name, self.description, **keywords) - def argparse_action(self) -> Optional[str]: + def _argparse_action(self) -> Optional[str]: if self.default is True: return 'store_false' elif self.default is False: return 'store_true' return None - def argparse_choices(self) -> Any: + def _argparse_choices(self) -> Any: if self.opt_type is UserBooleanOption: return [True, False] elif self.opt_type is UserFeatureOption: @@ -762,6 +740,24 @@ class BuiltinOption(Generic[_U]): pass return self.default + def add_to_argparse(self, name: str, parser: argparse.ArgumentParser) -> None: + kwargs = {} + + c = self._argparse_choices() + b = self._argparse_action() + h = self.description + if not b: + h = '{} (default: {}).'.format(h.rstrip('.'), self.prefixed_default(name)) + else: + kwargs['action'] = b + if c and not b: + kwargs['choices'] = c + kwargs['default'] = argparse.SUPPRESS + kwargs['dest'] = name + + cmdline_name = self.argparse_name_to_arg(name) + parser.add_argument(cmdline_name, help=h, **kwargs) + builtin_options = { 'buildtype': BuiltinOption(UserComboOption, 'Build type to use', 'debug', |