| 1 |
=============== |
| 2 |
Fire Comparison |
| 3 |
=============== |
| 4 |
Fire_ is a CLI parsing library by Google that attempts to generate a CLI from any Python object. |
| 5 |
To that end, I think Fire definitely achieves its goal. |
| 6 |
However, I think Fire has too much magic, and not enough control. |
| 7 |
|
| 8 |
From the `Fire documentation`_: |
| 9 |
|
| 10 |
The types of the arguments are determined by their values, rather than by the function signature where they're used. |
| 11 |
You can pass any Python literal from the command line: numbers, strings, tuples, lists, dictionaries, (sets are only supported in some versions of Python). |
| 12 |
You can also nest the collections arbitrarily as long as they only contain literals. |
| 13 |
|
| 14 |
Essentially, Fire ignores type hints and parses CLI parameters as if they were python expressions. |
| 15 |
|
| 16 |
.. code-block:: python |
| 17 |
|
| 18 |
import fire |
| 19 |
|
| 20 |
|
| 21 |
def hello(name: str = "World"): |
| 22 |
print(f"{name=} {type(name)=}") |
| 23 |
|
| 24 |
|
| 25 |
if __name__ == "__main__": |
| 26 |
fire.Fire(hello) |
| 27 |
|
| 28 |
.. code-block:: console |
| 29 |
|
| 30 |
$ my-script foo |
| 31 |
name='foo' type(name)=<class 'str'> |
| 32 |
|
| 33 |
$ my-script 100 |
| 34 |
name=100 type(name)=<class 'int'> |
| 35 |
|
| 36 |
$ my-script true |
| 37 |
name='true' type(name)=<class 'str'> |
| 38 |
|
| 39 |
$ my-script True |
| 40 |
name=True type(name)=<class 'bool'> |
| 41 |
|
| 42 |
|
| 43 |
The equivalent in Cyclopts: |
| 44 |
|
| 45 |
.. code-block:: python |
| 46 |
|
| 47 |
import cyclopts |
| 48 |
|
| 49 |
|
| 50 |
app = cyclopts.App() |
| 51 |
|
| 52 |
|
| 53 |
@app.default |
| 54 |
def hello(name: str = "World"): |
| 55 |
print(f"{name=} {type(name)=}") |
| 56 |
|
| 57 |
|
| 58 |
if __name__ == "__main__": |
| 59 |
app() |
| 60 |
|
| 61 |
.. code-block:: console |
| 62 |
|
| 63 |
$ my-script foo |
| 64 |
name='foo' type(name)=<class 'str'> |
| 65 |
|
| 66 |
$ my-script 100 |
| 67 |
name='100' type(name)=<class 'str'> |
| 68 |
|
| 69 |
$ my-script true |
| 70 |
name='true' type(name)=<class 'str'> |
| 71 |
|
| 72 |
$ my-script True |
| 73 |
name='True' type(name)=<class 'str'> |
| 74 |
|
| 75 |
Fire is fine for some quick prototyping, but is not suitable for a serious CLI. |
| 76 |
Therefore, I wouldn't say Fire is a direct competitor to Cyclopts. |
| 77 |
|
| 78 |
|
| 79 |
.. _Fire: https://github.com/google/python-fire |
| 80 |
.. _Fire documentation: https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing |