Skip to main content

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(zouqi.Runner):
    def __init__(self):
        super().__init__()
        self.add_argument("who", type=str)
        self.parse_args()

    # (This is not a command.)
    def show(self, action, something):
        print(self.args.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):
        super().__init__()

    @zouqi.command(inherit=True)
    def drive(self, title, *args, **kwargs):
        # other args are automatically inherited from its parent class
        print(self.args.who, "is a", title)
        super().drive(*args, **kwargs)


class SuperFancyRunner(FancyRunner):
    def __init__(self):
        super().__init__()

    @zouqi.command(inherit=True)
    def drive(self, *args, title: str = "super fancy driver", **kwargs):
        super().drive(title, *args, **kwargs)


if __name__ == "__main__":
    SuperFancyRunner().run()

Runs

$ python3 example.py 
usage: example.py [-h] {drive,drive_and_wash,wash}
example.py: error: the following arguments are required: command
$ python3 example.py drive John car
John is a super fancy driver
John drives a car
$ python3 example.py drive_and_wash John --something truck
John is a super fancy driver
John drives a pretty truck
John washes a pretty truck, good.

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

zouqi-1.0.4.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

zouqi-1.0.4-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file zouqi-1.0.4.tar.gz.

File metadata

  • Download URL: zouqi-1.0.4.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.12

File hashes

Hashes for zouqi-1.0.4.tar.gz
Algorithm Hash digest
SHA256 f7a14113cade4e43387048610841d39222060350eddfc190ed2c72ae1fa17628
MD5 a91d742777575e035a3cffe69c231ee6
BLAKE2b-256 f37eca5aeb55a61979ebc8caffe37ad6873f431f8b2fa028f87aa38003e0d266

See more details on using hashes here.

File details

Details for the file zouqi-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: zouqi-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.12

File hashes

Hashes for zouqi-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6523ccf85845377a7c1764f7e2b5fd706980e7c5c192350a580f428e55df3035
MD5 73a9cd98480b1062164ee14669cbd21f
BLAKE2b-256 97a88599594348c8968a56af56f9f06d1e6b25760955cbb2998606af7b15bb7d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page