0
0
Flaskframework~20 mins

Command pattern with Flask CLI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask CLI Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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!")
AIt prints nothing and exits silently
BIt prints: Hello from Flask CLI!
CIt raises a SyntaxError due to missing parentheses
DIt raises a RuntimeError because the command is not registered
Attempts:
2 left
💡 Hint
Look at the decorator and the function body using click.echo.
📝 Syntax
intermediate
2: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')
A
@app.cli.command('task')
def task():
    click.echo('Task done')
B
@app.cli.command('task')
def task:
    click.echo('Task done')
C
@app.cli.command('task')
def task():
    print('Task done')
D
@app.cli.command('task')
async def task():
    click.echo('Task done')
Attempts:
2 left
💡 Hint
Check function definition syntax and usage of click.echo.
state_output
advanced
2: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}!')
AError: Missing required argument 'name'
BHello, World!
CHello, Alice!
DHello, --name Alice!
Attempts:
2 left
💡 Hint
Look at the click.option decorator and the default value.
🔧 Debug
advanced
2: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}')
AThe 'times' argument is a string, but range() needs an integer
BMissing parentheses in function definition
Cclick.echo cannot be used inside loops
DThe command decorator is missing parentheses
Attempts:
2 left
💡 Hint
Check the type of 'times' passed from the CLI and how range() works.
🧠 Conceptual
expert
2:00remaining
How does Flask CLI implement the command pattern?
Which statement best describes how Flask CLI uses the command pattern?
AFlask CLI uses classes to create command objects that must be instantiated manually
BFlask CLI commands are automatically generated from route functions without decorators
CFlask CLI requires commands to be defined in separate files and imported dynamically
DFlask CLI registers functions as commands that encapsulate actions, allowing users to invoke them by name
Attempts:
2 left
💡 Hint
Think about how the @app.cli.command decorator works.