Пример #1
0
 def test_allows_provided_subcommands(self):
     parser = argparse.ArgumentParser()
     subparsers = parser.add_subparsers()
     subparser = subparsers.add_parser("magic")
     subparser.set_defaults(chosen="magic")
     with argparse_util.allow_missing_subcommand():
         args = parser.parse_args(["magic"])
     self.assertEqual(args, argparse.Namespace(chosen="magic"))
Пример #2
0
    def configure(self, argv=("", ), **kwargs):
        """Configures TensorBoard behavior via flags.

        This method will populate the "flags" property with an argparse.Namespace
        representing flag values parsed from the provided argv list, overridden by
        explicit flags from remaining keyword arguments.

        Args:
          argv: Can be set to CLI args equivalent to sys.argv; the first arg is
            taken to be the name of the path being executed.
          kwargs: Additional arguments will override what was parsed from
            argv. They must be passed as Python data structures, e.g.
            `foo=1` rather than `foo="1"`.

        Returns:
          Either argv[:1] if argv was non-empty, or [''] otherwise, as a mechanism
          for absl.app.run() compatibility.

        Raises:
          ValueError: If flag values are invalid.
        """

        base_parser = argparse_flags.ArgumentParser(
            prog="tensorboard",
            description=(
                "TensorBoard is a suite of web applications for "
                "inspecting and understanding your TensorFlow runs "
                "and graphs. https://github.com/tensorflow/tensorboard "),
        )
        subparsers = base_parser.add_subparsers(
            help="TensorBoard subcommand (defaults to %r)" %
            _SERVE_SUBCOMMAND_NAME)

        serve_subparser = subparsers.add_parser(
            _SERVE_SUBCOMMAND_NAME,
            help="start local TensorBoard server (default subcommand)",
        )
        serve_subparser.set_defaults(
            **{_SUBCOMMAND_FLAG: _SERVE_SUBCOMMAND_NAME})

        if len(argv) < 2 or argv[1].startswith("-"):
            # This invocation, if valid, must not use any subcommands: we
            # don't permit flags before the subcommand name.
            serve_parser = base_parser
        else:
            # This invocation, if valid, must use a subcommand: we don't take
            # any positional arguments to `serve`.
            serve_parser = serve_subparser

        for (name, subcommand) in six.iteritems(self.subcommands):
            subparser = subparsers.add_parser(
                name,
                help=subcommand.help(),
                description=subcommand.description(),
            )
            subparser.set_defaults(**{_SUBCOMMAND_FLAG: name})
            subcommand.define_flags(subparser)

        for loader in self.plugin_loaders:
            loader.define_flags(serve_parser)

        arg0 = argv[0] if argv else ""

        with argparse_util.allow_missing_subcommand():
            flags = base_parser.parse_args(
                argv[1:])  # Strip binary name from argv.
        if getattr(flags, _SUBCOMMAND_FLAG, None) is None:
            # Manually assign default value rather than using `set_defaults`
            # on the base parser to work around Python bug #9351 on old
            # versions of `argparse`: <https://bugs.python.org/issue9351>
            setattr(flags, _SUBCOMMAND_FLAG, _SERVE_SUBCOMMAND_NAME)

        self.cache_key = manager.cache_key(
            working_directory=os.getcwd(),
            arguments=argv[1:],
            configure_kwargs=kwargs,
        )
        if arg0:
            # Only expose main module Abseil flags as TensorBoard native flags.
            # This is the same logic Abseil's ArgumentParser uses for determining
            # which Abseil flags to include in the short helpstring.
            for flag in set(absl_flags.FLAGS.get_key_flags_for_module(arg0)):
                if hasattr(flags, flag.name):
                    raise ValueError("Conflicting Abseil flag: %s" % flag.name)
                setattr(flags, flag.name, flag.value)
        for k, v in kwargs.items():
            if not hasattr(flags, k):
                raise ValueError("Unknown TensorBoard flag: %s" % k)
            setattr(flags, k, v)
        if getattr(flags, _SUBCOMMAND_FLAG) == _SERVE_SUBCOMMAND_NAME:
            for loader in self.plugin_loaders:
                loader.fix_flags(flags)
        self.flags = flags
        return [arg0]
Пример #3
0
 def test_still_complains_on_missing_arguments(self):
     parser = argparse.ArgumentParser()
     parser.add_argument("please_provide_me")
     with argparse_util.allow_missing_subcommand():
         with self.assertRaises(SystemExit):
             parser.parse_args([])