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:
    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         │
│who: John                │
├─────────────────────────┤
│something: pretty 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.5.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

zouqi-1.0.5-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: zouqi-1.0.5.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for zouqi-1.0.5.tar.gz
Algorithm Hash digest
SHA256 64797c4e6974f15cbb2e62e2f750a6e87811f9433b64cc5a4d8eb6df1a838647
MD5 816cc81758212dbeecbc47944d64afdd
BLAKE2b-256 a8fa2d78ada36bbcd3a6971743ff241894578ddc447da4019b6101fd9aaab632

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zouqi-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0

File hashes

Hashes for zouqi-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6cdcb8fafd145f7f8211bb6e4e2ef900b61dab4f2d9448729676aff67596872f
MD5 9d4746bfff37b9197ca2e7affa5a54b4
BLAKE2b-256 c2b972fdc67465dc8c9642460d9412f06b24356bd97227cb1cbb8a98da5c9221

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