def parse_args()

in models/tfci.py [0:0]


def parse_args(argv):
  """Parses command line arguments."""
  parser = argparse_flags.ArgumentParser(
      formatter_class=argparse.ArgumentDefaultsHelpFormatter)

  # High-level options.
  parser.add_argument(
      "--url_prefix",
      default=URL_PREFIX,
      help="URL prefix for downloading model metagraphs.")
  parser.add_argument(
      "--metagraph_cache",
      default=METAGRAPH_CACHE,
      help="Directory where to cache model metagraphs.")
  subparsers = parser.add_subparsers(
      title="commands", dest="command",
      help="Invoke '<command> -h' for more information.")

  # 'compress' subcommand.
  compress_cmd = subparsers.add_parser(
      "compress",
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      description="Reads a PNG file, compresses it using the given model, and "
                  "writes a TFCI file.")
  compress_cmd.add_argument(
      "model",
      help="Unique model identifier. See 'models' command for options. If "
           "'target_bpp' is provided, don't specify the index at the end of "
           "the model identifier.")
  compress_cmd.add_argument(
      "--target_bpp", type=float,
      help="Target bits per pixel. If provided, a binary search is used to try "
           "to match the given bpp as close as possible. In this case, don't "
           "specify the index at the end of the model identifier. It will be "
           "automatically determined.")
  compress_cmd.add_argument(
      "--bpp_strict", action="store_true",
      help="Try never to exceed 'target_bpp'. Ignored if 'target_bpp' is not "
           "set.")

  # 'decompress' subcommand.
  decompress_cmd = subparsers.add_parser(
      "decompress",
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      description="Reads a TFCI file, reconstructs the image using the model "
                  "it was compressed with, and writes back a PNG file.")

  # 'models' subcommand.
  subparsers.add_parser(
      "models",
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      description="Lists available trained models. Requires an internet "
                  "connection.")

  tensors_cmd = subparsers.add_parser(
      "tensors",
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      description="Lists names of internal tensors of a given model.")
  tensors_cmd.add_argument(
      "model",
      help="Unique model identifier. See 'models' command for options.")

  dump_cmd = subparsers.add_parser(
      "dump",
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      description="Dumps values of given internal tensors of a model in "
                  "NumPy's .npz format.")
  dump_cmd.add_argument(
      "model",
      help="Unique model identifier. See 'models' command for options.")
  dump_cmd.add_argument(
      "--tensor", "-t", nargs="+",
      help="Name(s) of tensor(s) to dump. Must provide at least one. See "
           "'tensors' command for options.")

  # Arguments for 'compress', 'decompress', and 'dump'.
  for cmd, ext in (
      (compress_cmd, ".tfci"),
      (decompress_cmd, ".png"),
      (dump_cmd, ".npz"),
  ):
    cmd.add_argument(
        "input_file",
        help="Input filename.")
    cmd.add_argument(
        "output_file", nargs="?",
        help=f"Output filename (optional). If not provided, appends '{ext}' to "
             f"the input filename.")

  # Parse arguments.
  args = parser.parse_args(argv[1:])
  if args.command is None:
    parser.print_usage()
    sys.exit(2)
  return args