File: README.rst

package info (click to toggle)
python-cyclopts 3.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,288 kB
  • sloc: python: 11,445; makefile: 24
file content (80 lines) | stat: -rw-r--r-- 2,010 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
===============
Fire Comparison
===============
Fire_ is a CLI parsing library by Google that attempts to generate a CLI from any Python object.
To that end, I think Fire definitely achieves its goal.
However, I think Fire has too much magic, and not enough control.

From the `Fire documentation`_:

    The types of the arguments are determined by their values, rather than by the function signature where they're used.
    You can pass any Python literal from the command line: numbers, strings, tuples, lists, dictionaries, (sets are only supported in some versions of Python).
    You can also nest the collections arbitrarily as long as they only contain literals.

Essentially, Fire ignores type hints and parses CLI parameters as if they were python expressions.

.. code-block:: python

   import fire


   def hello(name: str = "World"):
       print(f"{name=} {type(name)=}")


   if __name__ == "__main__":
       fire.Fire(hello)

.. code-block:: console

   $ my-script foo
   name='foo' type(name)=<class 'str'>

   $ my-script 100
   name=100 type(name)=<class 'int'>

   $ my-script true
   name='true' type(name)=<class 'str'>

   $ my-script True
   name=True type(name)=<class 'bool'>


The equivalent in Cyclopts:

.. code-block:: python

   import cyclopts


   app = cyclopts.App()


   @app.default
   def hello(name: str = "World"):
       print(f"{name=} {type(name)=}")


   if __name__ == "__main__":
       app()

.. code-block:: console

   $ my-script foo
   name='foo' type(name)=<class 'str'>

   $ my-script 100
   name='100' type(name)=<class 'str'>

   $ my-script true
   name='true' type(name)=<class 'str'>

   $ my-script True
   name='True' type(name)=<class 'str'>

Fire is fine for some quick prototyping, but is not suitable for a serious CLI.
Therefore, I wouldn't say Fire is a direct competitor to Cyclopts.


.. _Fire: https://github.com/google/python-fire
.. _Fire documentation: https://github.com/google/python-fire/blob/master/docs/guide.md#argument-parsing