Registry of 'Flag' objects.
tf.compat.v1.flags.FlagValues()
A '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 'FlagValues' object is needed by an application: flags.FLAGS
This class is heavily overloaded:
'Flag' objects are registered via setitem: FLAGS['longname'] = x # register a new flag
The .value attribute of the registered 'Flag' objects can be accessed as attributes of this 'FlagValues' object, through getattr. Both the long and short name of the original '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 'Flag' objects through the call method. Unparsed arguments, including argv0 are returned. argv = FLAGS(sys.argv) # scan command line arguments
The original registered 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 'FlagValues' object provides help for all of the registered 'Flag' objects.
__call____call__(
argv, known_only=False
)
Parses flags from argv; stores parsed flags into this FlagValues object.
All unparsed arguments are returned.
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.The list of arguments not parsed as options, including argv[0].
Error: Raised on any parsing error.TypeError: Raised on passing wrong type of arguments.ValueError: Raised on flag value parsing error.__contains____contains__(
name
)
Returns True if name is a value (flag) in the dict.
__getitem____getitem__(
name
)
Returns the Flag object for the flag --name.
__iter____iter__()
__len____len__()
append_flag_valuesappend_flag_values(
flag_values
)
Appends flags registered in another FlagValues instance.
flag_values: FlagValues, the FlagValues instance from which to copy flags.append_flags_into_fileappend_flags_into_file(
filename
)
Appends all flags assignments from this FlagInfo object to a file.
Output will be in the format of a flagfile.
NOTE: MUST mirror the behavior of the C++ AppendFlagsIntoFile from https://github.com/gflags/gflags.
filename: str, name of the file.find_module_defining_flagfind_module_defining_flag(
flagname, default=None
)
Return the name of the module defining this flag, or default.
flagname: str, name of the flag to lookup.default: Value to return if flagname is not defined. Defaults
to None.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.
find_module_id_defining_flagfind_module_id_defining_flag(
flagname, default=None
)
Return the ID of the module defining this flag, or default.
flagname: str, name of the flag to lookup.default: Value to return if flagname is not defined. Defaults
to None.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.
flag_values_dictflag_values_dict()
Returns a dictionary that maps flag names to flag values.
flags_by_module_dictflags_by_module_dict()
Returns the dictionary of module_name -> list of defined flags.
A dictionary. Its keys are module names (strings). Its values are lists of Flag objects.
flags_by_module_id_dictflags_by_module_id_dict()
Returns the dictionary of module_id -> list of defined flags.
A dictionary. Its keys are module IDs (ints). Its values are lists of Flag objects.
flags_into_stringflags_into_string()
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: MUST mirror the behavior of the C++ CommandlineFlagsIntoString from https://github.com/gflags/gflags.
str, the string with the flags assignments from this FlagValues object. The flags are ordered by (module_name, flag_name).
get_flag_valueget_flag_value(
name, default
)
Returns the value of a flag (if not None) or a default value.
name: str, the name of a flag.default: Default value to use if the flag value is None.Requested flag value or default.
get_helpget_help(
prefix='', include_special_flags=True
)
Returns a help string for all known flags.
prefix: str, per-line output prefix.include_special_flags: bool, whether to include description of
SPECIAL_FLAGS, i.e. --flagfile and --undefok.str, formatted help message.
get_key_flags_for_moduleget_key_flags_for_module(
module
)
Returns the list of key flags for a module.
module: module|str, the module to get key flags from.[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.is_gnu_getoptis_gnu_getopt()
is_parsedis_parsed()
Returns whether flags were parsed.
key_flags_by_module_dictkey_flags_by_module_dict()
Returns the dictionary of module_name -> list of key flags.
A dictionary. Its keys are module names (strings). Its values are lists of Flag objects.
main_module_helpmain_module_help()
Describes the key flags of the main module.
str, describing the key flags of the main module.
mark_as_parsedmark_as_parsed()
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_helpmodule_help(
module
)
Describes the key flags of a module.
module: module|str, the module to describe the key flags for.str, describing the key flags of a module.
read_flags_from_filesread_flags_from_files(
argv, force_gnu=True
)
Processes command line args, but also allow args to be read from file.
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.A new list which has the original list combined with what we read from any flagfile(s).
IllegalFlagValueError: Raised when --flagfile is provided with no
argument.This function is called by FLAGS(argv).
It scans the input list for a flag that looks like:
--flagfile=
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=
register_flag_by_moduleregister_flag_by_module(
module_name, flag
)
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.
module_name: str, the name of a Python module.flag: Flag, the Flag instance that is key to the module.register_flag_by_module_idregister_flag_by_module_id(
module_id, flag
)
Records the module that defines a specific flag.
module_id: int, the ID of the Python module.flag: Flag, the Flag instance that is key to the module.register_key_flag_for_moduleregister_key_flag_for_module(
module_name, flag
)
Specifies that a flag is a key flag for a module.
module_name: str, the name of a Python module.flag: Flag, the Flag instance that is key to the module.remove_flag_valuesremove_flag_values(
flag_values
)
Remove flags that were previously appended from another FlagValues.
flag_values: FlagValues, the FlagValues instance containing flags to
remove.set_defaultset_default(
name, value
)
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.
name: str, the name of the flag to modify.value: The new default value.UnrecognizedFlagError: Raised when there is no registered flag named name.IllegalFlagValueError: Raised when value is not valid.set_gnu_getoptset_gnu_getopt(
gnu_getopt=True
)
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
gnu_getopt: bool, whether or not to use GNU style scanning.unparse_flagsunparse_flags()
Unparses all flags to the point before any FLAGS(argv) was called.
write_help_in_xml_formatwrite_help_in_xml_format(
outfile=None
)
Outputs flag documentation in XML format.
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.,
outfile: File object we write to. Default None means sys.stdout.