Back to Python Fire

Api

docs/api.md

0.7.13.0 KB
Original Source

Python Fire Quick Reference

SetupCommandNotes
installpip install fireInstalls fire from pypi
Creating a CLICommandNotes
importimport fire
Callfire.Fire()Turns the current module into a Fire CLI.
Callfire.Fire(component)Turns component into a Fire CLI.
Using a CLICommandNotes
Helpcommand --helpShow the help screen.
REPLcommand -- --interactiveEnters interactive mode.
Separatorcommand -- --separator=XThis sets the separator to X. The default separator is -.
Completioncommand -- --completion [shell]Generate a completion script for the CLI.
Tracecommand -- --traceGets a Fire trace for the command.
Verbosecommand -- --verbose

Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help.

Arguments for Calling fire.Fire()

ArgumentUsageNotes
componentfire.Fire(component)If omitted, defaults to a dict of all locals and globals.
commandfire.Fire(command='hello --name=5')Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default.
namefire.Fire(name='tool')The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically.
serializefire.Fire(serialize=custom_serializer)If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method.

Using a Fire CLI without modifying any code

You can use Python Fire on a module without modifying the code of the module. The syntax for this is:

python -m fire <module> <arguments>

or

python -m fire <filepath> <arguments>

For example, python -m fire calendar -h will treat the built in calendar module as a CLI and provide its help.