CliKit
CliKit is a Python library providing a collection of utilities to build robust and testable command-line interfaces. It serves as a foundational component for other CLI frameworks like Cleo. The library is currently at version 0.6.2, with its last release in June 2020, indicating a maintenance phase following a period of active development in late 2019 and early 2020.
Warnings
- breaking Version 0.4.0 introduced changes to how event names are stored and exposed.
- gotcha Default progress bar update rate was limited to a maximum of every 100ms in v0.6.1.
- gotcha Progress bars and indicators passed an `IO` instance will now default to using the error output stream (`stderr`) instead of standard output (`stdout`).
- gotcha Compatibility with the `pastel` dependency was a known issue that required fixes in v0.4.2.
- gotcha Early versions (e.g., 0.4.1, 0.4.3) had specific fixes for Python 2.7 related to encoding errors in questions and exception trace rendering.
Install
-
pip install clikit
Imports
- Application
from clikit.api.application import Application
- Command
from clikit.api.command import Command
- Argument
from clikit.api.args import Argument
- Option
from clikit.api.args import Option
- IO
from clikit.ui.io import IO
Quickstart
from clikit.api.application import Application
from clikit.api.command import Command
from clikit.api.args import Argument, Option
from clikit.ui.io import IO
class HelloCommand(Command):
def __init__(self):
super().__init__()
self.set_name("hello")
self.set_description("Greets the given name.")
self.add_argument(Argument("name", Argument.REQUIRED, "The name to greet."))
self.add_option(Option("loud", "l", Option.NO_VALUE, "Make the greeting loud."))
def handle(self, io: IO):
name = io.args.get("name")
loud = io.args.get("loud")
message = f"Hello, {name}!"
if loud:
message = message.upper()
io.write_line(message)
if __name__ == "__main__":
application = Application("My CLI App", "1.0.0")
application.add_command(HelloCommand())
application.run()