Example #1
0
    def __init__(self, env, config, scope, parent_parser, legacy_parser=None):
        self._env = env
        self._config = config
        self._scope = scope

        # If True, no more registration is allowed on this parser.
        self._frozen = False

        # The argparser we use for actually parsing args.
        self._argparser = CustomArgumentParser(conflict_handler='resolve')

        # The argparser we use for formatting help messages.
        # We don't use self._argparser for this as it will have all options from enclosing scopes
        # registered on it too, which would create unnecessarily repetitive help messages.
        self._help_argparser = CustomArgumentParser(
            conflict_handler='resolve', formatter_class=PantsHelpFormatter)

        # If True, we have at least one option to show help for.
        self._has_help_options = False

        # Map of external to internal dest names. See docstring for _set_dest below.
        self._dest_forwardings = {}

        # A Parser instance, or None for the global scope parser.
        self._parent_parser = parent_parser

        # List of Parser instances.
        self._child_parsers = []

        if self._parent_parser:
            self._parent_parser._register_child_parser(self)

        # Handles legacy options on our behalf.
        self._legacy_options = LegacyOptions(
            scope, legacy_parser) if legacy_parser else None
Example #2
0
    def test_registration(self):
        optparser = optparse.OptionParser()
        legacy_options = LegacyOptions('compile.java', optparser)
        legacy_options.register(['--foo'], {'action': 'store_true'},
                                'compile_java_foo')
        legacy_options.register(['--bar'], {'type': long}, 'compile_java_bar')
        legacy_options.register(['--baz'], {'choices': ['xx', 'yy', 'zz']},
                                'compile_java_baz')
        legacy_options.register(['--qux'], {
            'type': int,
            'choices': [1, 2, 3],
            'action': 'append'
        }, 'compile_java_qux')
        legacy_options.register(['--corge'], {
            'type': int,
            'default': 55
        }, 'compile_java_corge')

        args = shlex.split(
            str('--compile-java-foo --compile-java-bar=33 --compile-java-baz=xx '
                '--compile-java-qux=1 --compile-java-qux=4'))
        opts, _ = optparser.parse_args(args)
        self.assertTrue(opts.compile_java_foo)
        self.assertEquals(opts.compile_java_bar, 33)
        self.assertEquals(opts.compile_java_baz, 'xx')
        self.assertEquals(opts.compile_java_qux,
                          [1, 4])  # Choices not enforced for non-string types.
        self.assertEquals(opts.compile_java_corge, 55)  # Defaults preserved.

        with pytest.raises(SystemExit):
            args = shlex.split(str(
                '--compile-java-baz=ww'))  # Choices enforced for string types.
            opts, _ = optparser.parse_args(args)