コード例 #1
0
    def test_fromfile(self):
        ohi = HelpInfoExtracter('').get_option_help_info([], {})
        self.assertFalse(ohi.fromfile)

        kwargs = {'fromfile': False}
        ohi = HelpInfoExtracter('').get_option_help_info([], kwargs)
        self.assertFalse(ohi.fromfile)

        kwargs = {'fromfile': True}
        ohi = HelpInfoExtracter('').get_option_help_info([], kwargs)
        self.assertTrue(ohi.fromfile)
コード例 #2
0
ファイル: help_formatter.py プロジェクト: mcguigan/pants
  def format_options(self, scope, description, option_registrations_iter):
    """Return a help message for the specified options.

    :param option_registrations_iter: An iterator over (args, kwargs) pairs, as passed in to
                                      options registration.
    """
    oshi = HelpInfoExtracter(self._scope).get_option_scope_help_info(option_registrations_iter)
    lines = []

    def add_option(ohis, *, category=None):
      lines.append('')
      display_scope = scope or 'Global'
      if category:
        lines.append(self._maybe_green(f'{display_scope} {category}:'))
      else:
        lines.append(self._maybe_green(f'{display_scope}:'))
        if description:
          lines.append(description)
      lines.append(' ')
      if not ohis:
        lines.append('No options available.')
        return
      for ohi in ohis:
        lines.extend(self.format_option(ohi))

    add_option(oshi.basic)
    if self._show_recursive:
      add_option(oshi.recursive, category='recursive')
    if self._show_advanced:
      add_option(oshi.advanced, category='advanced')
    return [*lines, '\n']
コード例 #3
0
    def format_options(self, scope, description, option_registrations_iter):
        """Return a help message for the specified options.

    :API: public

    :param option_registrations_iter: An iterator over (args, kwargs) pairs, as passed in to
                                      options registration.
    """
        oshi = HelpInfoExtracter(
            self._scope).get_option_scope_help_info(option_registrations_iter)
        lines = []

        def add_option(category, ohis):
            if ohis:
                lines.append('')
                display_scope = scope or 'Global'
                if category:
                    lines.append(
                        self._maybe_blue('{} {} options:'.format(
                            display_scope, category)))
                else:
                    lines.append(
                        self._maybe_blue('{} options:'.format(display_scope)))
                    if description:
                        lines.append(description)
                lines.append(' ')
                for ohi in ohis:
                    lines.extend(self.format_option(ohi))

        add_option('', oshi.basic)
        if self._show_recursive:
            add_option('recursive', oshi.recursive)
        if self._show_advanced:
            add_option('advanced', oshi.advanced)
        return lines
コード例 #4
0
 def do_test(args, kwargs, expected_display_args, expected_scoped_cmd_line_args):
   # The scoped and unscoped args are the same in global scope.
   expected_unscoped_cmd_line_args = expected_scoped_cmd_line_args
   ohi = HelpInfoExtracter('').get_option_help_info(args, kwargs)
   self.assertListEqual(expected_display_args, ohi.display_args)
   self.assertListEqual(expected_scoped_cmd_line_args, ohi.scoped_cmd_line_args)
   self.assertListEqual(expected_unscoped_cmd_line_args, ohi.unscoped_cmd_line_args)
コード例 #5
0
def test_deprecated():
    kwargs = {"removal_version": "999.99.9", "removal_hint": "do not use this"}
    ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs)
    assert "999.99.9" == ohi.removal_version
    assert "do not use this" == ohi.removal_hint
    assert ohi.deprecated_message is not None
    assert ohi.deprecation_active
コード例 #6
0
ファイル: help_formatter.py プロジェクト: wisechengyi/pants
    def format_options(self, scope, description, option_registrations_iter):
        """Return a help message for the specified options.

        :param scope: The options scope.
        :param description: The description of the scope.
        :param option_registrations_iter: An iterator over (args, kwargs) pairs, as passed in to
                                          options registration.
        """
        oshi = HelpInfoExtracter(
            self._scope).get_option_scope_help_info(option_registrations_iter)
        lines = []

        def add_option(ohis, *, category=None):
            lines.append("")
            display_scope = scope or "Global"
            if category:
                lines.append(self._maybe_green(f"{display_scope} {category}:"))
            else:
                lines.append(self._maybe_green(f"{display_scope}:"))
                if description:
                    lines.append(description)
            lines.append(" ")
            if not ohis:
                lines.append("No options available.")
                return
            for ohi in ohis:
                lines.extend(self.format_option(ohi))

        add_option(oshi.basic)
        if self._show_advanced:
            add_option(oshi.advanced, category="advanced")
        if self._show_deprecated:
            add_option(oshi.deprecated, category="deprecated")
        return [*lines, "\n"]
コード例 #7
0
def test_deprecation_start_version_future():
    kwargs = {
        "deprecation_start_version": "999.99.8",
        "removal_version": "999.99.9"
    }
    ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs)
    assert "999.99.9" == ohi.removal_version
    assert not ohi.deprecation_active
コード例 #8
0
        def do_test(kwargs, expected_basic=False, expected_advanced=False):
            def exp_to_len(exp):
                return int(exp)  # True -> 1, False -> 0.

            oshi = HelpInfoExtracter("").get_option_scope_help_info([([],
                                                                      kwargs)])
            self.assertEqual(exp_to_len(expected_basic), len(oshi.basic))
            self.assertEqual(exp_to_len(expected_advanced), len(oshi.advanced))
コード例 #9
0
 def test_deprecated(self):
     kwargs = {
         'deprecated_version': '999.99.9',
         'deprecated_hint': 'do not use this'
     }
     ohi = HelpInfoExtracter('').get_option_help_info([], kwargs)
     self.assertEquals('999.99.9', ohi.deprecated_version)
     self.assertEquals('do not use this', ohi.deprecated_hint)
     self.assertIsNotNone(ohi.deprecated_message)
コード例 #10
0
 def do_test(args, kwargs, expected_display_args,
             expected_scoped_cmd_line_args):
     # The scoped and unscoped args are the same in global scope.
     expected_unscoped_cmd_line_args = expected_scoped_cmd_line_args
     ohi = HelpInfoExtracter("").get_option_help_info(args, kwargs)
     assert tuple(expected_display_args) == ohi.display_args
     assert tuple(expected_scoped_cmd_line_args) == ohi.scoped_cmd_line_args
     assert tuple(
         expected_unscoped_cmd_line_args) == ohi.unscoped_cmd_line_args
コード例 #11
0
 def test_deprecated(self):
     kwargs = {
         "removal_version": "999.99.9",
         "removal_hint": "do not use this"
     }
     ohi = HelpInfoExtracter("").get_option_help_info([], kwargs)
     self.assertEqual("999.99.9", ohi.removal_version)
     self.assertEqual("do not use this", ohi.removal_hint)
     self.assertIsNotNone(ohi.deprecated_message)
コード例 #12
0
 def do_test(args, kwargs, expected_display_args,
             expected_scoped_cmd_line_args,
             expected_unscoped_cmd_line_args):
     ohi = HelpInfoExtracter('bar.baz').get_option_help_info(
         args, kwargs)
     self.assertListEqual(expected_display_args, ohi.display_args)
     self.assertListEqual(expected_scoped_cmd_line_args,
                          ohi.scoped_cmd_line_args)
     self.assertListEqual(expected_unscoped_cmd_line_args,
                          ohi.unscoped_cmd_line_args)
コード例 #13
0
 def get_scope_data(scope):
   ret = []
   for si in ScopeInfoIterator(self.context.options.known_scope_to_info).iterate([scope]):
     help_info = HelpInfoExtracter(si.scope).get_option_scope_help_info_from_parser(
       self.context.options.get_parser(si.scope))
     ret.append({
       # We don't use _asdict(), because then .description wouldn't be available.
       'scope_info': si,
       # We do use _asdict() here, so our mustache library can do property expansion.
       'help_info': help_info._asdict(),
     })
   return ret
コード例 #14
0
 def do_test(
     args,
     kwargs,
     expected_display_args,
     expected_scoped_cmd_line_args,
     expected_unscoped_cmd_line_args,
 ):
     ohi = HelpInfoExtracter("bar.baz").get_option_help_info(args, kwargs)
     assert tuple(expected_display_args) == ohi.display_args
     assert tuple(expected_scoped_cmd_line_args) == ohi.scoped_cmd_line_args
     assert tuple(
         expected_unscoped_cmd_line_args) == ohi.unscoped_cmd_line_args
コード例 #15
0
    def do_test(kwargs, expected_basic=False, expected_advanced=False):
        def exp_to_len(exp):
            return int(exp)  # True -> 1, False -> 0.

        parser = Parser(
            env={},
            config=Config.load([]),
            scope_info=GlobalOptions.get_scope_info(),
        )
        parser.register("--foo", **kwargs)
        oshi = HelpInfoExtracter("").get_option_scope_help_info(
            "", parser, False)
        assert exp_to_len(expected_basic) == len(oshi.basic)
        assert exp_to_len(expected_advanced) == len(oshi.advanced)
コード例 #16
0
 def _format_for_global_scope(show_advanced, show_deprecated, args, kwargs):
     parser = Parser(
         env={},
         config=Config.load([]),
         scope_info=GlobalOptions.get_scope_info(),
         parent_parser=None,
     )
     parser.register(*args, **kwargs)
     # Force a parse to generate the derivation history.
     parser.parse_args(Parser.ParseArgsRequest((), OptionValueContainerBuilder(), [], False))
     oshi = HelpInfoExtracter("").get_option_scope_help_info("", parser, False)
     return HelpFormatter(
         show_advanced=show_advanced, show_deprecated=show_deprecated, color=False
     ).format_options(oshi)
コード例 #17
0
 def do_test(args, kwargs, expected_default_str):
     # Defaults are computed in the parser and added into the kwargs, so we
     # must jump through this hoop in this test.
     parser = Parser(
         env={},
         config=Config.load([]),
         scope_info=GlobalOptions.get_scope_info(),
     )
     parser.register(*args, **kwargs)
     oshi = HelpInfoExtracter(parser.scope).get_option_scope_help_info(
         "description", parser, False)
     assert oshi.description == "description"
     assert len(oshi.basic) == 1
     ohi = oshi.basic[0]
     assert to_help_str(ohi.default) == expected_default_str
コード例 #18
0
def extracter() -> HelpInfoExtracter:
    return HelpInfoExtracter("test")
コード例 #19
0
def test_not_deprecated():
    ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], {})
    assert ohi.removal_version is None
    assert not ohi.deprecation_active
コード例 #20
0
 def test_choices_enum(self) -> None:
     kwargs = {"type": LogLevel}
     ohi = HelpInfoExtracter("").get_option_help_info([], kwargs)
     assert ohi.choices == "info, debug"
コード例 #21
0
 def test_choices(self) -> None:
     kwargs = {"choices": ["info", "debug"]}
     ohi = HelpInfoExtracter("").get_option_help_info([], kwargs)
     assert ohi.choices == "info, debug"
コード例 #22
0
 def test_passthrough(self):
     kwargs = {"passthrough": True, "type": list, "member_type": str}
     ohi = HelpInfoExtracter("").get_option_help_info(["--thing"], kwargs)
     assert 2 == len(ohi.display_args)
     assert any(args.startswith("--thing") for args in ohi.display_args)
     assert any(args.startswith("... -- ") for args in ohi.display_args)
コード例 #23
0
def test_choices() -> None:
    kwargs = {"choices": ["info", "debug"]}
    ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs)
    assert ohi.choices == ("info", "debug")
コード例 #24
0
def test_list_of_enum() -> None:
    kwargs = {"type": list, "member_type": LogLevel}
    ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs)
    assert ohi.choices == ("info", "debug")
コード例 #25
0
 def test_choices(self) -> None:
     kwargs = {'choices': ['info', 'debug']}
     ohi = HelpInfoExtracter('').get_option_help_info([], kwargs)
     assert ohi.choices == 'info, debug'
コード例 #26
0
 def do_test(args, kwargs, expected_default):
     ohi = HelpInfoExtracter('').get_option_help_info(args, kwargs)
     self.assertEqual(expected_default, ohi.default)
コード例 #27
0
def test_choices_enum() -> None:
    kwargs = {"type": LogLevelSimple}
    ohi = HelpInfoExtracter("").get_option_help_info(["--foo"], kwargs)
    assert ohi.choices == ("info", "debug")