|
| 1 | +# Python Fire |
| 2 | +_Python Fire is a library for creating command line interfaces (CLIs) from |
| 3 | +absolutely any Python object._ |
| 4 | + |
| 5 | +- Python Fire is a simple way to create a CLI in Python. [[1]](doc/benefits.md#simple-cli) |
| 6 | +- Python Fire is a helpful tool for developing and debugging Python code. [[2]](doc/benefits.md#debugging) |
| 7 | +- Python Fire helps with exploring existing code or turning other people's code |
| 8 | +into a CLI. [[3]](doc/benefits.md#exploring) |
| 9 | +- Python Fire makes transitioning between Bash and Python easier. [[4]](doc/benefits.md#bash) |
| 10 | +- Python Fire makes using a Python REPL easier by setting up the REPL with the |
| 11 | +modules and variables you'll need already imported and created. [[5]](doc/benefits.md#repl) |
| 12 | + |
| 13 | +## Installation |
| 14 | +
341A
td> |
| 15 | +`pip install fire` |
| 16 | + |
| 17 | +## Basic Usage |
| 18 | + |
| 19 | +You can call `Fire` on any Python object:<br> |
| 20 | +functions, classes, modules, objects, dictionaries, lists, tuples, etc. |
| 21 | +They all work! |
| 22 | + |
| 23 | +Here's a simple example. |
| 24 | + |
| 25 | +```python |
| 26 | +import fire |
| 27 | + |
| 28 | +class Calculator(object): |
| 29 | + """A simple calculator class.""" |
| 30 | + |
| 31 | + def double(self, number): |
| 32 | + return 2 * number |
| 33 | + |
| 34 | +if __name__ == '__main__': |
| 35 | + fire.Fire(Calculator) |
| 36 | +``` |
| 37 | + |
| 38 | +Then, from the command line, you can run: |
| 39 | + |
| 40 | +```bash |
| 41 | +python calculator.py double 10 # 20 |
| 42 | +python calculator.py double --number=15 # 30 |
| 43 | +``` |
| 44 | + |
| 45 | +To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn |
| 46 | +about Fire's other features, see the [Using a Fire CLI page](doc/using-cli.md). |
| 47 | + |
| 48 | + |
| 49 | +## Why is it called Fire? |
| 50 | + |
| 51 | +When you call `Fire`, it fires off (executes) your command. |
| 52 | + |
| 53 | + |
| 54 | +## Reference |
| 55 | + |
| 56 | +| Setup | Command | Notes |
| 57 | +| :------ | :------------------ | :--------- |
| 58 | +| install | `pip install fire` | |
| 59 | + |
| 60 | +| Creating a CLI | Command | Notes |
| 61 | +| :--------------| :--------------------- | :--------- |
| 62 | +| import | `import fire` | |
| 63 | +| Call | `fire.Fire()` | Turns the current module into a Fire CLI. |
| 64 | +| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI. |
| 65 | + |
| 66 | +| Using a CLI | Command | Notes |
| 67 | +| :------------- | :------------------------- | :--------- |
| 68 | +| [Help](doc/using-cli.md#help-flag) | `command -- --help` | |
| 69 | +| [REPL](doc/using-cli.md#interactive-flag) | `command -- --interactive` | Enters interactive mode. |
| 70 | +| [Separator](doc/using-cli.md#separator-flag) | `command -- --separator=X` | This sets the separator to `X`. The default separator is `-`. |
| 71 | +| [Completion](doc/using-cli.md#completion-flag) | `command -- --completion` | Generate a completion script for the CLI. |
| 72 | +| [Trace](doc/using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire trace for the command. |
| 73 | +| [Verbose](doc/using-cli.md#verbose-flag) | `command -- --verbose` | |
| 74 | +_Note that flags are separated from the Fire command by an isolated `--` arg._ |
| 75 | + |
| 76 | + |
| 77 | +## Disclaimer |
| 78 | + |
| 79 | +This is not an official Google product. |
0 commit comments