python fire cli with type hint support
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file afire-1.1.0.tar.gz
.
File metadata
- Download URL: afire-1.1.0.tar.gz
- Upload date:
- Size: 91.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.16 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb264c1fab81cdafcae43b702d57b9f9565b9a2dfbbf789ca66413bcaecf9b60 |
|
MD5 | 7b1ab94736c8a693ffad1ec0301794ee |
|
BLAKE2b-256 | f22f76ea34d94268f994b4d06bda1378ba0f5145e12946ea849eda1ad9994f8d |
File details
Details for the file afire-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: afire-1.1.0-py3-none-any.whl
- Upload date:
- Size: 121.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.8.16 Darwin/23.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1fed94a472ca1fea4cbd30a21ea548b4e039d280821ee16b777a02162ed67c5 |
|
MD5 | 0ab7013f5ad192b90901d4c90a36db8f |
|
BLAKE2b-256 | f4a2de8eeb06f0841e8237d2844a5aafdc59cd1af461d34ab8a9c404ca574dd8 |