Catalog PyPI
PACKAGE EXPLORERView in registry

afire

PyPI

python fire cli with type hint support

Versions collected1
Direct dependencies2From release 1.1.0
Last observed20 Oct, 21:07 UTC

README

Version 1.1.0 documentation

Python Afire PyPI

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:

typeexample
str"a"
int1
bytesb"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

strintbytes<- input
str***
intcan be converted to int*x
bytes*(utf8)*(length 8, big order)*
datetime/dateformat: YYYY-MM-DD-HH:MM:SSxx
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 parameterif supportif supportif 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.

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
1.1.03 Jul 20242

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 21:07 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:76afe71b0c389b45ab31ccd617bbdcc0f5a115b8b587dbc348ac86f8859d1a65