absl.app module
Generic entry point for Abseil Python applications.
To use this module, define a main function with a single argv argument
and call app.run(main). For example:
def main(argv):
if len(argv) > 1:
raise app.UsageError('Too many command-line arguments.')
if __name__ == '__main__':
app.run(main)
- class absl.app.ExceptionHandler[source]
Bases:
objectBase exception handler from which other may inherit.
- class absl.app.HelpFlag[source]
Bases:
BooleanFlagSpecial boolean flag that displays usage and raises SystemExit.
- NAME = 'help'
- SHORT_NAME = '?'
- class absl.app.HelpXMLFlag[source]
Bases:
BooleanFlagSimilar to HelpfullFlag, but generates output in XML format.
- class absl.app.HelpfullFlag[source]
Bases:
BooleanFlagDisplay help for flags in the main module and all dependent modules.
- class absl.app.HelpshortFlag[source]
Bases:
HelpFlag–helpshort is an alias for –help.
- NAME = 'helpshort'
- SHORT_NAME = None
- exception absl.app.UsageError(message, exitcode=1)[source]
Bases:
ErrorException raised when the arguments supplied by the user are invalid.
Raise this when the arguments supplied are invalid from the point of view of the application. For example when two mutually exclusive flags have been supplied or when there are not enough non-flag arguments. It is distinct from flags.Error which covers the lower level of parsing and validating individual flags.
- absl.app.call_after_init(callback)[source]
Calls the given callback only once ABSL has finished initialization.
If ABSL has already finished initialization when
call_after_initis called then the callback is executed immediately, otherwise callback is stored to be executed afterapp.runhas finished initializing (aka. just before the main function is called).If called after
app.run, this is equivalent to callingcallback()in the caller thread. If called beforeapp.run, callbacks are run sequentially (in an undefined order) in the same thread asapp.run.- Parameters:
callback – a callable to be called once ABSL has finished initialization. This may be immediate if initialization has already finished. It takes no arguments and returns nothing.
- absl.app.install_exception_handler(handler)[source]
Installs an exception handler.
- Parameters:
handler – ExceptionHandler, the exception handler to install.
- Raises:
TypeError – Raised when the handler was not of the correct type.
All installed exception handlers will be called if main() exits via an abnormal exception, i.e. not one of SystemExit, KeyboardInterrupt, FlagsError or UsageError.
- absl.app.parse_flags_with_usage(args)[source]
Tries to parse the flags, print usage, and exit if unparsable.
- Parameters:
args – [str], a non-empty list of the command line arguments including program name.
- Returns:
[str], a non-empty list of remaining command line arguments after parsing flags, including program name.
- absl.app.run(main, argv=None, flags_parser=<function parse_flags_with_usage>)[source]
Begins executing the program.
- Parameters:
main – The main function to execute. It takes an single argument “argv”, which is a list of command line arguments with parsed flags removed. The return value is passed to sys.exit, and so for example a return value of 0 or None results in a successful termination, whereas a return value of 1 results in abnormal termination. For more details, see https://docs.python.org/3/library/sys#sys.exit
argv – A non-empty list of the command line arguments including program name, sys.argv is used if None.
flags_parser – Callable[[List[Text]], Any], the function used to parse flags. The return value of this function is passed to main untouched. It must guarantee FLAGS is parsed after this function is called. Should be passed as a keyword-only arg which will become mandatory in a future release.
Parses command line flags with the flag module.
If there are any errors, prints usage().
Calls main() with the remaining arguments.
If main() raises a UsageError, prints usage and the error message.
- absl.app.usage(shorthelp=False, writeto_stdout=False, detailed_error=None, exitcode=None)[source]
Writes __main__’s docstring to stderr with some help text.
- Parameters:
shorthelp – bool, if True, prints only flags from the main module, rather than all flags.
writeto_stdout – bool, if True, writes help message to stdout, rather than to stderr.
detailed_error – str, additional detail about why usage info was presented.
exitcode – optional integer, if set, exits with this status code after writing help.