How to create a custom command?

073

To create a custom command in Python using the Flask CLI, you can follow these steps:

  1. Install Flask (if you haven't already):

    pip install Flask
  2. 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()
  3. Run the custom command from the terminal by executing the following command:

    python commands.py John

    This 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!

0 Comments

no data
Be the first to share your comment!