Skip to content

rndmit/pycli

Repository files navigation

PyCLI

Status: work in progress

This software is not ready for production usage. Use it at your own risk

Click is good but you need to compose commands with a lot of decorators. Fire is good for fast prototyping but its full of magic underhood. So I decided to try to write a new CLI framework.

PyCLI is a library for creating CLI Applications. It's inspired by spf13/cobra and written with an emphasis on strictly typing, extensibility and as less magic as possible.

Define your application as child classes of the Command class. Instantiate Application class and register your commands in it. After firing Application.run method PyCLI parses argv list, determines entered commands and extract values for their options.

TL;DR

Minimal working application:

exit(
    pycli.Application(
        "example", 
        "Example application made with PyCLI"
    ).run()
)

Example command:

class Foo(pycli.Command):
    short = "Prints 'foo'"
    long = "Prints 'foo' to stdout"
    
    _opt_verbose = pycli.Option[bool]( # local option
            "verbose", 
            ["-v", "--verbose"], 
            "Print more information", 
            is_flag=True)

    def exec(self, vals: pycli.Values) -> int:
        verbose = vals.get(self._opt_verbose)
        if verbose: # verbose is bool because option's type is
            print("Printing 'foo'")
        print("foo")
        return 0

pycli.Application().with_commands(Foo()).run()

Features

  • Typed options with nargs
  • Flag positions has no matter
  • Autogenerated help
  • Customizable help and error messages with jinja templates
  • Lifecycle hooks (WIP)
  • Interactive mode (WIP)

TODO

  • Unit-tests
  • Usage documentation
  • Flagless arguments
  • WIP-features