zouqi is a CLI starter similar to python-fire. It is purely built on argparse.
Project description
Zouqi: A Python CLI Starter Purely Built on argparse.
Zouqi (『走起』 in Chinese) is a CLI starter similar to python-fire. It is purely built on argparse.
Why not python-fire?
- Fire cannot be used to share options between commands easily.
- Fire treat all member functions as its command, which is not desirable in many situations.
Installation
pip install zouqi
Example
Code
import zouqi
from zouqi.parsing import ignored
def prettify(something):
return f"pretty {something}"
class Runner:
def __init__(self, who: str):
self.who = who
# (This is not a command.)
def show(self, action, something):
print(self.who, action, something)
# Decorate the command with the zouqi.command decorator.
@zouqi.command
def drive(self, something):
# Equivalent to: parser.add_argument('something').
# the parsed args will be stored in self.drive.args instead of self.args
self.show("drives a", something)
@zouqi.command
def wash(self, something, hidden_option: ignored = ""):
# hidden option will be ignored during parsing but still passable by another function
self.show("washes a", something + hidden_option)
@zouqi.command
def drive_and_wash(self, something: prettify = "car"):
# Equivalent to: parser.add_argument('--something', type=prettify, default='car').
# Type hint is used as argument parser (a little bit abuse of type hint here).
self.drive(something)
self.wash(something, ", good.")
class FancyRunner(Runner):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def drive(self, title, *args, **kwargs):
# other args are automatically inherited from its parent class
print(self.who, "is a", title)
super().drive(*args, **kwargs)
class SuperFancyRunner(FancyRunner):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@zouqi.command(inherit=True)
def drive(self, *args, title: str = "super fancy driver", **kwargs):
super().drive(title, *args, **kwargs)
if __name__ == "__main__":
print("======= Calling in the script ========")
SuperFancyRunner("John").drive_and_wash("car")
print("======= Calling from the CLI ========")
zouqi.start(SuperFancyRunner)
Runs
$ python3 example.py
======= Calling in the script ========
John is a super fancy driver
John drives a car
John washes a car, good.
======= Calling from the CLI ========
usage: example.py [-h] [--print-args] {drive,drive_and_wash,wash} who
example.py: error: the following arguments are required: command, who
$ python3 example.py drive John car
======= Calling in the script ========
John is a super fancy driver
John drives a car
John washes a car, good.
======= Calling from the CLI ========
John is a super fancy driver
John drives a car
$ python3 example.py drive_and_wash John --something truck --print-args
======= Calling in the script ========
John is a super fancy driver
John drives a car
John washes a car, good.
======= Calling from the CLI ========
┌─────────────────────────┐
│ Arguments │
├─────────────────────────┤
│command: drive_and_wash │
│print_args: True │
│something: pretty truck │
│who: John │
└─────────────────────────┘
John is a super fancy driver
John drives a pretty truck
John washes a pretty truck, good.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
zouqi-1.0.6.tar.gz
(5.3 kB
view details)
Built Distribution
zouqi-1.0.6-py3-none-any.whl
(5.9 kB
view details)
File details
Details for the file zouqi-1.0.6.tar.gz
.
File metadata
- Download URL: zouqi-1.0.6.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48741c7cace43f593a91c57f47a9dd69e711e57aee606fec6236adb3bd5477ce |
|
MD5 | 6c49f654c2d61a1776e77ea2e43dc209 |
|
BLAKE2b-256 | e0db574529d687b8a1b2dbf82077eafb5a2d20ad1ad13b9ba6fbcafecb3a6b28 |
File details
Details for the file zouqi-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: zouqi-1.0.6-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f6b158fea9301da95cb2e197b4a736e581520573ed5ed9d80de32a89a645247 |
|
MD5 | 6a2fe088c6d99ca78285655d8223a274 |
|
BLAKE2b-256 | 9b29dfbbeb18bf67c3b0375bcf1ab9ac72aa09ba8a1d13c629afdb5553f761da |