# tf.compat.v1.flags.FlagValues

<br />

Registry of :class:`~absl.flags.Flag` objects.

    tf.compat.v1.flags.FlagValues()

A :class:`FlagValues` can then scan command line arguments, passing flag
arguments through to the 'Flag' objects that it owns. It also
provides easy access to the flag values. Typically only one
:class:`FlagValues` object is needed by an application:
:const:`FLAGS`.

This class is heavily overloaded:

:class:`Flag` objects are registered via `__setitem__`::

     FLAGS['longname'] = x   # register a new flag

The `.value` attribute of the registered :class:`~absl.flags.Flag` objects
can be accessed as attributes of this :class:`FlagValues` object, through
`__getattr__`. Both the long and short name of the original
:class:`~absl.flags.Flag` objects can be used to access its value::

     FLAGS.longname  # parsed flag value
     FLAGS.x  # parsed flag value (short name)

Command line arguments are scanned and passed to the registered
:class:`~absl.flags.Flag` objects through the `__call__` method. Unparsed
arguments, including `argv[0]` (e.g. the program name) are returned::

     argv = FLAGS(sys.argv)  # scan command line arguments

The original registered :class:`~absl.flags.Flag` objects can be retrieved
through the use of the dictionary-like operator, `__getitem__`::

     x = FLAGS['longname']   # access the registered Flag object

The `str()` operator of a :class:`absl.flags.FlagValues` object provides
help for all of the registered :class:`~absl.flags.Flag` objects.

## Methods

### `append_flag_values`

    append_flag_values(
        flag_values: 'FlagValues'
    ) -> None

Appends flags registered in another FlagValues instance.

<br />

<br />

<br />

| Args ||
|---|---|
| `flag_values` | FlagValues, the FlagValues instance from which to copy flags. |

<br />

### `append_flags_into_file`

    append_flags_into_file(
        filename: Text
    ) -> None

Appends all flags assignments from this FlagInfo object to a file.

Output will be in the format of a flagfile.

> [!NOTE]
> **Note:** MUST mirror the behavior of the C++ AppendFlagsIntoFile from <https://github.com/gflags/gflags>

<br />

<br />

<br />

| Args ||
|---|---|
| `filename` | str, name of the file. |

<br />

### `find_module_defining_flag`

    find_module_defining_flag(
        flagname: Text, default: Optional[_T] = None
    ) -> Union[str, Optional[_T]]

Return the name of the module defining this flag, or default.

<br />

<br />

<br />

| Args ||
|---|---|
| `flagname` | str, name of the flag to lookup. |
| `default` | Value to return if flagname is not defined. Defaults to None. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The name of the module which registered the flag with this name. If no such module exists (i.e. no flag with this name exists), we return default. ||

<br />

### `find_module_id_defining_flag`

    find_module_id_defining_flag(
        flagname: Text, default: Optional[_T] = None
    ) -> Union[int, Optional[_T]]

Return the ID of the module defining this flag, or default.

<br />

<br />

<br />

| Args ||
|---|---|
| `flagname` | str, name of the flag to lookup. |
| `default` | Value to return if flagname is not defined. Defaults to None. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The ID of the module which registered the flag with this name. If no such module exists (i.e. no flag with this name exists), we return default. ||

<br />

### `flag_values_dict`

    flag_values_dict() -> Dict[Text, Any]

Returns a dictionary that maps flag names to flag values.

### `flags_by_module_dict`

    flags_by_module_dict() -> Dict[Text, List[Flag]]

Returns the dictionary of module_name -\> list of defined flags.

<br />

<br />

<br />

| Returns ||
|---|---|
| A dictionary. Its keys are module names (strings). Its values are lists of Flag objects. ||

<br />

### `flags_by_module_id_dict`

    flags_by_module_id_dict() -> Dict[int, List[Flag]]

Returns the dictionary of module_id -\> list of defined flags.

<br />

<br />

<br />

| Returns ||
|---|---|
| A dictionary. Its keys are module IDs (ints). Its values are lists of Flag objects. ||

<br />

### `flags_into_string`

    flags_into_string() -> Text

Returns a string with the flags assignments from this FlagValues object.

This function ignores flags whose value is None. Each flag
assignment is separated by a newline.

> [!NOTE]
> **Note:** MUST mirror the behavior of the C++ CommandlineFlagsIntoString from <https://github.com/gflags/gflags>

<br />

<br />

<br />

| Returns ||
|---|---|
| str, the string with the flags assignments from this FlagValues object. The flags are ordered by (module_name, flag_name). ||

<br />

### `get_flag_value`

    get_flag_value(
        name: Text, default: Any
    ) -> Any

Returns the value of a flag (if not None) or a default value.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | str, the name of a flag. |
| `default` | Default value to use if the flag value is None. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Requested flag value or default. ||

<br />

### `get_flags_for_module`

    get_flags_for_module(
        module: Union[Text, Any]
    ) -> List[https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/flags/Flag]

Returns the list of flags defined by a module.

<br />

<br />

<br />

| Args ||
|---|---|
| `module` | module\|str, the module to get flags from. |

<br />

<br />

| Returns ||
|---|---|
| \[Flag\], a new list of Flag instances. Caller may update this list as ||
| `desired` | none of those changes will affect the internals of this FlagValue instance. |

<br />

### `get_help`

    get_help(
        prefix: Text = '', include_special_flags: bool = True
    ) -> Text

Returns a help string for all known flags.

<br />

<br />

<br />

| Args ||
|---|---|
| `prefix` | str, per-line output prefix. |
| `include_special_flags` | bool, whether to include description of SPECIAL_FLAGS, i.e. --flagfile and --undefok. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| str, formatted help message. ||

<br />

### `get_key_flags_for_module`

    get_key_flags_for_module(
        module: Union[Text, Any]
    ) -> List[https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/flags/Flag]

Returns the list of key flags for a module.

<br />

<br />

<br />

| Args ||
|---|---|
| `module` | module\|str, the module to get key flags from. |

<br />

<br />

| Returns ||
|---|---|
| \[Flag\], a new list of Flag instances. Caller may update this list as ||
| `desired` | none of those changes will affect the internals of this FlagValue instance. |

<br />

### `is_gnu_getopt`

    is_gnu_getopt() -> bool

### `is_parsed`

    is_parsed() -> bool

Returns whether flags were parsed.

### `key_flags_by_module_dict`

    key_flags_by_module_dict() -> Dict[Text, List[Flag]]

Returns the dictionary of module_name -\> list of key flags.

<br />

<br />

<br />

| Returns ||
|---|---|
| A dictionary. Its keys are module names (strings). Its values are lists of Flag objects. ||

<br />

### `main_module_help`

    main_module_help() -> Text

Describes the key flags of the main module.

<br />

<br />

<br />

| Returns ||
|---|---|
| str, describing the key flags of the main module. ||

<br />

### `mark_as_parsed`

    mark_as_parsed() -> None

Explicitly marks flags as parsed.

Use this when the caller knows that this FlagValues has been parsed as if
a `__call__()` invocation has happened. This is only a public method for
use by things like appcommands which do additional command like parsing.

### `module_help`

    module_help(
        module: Any
    ) -> Text

Describes the key flags of a module.

<br />

<br />

<br />

| Args ||
|---|---|
| `module` | module\|str, the module to describe the key flags for. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| str, describing the key flags of a module. ||

<br />

### `read_flags_from_files`

    read_flags_from_files(
        argv: Sequence[Text], force_gnu: bool = True
    ) -> List[Text]

Processes command line args, but also allow args to be read from file.

<br />

<br />

<br />

| Args ||
|---|---|
| `argv` | \[str\], a list of strings, usually sys.argv\[1:\], which may contain one or more flagfile directives of the form --flagfile="./filename". Note that the name of the program (sys.argv\[0\]) should be omitted. |
| `force_gnu` | bool, if False, --flagfile parsing obeys the FLAGS.is_gnu_getopt() value. If True, ignore the value and always follow gnu_getopt semantics. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A new list which has the original list combined with what we read from any flagfile(s). ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `IllegalFlagValueError` | Raised when --flagfile is provided with no argument. |

<br />

This function is called by FLAGS(argv).
It scans the input list for a flag that looks like:
--flagfile=. Then it opens , reads all valid key and value pairs and inserts them into the input list in exactly the place where the --flagfile arg is found.

Note that your application's flags are still defined the usual way
using absl.flags DEFINE_flag() type functions.

Notes (assuming we're getting a commandline of some sort as our input):

- For duplicate flags, the last one we hit should "win".
- Since flags that appear later win, a flagfile's settings can be "weak" if the --flagfile comes at the beginning of the argument sequence, and it can be "strong" if the --flagfile comes at the end.
- A further "--flagfile=" CAN be nested in a flagfile. It will be expanded in exactly the spot where it is found.
- In a flagfile, a line beginning with # or // is a comment.
- Entirely blank lines *should* be ignored.

### `register_flag_by_module`

    register_flag_by_module(
        module_name: Text,
        flag: https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/flags/Flag
    ) -> None

Records the module that defines a specific flag.

We keep track of which flag is defined by which module so that we
can later sort the flags by module.

<br />

<br />

<br />

| Args ||
|---|---|
| `module_name` | str, the name of a Python module. |
| `flag` | Flag, the Flag instance that is key to the module. |

<br />

### `register_flag_by_module_id`

    register_flag_by_module_id(
        module_id: int,
        flag: https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/flags/Flag
    ) -> None

Records the module that defines a specific flag.

<br />

<br />

<br />

| Args ||
|---|---|
| `module_id` | int, the ID of the Python module. |
| `flag` | Flag, the Flag instance that is key to the module. |

<br />

### `register_key_flag_for_module`

    register_key_flag_for_module(
        module_name: Text,
        flag: https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/flags/Flag
    ) -> None

Specifies that a flag is a key flag for a module.

<br />

<br />

<br />

| Args ||
|---|---|
| `module_name` | str, the name of a Python module. |
| `flag` | Flag, the Flag instance that is key to the module. |

<br />

### `remove_flag_values`

    remove_flag_values(
        flag_values: 'Union[FlagValues, Iterable[Text]]'
    ) -> None

Remove flags that were previously appended from another FlagValues.

<br />

<br />

<br />

| Args ||
|---|---|
| `flag_values` | FlagValues, the FlagValues instance containing flags to remove. |

<br />

### `set_default`

    set_default(
        name: Text, value: Any
    ) -> None

Changes the default value of the named flag object.

The flag's current value is also updated if the flag is currently using
the default value, i.e. not specified in the command line, and not set
by FLAGS.name = value.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | str, the name of the flag to modify. |
| `value` | The new default value. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `UnrecognizedFlagError` | Raised when there is no registered flag named name. |
| `IllegalFlagValueError` | Raised when value is not valid. |

<br />

### `set_gnu_getopt`

    set_gnu_getopt(
        gnu_getopt: bool = True
    ) -> None

Sets whether or not to use GNU style scanning.

GNU style allows mixing of flag and non-flag arguments. See
<http://docs.python.org/library/getopt.html#getopt.gnu_getopt>

<br />

<br />

<br />

| Args ||
|---|---|
| `gnu_getopt` | bool, whether or not to use GNU style scanning. |

<br />

### `unparse_flags`

    unparse_flags() -> None

Unparses all flags to the point before any FLAGS(argv) was called.

### `validate_all_flags`

    validate_all_flags() -> None

Verifies whether all flags pass validation.

<br />

<br />

<br />

| Raises ||
|---|---|
| `AttributeError` | Raised if validators work with a non-existing flag. |
| `IllegalFlagValueError` | Raised if validation fails for at least one validator. |

<br />

### `write_help_in_xml_format`

    write_help_in_xml_format(
        outfile: Optional[TextIO] = None
    ) -> None

Outputs flag documentation in XML format.

> [!NOTE]
> **Note:** We use element names that are consistent with those used by the C++ command-line flag library, from <https://github.com/gflags/gflags> We also use a few new elements (e.g., ), but we do not interfere / overlap with existing XML elements used by the C++ library. Please maintain this consistency.

<br />

<br />

<br />

| Args ||
|---|---|
| `outfile` | File object we write to. Default None means sys.stdout. |

<br />

### `__call__`

    __call__(
        argv: Sequence[Text], known_only: bool = False
    ) -> List[Text]

Parses flags from argv; stores parsed flags into this FlagValues object.

All unparsed arguments are returned.

<br />

<br />

<br />

| Args ||
|---|---|
| `argv` | a tuple/list of strings. |
| `known_only` | bool, if True, parse and remove known flags; return the rest untouched. Unknown flags specified by --undefok are not returned. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The list of arguments not parsed as options, including argv\[0\]. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `Error` | Raised on any parsing error. |
| `TypeError` | Raised on passing wrong type of arguments. |
| `ValueError` | Raised on flag value parsing error. |

<br />

### `__contains__`

    __contains__(
        name: Text
    ) -> bool

Returns True if name is a value (flag) in the dict.

### `__getitem__`

    __getitem__(
        name: Text
    ) -> https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/flags/Flag

Returns the Flag object for the flag --name.

### `__iter__`

    __iter__() -> Iterator[Text]

### `__len__`

    __len__() -> int