Exemplo n.º 1
0
    def _to_goptioncontext(self, values):
        if self.description:
            parameter_string = self.usage + " - " + self.description
        else:
            parameter_string = self.usage
        context = _gi.OptionContext(parameter_string)
        context.set_help_enabled(self.help_enabled)
        context.set_ignore_unknown_options(self.ignore_unknown_options)

        for option_group in self.option_groups:
            if isinstance(option_group, _gi.OptionGroup):
                g_group = option_group
            else:
                g_group = option_group.get_option_group(self)
            context.add_group(g_group)

        def callback(option_name, option_value, group):
            if option_name.startswith('--'):
                opt = self._long_opt[option_name]
            else:
                opt = self._short_opt[option_name]
            opt.process(option_name, option_value, values, self)

        main_group = _gi.OptionGroup(None, None, None, callback)
        main_entries = []
        for option in self.option_list:
            main_entries.extend(option._to_goptionentries())
        main_group.add_entries(main_entries)
        context.set_main_group(main_group)

        return context
Exemplo n.º 2
0
    def _to_goptiongroup(self, parser):
        def callback(option_name, option_value, group):
            if option_name.startswith('--'):
                opt = self._long_opt[option_name]
            else:
                opt = self._short_opt[option_name]

            try:
                opt.process(option_name, option_value, self.values, parser)
            except OptionValueError:
                error = sys.exc_info()[1]
                gerror = GError(str(error))
                gerror.domain = OPTION_CONTEXT_ERROR_QUARK
                gerror.code = GLib.OptionError.BAD_VALUE
                gerror.message = str(error)
                raise gerror

        group = _gi.OptionGroup(self.name, self.description,
                                self.help_description, callback)
        if self.translation_domain:
            group.set_translation_domain(self.translation_domain)

        entries = []
        for option in self.option_list:
            entries.extend(option._to_goptionentries())

        group.add_entries(entries)

        return group