Ejemplo n.º 1
0
def DEFINE_output_path(
    name: str,
    default: Union[None, str, pathlib.Path],
    help: str,
    required: bool = False,
    is_dir: bool = False,
    exist_ok: bool = True,
    must_exist: bool = False,
    validator: Callable[[pathlib.Path], bool] = None,
):
    """Registers a flag whose value is an output path.

  An "output path" is a path to a file or directory that may or may not already
  exist. The parsed value is a pathlib.Path instance. The idea is that this flag
  can be used to specify paths to files or directories that will be created
  during program execution. However, note that specifying an output path does
  not guarantee that the file will be produced.

  Args:
    name: The name of the flag.
    default: The default value for the flag. While None is a legal value, it
      will fail during parsing - output paths are required flags.
    help: The help string.
    is_dir: If true, require the that the value be a directory. Else, require
      that the value be a file. Parsing will fail if the path already exists and
      is of the incorrect type.
    exist_ok: If False, require that the path not exist, else parsing will fail.
    must_exist: If True, require that the path exists, else parsing will fail.
  """
    parser = flags_parsers.PathParser(
        must_exist=must_exist,
        exist_ok=exist_ok,
        is_dir=is_dir,
    )
    serializer = absl_flags.ArgumentSerializer()
    absl_flags.DEFINE(
        parser,
        name,
        default,
        help,
        absl_flags.FLAGS,
        serializer,
        module_name=get_calling_module_name(),
    )
    if required:
        absl_flags.mark_flag_as_required(name)
    if validator:
        RegisterFlagValidator(name, validator)
Ejemplo n.º 2
0
def DEFINE_input_path(
    name: str,
    default: Union[None, str, pathlib.Path],
    help: str,
    required: bool = False,
    is_dir: bool = False,
    validator: Callable[[pathlib.Path], bool] = None,
):
    """Registers a flag whose value is an input path.

  An "input path" is a path to a file or directory that exists. The parsed value
  is a pathlib.Path instance. Flag parsing will fail if the value of this flag
  is not a path to an existing file or directory.

  Args:
    name: The name of the flag.
    default: The default value for the flag. While None is a legal value, it
      will fail during parsing - input paths are required flags.
    help: The help string.
    is_dir: If true, require the that the value be a directory. Else, require
      that the value be a file. Parsing will fail if this is not the case.
  """
    parser = flags_parsers.PathParser(must_exist=True, is_dir=is_dir)
    serializer = absl_flags.ArgumentSerializer()
    absl_flags.DEFINE(
        parser,
        name,
        default,
        help,
        absl_flags.FLAGS,
        serializer,
        module_name=get_calling_module_name(),
    )
    if required:
        absl_flags.mark_flag_as_required(name)
    if validator:
        RegisterFlagValidator(name, validator)