Flag names are globally defined! So in general, we need to be careful to pick names that are unlikely to be used by other libraries. If there is a conflict, we'll get an error at import time.
Example Usage
Flag 类型
DEFINE_string`: takes any input and interprets it as a string.
DEFINE_bool or DEFINE_boolean: typically does not take an argument: pass --myflag to set FLAGS.myflag to True, or --nomyflag to set FLAGS.myflag to False. --myflag=true and --myflag=false are also supported, but not recommended.
DEFINE_float: takes an input and interprets it as a floating point number. This also takes optional arguments lower_bound and upper_bound; if the number specified on the command line is out of range, it raises a FlagError.
DEFINE_integer: takes an input and interprets it as an integer. This also takes optional arguments lower_bound and upper_bound as for floats.
DEFINE_enum: takes a list of strings that represents legal values. If the command-line value is not in this list, it raises a flag error; otherwise, it assigns to FLAGS.flag as a string.
DEFINE_list: Takes a comma-separated list of strings on the command line and stores them in a Python list object.
DEFINE_spaceseplist: Takes a space-separated list of strings on the commandline and stores them in a Python list object. For example: --myspacesepflag "foo bar baz"
DEFINE_multi_string: The same as DEFINE_string, except the flag can be specified more than once on the command line. The result is a Python list object (list of strings), even if the flag is only on the command line once.
DEFINE_multi_integer: The same as DEFINE_integer, except the flag can be specified more than once on the command line. The result is a Python list object (list of ints), even if the flag is only on the command line once.
DEFINE_multi_enum: The same as DEFINE_enum, except the flag can be specified more than once on the command line. The result is a Python list object (list of strings), even if the flag is only on the command line once.
Special Flags
Some flags have special meanings:
--help: prints a list of all key flags (see below).
--helpshort: alias for --help.
--helpfull: prints a list of all the flags in a human-readable fashion.
--helpxml: prints a list of all flags, in XML format. Do not parse the output of --helpfull and --helpshort. Instead, parse the output of --helpxml.
--flagfile=filename: read flags from file filename.
--undefok=f1,f2: ignore unrecognized option errors for f1,f2. For boolean flags, you should use --undefok=boolflag, and --boolflag and --noboolflag will be accepted. Do not use --undefok=noboolflag.
--: as in getopt(). This terminates flag-processing.
Abseil has its own library for logging in Python. It is implemented on top of the standard logging module in Python (described in PEP282), which is good if you’re already familiar with that library. This section mentions the basics of Abseil’s logging library. See the source for more details.
Abseil Python’s testing library is similar to Python’s standard unittest module (sometimes referred to as PyUnit) but offers some additional useful features on top of the standard library, such as interfacing with Abseil Flags.
To use the Abseil testing library, do the following in your unit tests:
import the absltest module
import the flags module, which gives you access to the variables FLAGS.test_srcdir and FLAGS.test_tmpdir.
from absl import app
from absl import flags
Flags = flags.FLAGS
flags.DEFINE_integer("num_times", 1, "Number of print times")
flags.DEFINE_string("name", None, "Your name")
# Required flag
flags.mark_flag_as_required("name")
def main(argv):
del argv # Unused
for _ in range(0, Flags.num_times):
print('Hello %s, from absl' % Flags.name)
if __name__ == "__main__":
app.run(main)
# test run python3 absl-hello.py --name=test
from absl import app
from absl import flags
FLAGS = flags.FLAGS
# Flag names are globally defined! So in general, we need to be
# careful to pick names that are unlikely to be used by other libraries.
# If there is a conflict, we'll get an error at import time.
flags.DEFINE_string('name', 'Jane Random', 'Your name.')
flags.DEFINE_integer('age', None, 'Your age in years.', lower_bound=0)
flags.DEFINE_boolean('debug', False, 'Produces debugging output.')
flags.DEFINE_enum('job', 'running', ['running', 'stopped'], 'Job status.')
def main(argv):
if FLAGS.debug:
print('non-flag arguments:', argv)
print('Happy Birthday', FLAGS.name)
if FLAGS.age is not None:
print('You are %d years old, and your job is %s' % (FLAGS.age, FLAGS.job))
if __name__ == '__main__':
app.run(main)
from absl import logging
logging.info('Interesting Stuff')
logging.info('Interesting Stuff with Arguments: %d', 42)
logging.set_verbosity(logging.INFO)
logging.log(logging.DEBUG, 'This will *not* be printed')
logging.set_verbosity(logging.DEBUG)
logging.log(logging.DEBUG, 'This will be printed')
logging.warning('Worrying Stuff')
logging.error('Alarming Stuff')
logging.fatal('AAAAHHHHH!!!!') # Process exits