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
    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
    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} who
example.py: error: the following arguments are required: command, who
$ 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.3.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

zouqi-1.0.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: zouqi-1.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 5a5fc0a53b7aabee14477eaa85b67442cbd3a062f6934689d8afc48764ea0b7e
MD5 2253307ade67456c5b81a81fba166172
BLAKE2b-256 b18cd0c2d618948da1ca481db17e1c693f1129407d5ef99868a03c5347d078ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: zouqi-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 114217699fd9209dd64be9577999680fe7458ea2aa0f688d689644633b9bffbf
MD5 977cd6a8546115c10701498891c1b432
BLAKE2b-256 265da89f5533cb802499fb797bb07ebfb809478207142dca1b426b8d674dfc20

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