Sayer¶
Fast. Scalable. Elegant. Command the CLI like a boss. π§ββοΈ
Documentation: https://sayer.dymmond.com π
Source Code: https://github.com/dymmond/sayer
The official supported version is always the latest released.
This comprehensive guide will help you set up and understand Sayer.
Weβll walk you through installation, project creation, and writing your first commands, all with explanations, examples, and common pitfalls.
Prerequisites¶
Before you begin, ensure you have:
- Python 3.10 or higher installed.
- A terminal/command prompt.
- A basic understanding of Python and CLI concepts.
Installation¶
Install Sayer using pip:
pip install sayer
If you encounter permission errors, try:
pip install --user sayer
Or use a virtual environment:
python -m venv env
source env/bin/activate # On Windows: env\Scripts\activate
pip install sayer
Creating a New CLI Project¶
Sayer can scaffold a complete project structure with one command:
sayer new myapp
This creates the following structure:
myapp/
βββ main.py # Entry point for your CLI
βββ commands/ # Directory for your custom commands
β βββ __init__.py
βββ pyproject.toml # Project metadata
βββ README.md
βββ .gitignore
What to do:
- Edit
main.py
to define your commands. - Add new modules under
commands/
to organize functionality.
What NOT to do:
- Donβt modify
pyproject.toml
unless you understand Python packaging. - Avoid hardcoding absolute paths inside your CLI; use dynamic paths.
Writing Your First Command¶
Open main.py
and add a basic command:
from sayer import Sayer, command
app = Sayer()
@app.command()
def hello(name: str):
"""Say hello to a user by name."""
print(f"Hello, {name}!")
if __name__ == "__main__":
app()
Run it:
python main.py hello --name Alice
Output:
Hello, Alice!
Understanding the Code¶
Sayer()
creates the CLI app.@app.command()
decorates thehello
function to expose it as a CLI command.- The
name
parameter is automatically parsed from--name
. if __name__ == "__main__": app()
runs the CLI when the script is executed.
Best Practices¶
- β Use clear and concise help strings (docstrings) for commands.
- β Test your commands with various argument combinations.
- β Avoid complex logic inside commands; delegate to helper functions.
- β Donβt assume
name
will always be provided β consider adding defaults.
Next Steps¶
- Explore API Reference for detailed module docs.
- Learn about Middleware for hooks and validation.
- Add complex parameters and encoders for advanced use cases.
With Sayer, youβre not just writing a CLI β youβre building a robust, maintainable, and user-friendly command-line application. Letβs get started!