To create a custom command in Python using the Flask CLI, you can follow these steps:
-
Install Flask (if you haven't already):
pip install Flask -
Create a Python file (e.g.,
commands.py) and add the following code:import click @click.command() @click.argument('name') def greet(name): click.echo(f'Hello, {name}!') if __name__ == '__main__': greet() -
Run the custom command from the terminal by executing the following command:
python commands.py JohnThis will output:
Hello, John!
Explanation:
- The
@click.command()decorator defines a new command. - The
@click.argument('name')decorator specifies that the command takes one argument,name. - The
click.echo()function is used to print the greeting message.
You can modify the command and its functionality as needed!
