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         │
│something: pretty truck  │
│who: John                │
└─────────────────────────┘
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.7.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

zouqi-1.0.7-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: zouqi-1.0.7.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

Hashes for zouqi-1.0.7.tar.gz
Algorithm Hash digest
SHA256 cc8f402018b31d527e4954095d08d5ec1f09e73607041d5bd0ac83948c30dd68
MD5 c79fee7ce5a155a3943b1c963c04953b
BLAKE2b-256 75af2290ea59c6885aea0a7a9c9399d0994c7c68aada650f01088ea3ed9f0265

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zouqi-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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

Hashes for zouqi-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 91f75480e56e218765244a7e069db7f9f4f5274526943371ca3d1054e3836405
MD5 06b063eff2b18b827f087ca2fdeb3b46
BLAKE2b-256 51c37f42a968a5c5f6f31cfafba3a1f2a42447b880ef8f34c3ffea4f8ee8ffdd

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