pip install clikt
python
from clikt import command, option, Argument
python
@command(help="A simple CLI application.")
def hello():
pass
python
@hello.argument("name")
def greet(name):
print(f"Hello, {name}!")
python
if __name__ == "__main__":
hello(prog_name="app")
python app.py greet John
Hello, John!
python
@hello.command(help="Greet the user with a custom message.")
@option("--message", "-m", help="The message to display.")
def greet(message):
print(message)
python app.py greet --message "Hello, world!"
Hello, world!
python
from clikt.core import Context
class NameIsRequired(Exception):
pass
@hello.argument("name", prompt="Please enter your name: ")
def greet(name):
if name is None:
raise NameIsRequired("Name is required.")
print(f"Hello, {name}!")
@hello.resultcallback(Context)
def process_result(ctx, **kwargs):
try:
ctx.invoke(greet, **kwargs)
except NameIsRequired as e:
ctx.fail(str(e))
python
@command(help="A simple CLI application.")
def hello():
pass
@hello.command(help="Greet the user with a custom message.")
@option("--message", "-m", help="The message to display.")
def greet(message):
print(message)
python app.py --help