Challenge - 5 Problems
Flask CLI Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when you run a custom Flask CLI command?
Given this Flask CLI command code, what will be printed when running
flask greet in the terminal?Flask
import click from flask import Flask app = Flask(__name__) @app.cli.command("greet") def greet(): click.echo("Hello from Flask CLI!")
Attempts:
2 left
💡 Hint
Look at the decorator and the function body using click.echo.
✗ Incorrect
The @app.cli.command decorator registers the function as a CLI command named 'greet'. Running 'flask greet' calls the function, which prints the message using click.echo.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Flask CLI command
Which option shows the correct way to define a Flask CLI command that prints 'Task done'?
Flask
from flask import Flask import click app = Flask(__name__) @app.cli.command('task') def task(): click.echo('Task done')
Attempts:
2 left
💡 Hint
Check function definition syntax and usage of click.echo.
✗ Incorrect
Option A correctly defines a function with parentheses and uses click.echo to print. Option A misses parentheses causing SyntaxError. Option A uses print which works but is not recommended in Flask CLI commands. Option A uses async def which Flask CLI does not support.
❓ state_output
advanced2:00remaining
What is the output after running this Flask CLI command with an argument?
Consider this Flask CLI command that takes a name argument. What will be printed when running
flask hello --name Alice?Flask
import click from flask import Flask app = Flask(__name__) @app.cli.command('hello') @click.option('--name', default='World', help='Name to greet') def hello(name): click.echo(f'Hello, {name}!')
Attempts:
2 left
💡 Hint
Look at the click.option decorator and the default value.
✗ Incorrect
The command defines an option '--name' with default 'World'. Passing '--name Alice' sets name to 'Alice', so the output is 'Hello, Alice!'.
🔧 Debug
advanced2:00remaining
Why does this Flask CLI command raise an error?
This Flask CLI command raises a TypeError when run. What is the cause?
Flask
import click from flask import Flask app = Flask(__name__) @app.cli.command('count') @click.argument('times') def count(times): for i in range(times): click.echo(f'Count {i+1}')
Attempts:
2 left
💡 Hint
Check the type of 'times' passed from the CLI and how range() works.
✗ Incorrect
Click passes arguments as strings by default. Using a string in range() causes TypeError. The fix is to convert 'times' to int.
🧠 Conceptual
expert2:00remaining
How does Flask CLI implement the command pattern?
Which statement best describes how Flask CLI uses the command pattern?
Attempts:
2 left
💡 Hint
Think about how the @app.cli.command decorator works.
✗ Incorrect
Flask CLI uses decorators to register functions as commands. Each command encapsulates a specific action. This matches the command pattern where commands are objects (functions here) that can be executed by name.