Python Afire 
Fork from python-fire. support type conversion based on the type hints.
Python AFire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object.
- Python Afire is a simple way to create a CLI in Python. [1]
- Python Afire is a helpful tool for developing and debugging Python code. [2]
- Python Afire helps with exploring existing code or turning other people's code into a CLI. [3]
- Python Afire makes transitioning between Bash and Python easier. [4]
- Python Afire makes using a Python REPL easier by setting up the REPL with the modules and variables you'll need already imported and created. [5]
- Python Afire fully support type hint.
Installation
To install Python Afire with pip, run: pip install afire
To install Python Afire with conda, run: conda install afire -c conda-forge
To install Python Afire from source, first clone the repository and then run:
poetry install
Basic Usage
You can call Fire on any Python object:
functions, classes, modules, objects, dictionaries, lists, tuples, etc.
They all work!
Here's an example of calling Fire on a function with type hint, it will automatically recognize and convert types according to your type hint.
import afire
from pathlib import Path
def hello(path: Path):
assert isinstance(path, Path)
if __name__ == '__main__':
afire.Fire(hello)
Then, from the command line, you can run:
python hello.py --path=/root # No error
python hello.py --help # Shows usage information.
Here's an example of calling Fire on a function with nested type hint.
import afire
from typing import Dict, Union, Set
def test(a: Union[Dict[str, int], Set[bytes]]):
# check types
assert isinstance(a, (Dict, Set))
# check types in dict or set
if isinstance(a, Dict):
for k, v in a.items():
assert isinstance(k, str)
assert isinstance(v, int)
else:
for i in a:
assert isinstance(i, bytes)
print(a)
if __name__ == '__main__':
afire.Fire(test)
Then, from the command line, you can run:
# dict type
python test.py --a='{1: 2}' # {'1': 2}
# or use position arg
python test.py '{1: 2}' # {'1': 2}
# set type
python test.py --a='{a, b, c}' # {b'a', b'b', b'c'}
Type conversion rules
Currently support input types:
| type | example |
|---|---|
| str | "a" |
| int | 1 |
| bytes | b"a" |
| List | [x, y, z] |
| Dict | {x: y} |
| Set | {x, y, z} |
| Tuple | (x, y, z) |
note: you can use str or bytes expr in complex type, e.g. [b"x", b"y"]
Rule
| str | int | bytes | <- input | |
|---|---|---|---|---|
| str | * | * | * | |
| int | can be converted to int | * | x | |
| bytes | *(utf8) | *(length 8, big order) | * | |
| datetime/date | format: YYYY-MM-DD-HH:MM:SS | x | x | |
| format: YYYY-MM-DD HH:MM:SS | ||||
| format: YYYY/MM/DD HH:MM:SS | ||||
| format: YYYYMMDDHHMMSS | ||||
| format: YYYY/MM/DD | ||||
| format: YYYY-MM-DD | ||||
| any type with one parameter | if support | if support | if support | |
| e.g. Path, float | ||||
| ^ type hint |
*: any kind of input will convert
x: not support to convert
Currently relation type hint only support Union and Optional.
License
Licensed under the Apache 2.0 License.
Disclaimer
This is not an official Google product.
Metadata
Release files for afire 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| afire-1.1.0.tar.gz | 91.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| afire-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 212.7 kB
Release files / afire-1.1.0.tar.gz
| Download URL | afire-1.1.0.tar.gz |
|---|---|
| Size | 91.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb264c1fab81cdafcae43b702d57b9f9565b9a2dfbbf789ca66413bcaecf9b60
|
|
BLAKE2b-256 checksum How to use checksums |
f22f76ea34d94268f994b4d06bda1378ba0f5145e12946ea849eda1ad9994f8d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.8.16 Darwin/23.4.0
|
Release files / afire-1.1.0-py3-none-any.whl
| Download URL | afire-1.1.0-py3-none-any.whl |
|---|---|
| Size | 121.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a1fed94a472ca1fea4cbd30a21ea548b4e039d280821ee16b777a02162ed67c5
|
|
BLAKE2b-256 checksum How to use checksums |
f4a2de8eeb06f0841e8237d2844a5aafdc59cd1af461d34ab8a9c404ca574dd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.3 CPython/3.8.16 Darwin/23.4.0
|