Writing Simple Python CLI using Click

Photo of author

Python Click package enables you to create CLI tools easily. With this, you can focus on core logic while Click does the flag parsing and validations.

Creating a command is as simple as:

import click

@click.command() # Creates a click command
@click.option(   # Check out click's documentation to see all the options
    "--input",
    required=True,
    help = "Some input that you must provide",
)
def cli(input):  # The options becomes parameters to the method
    # You can now use input to do something
    print(f'Hello {input}')

if __name__ == "__main__":
    # Name of this method doesn't matter. Command name is derived from
    # the file name.
    cli() 

Click has good documentation. Have a look.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive updates in your inbox once a month.

I don’t spam! You can always unsubscribe if you don't like it.

🗞️ Don’t miss the latest posts!

Subscribe to the Monthly Newsletter

I won't spam. Promise!

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments