📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Flask Web Framework Flask CLI

Flask CLI

4 min read
Build CLI commands with @app.cli.command() and Click for admin scripts and cron tasks.

Custom CLI Commands

import click
from flask.cli import with_appcontext

@app.cli.command("create-admin")
@click.argument("email")
@click.option("--name", default="Admin", help="Admin name")
@with_appcontext
def create_admin(email, name):
    """Create an admin user."""
    from app.models import User
    user = User(name=name, email=email, role="admin")
    user.set_password(click.prompt("Password", hide_input=True, confirmation_prompt=True))
    db.session.add(user)
    db.session.commit()
    click.echo("Admin " + email + " created!")

@app.cli.command("send-report")
@click.option("--date", default=None, help="Date YYYY-MM-DD")
def send_report(date):
    """Send daily report."""
    # generate and send report
    click.echo("Report sent!")

# Run
# flask create-admin admin@example.com --name "Super Admin"
# flask send-report --date 2024-01-15