Last active
February 8, 2023 14:38
-
-
Save hartleybrody/63cda1c9b57a37be6ce541aaf5bb9d8f to your computer and use it in GitHub Desktop.
setting up multiple click commands within a single file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example of setting up multiple click commands within one file. | |
Run these commands with: | |
python /path/to/file.py do-foo --foo 'hello world' | |
Note that click converts '_' to '-' in function name | |
""" | |
import click | |
@click.group() | |
def cli(): | |
# create group for all the commands so you can | |
# run them from the __name__ == "__main__" block | |
pass | |
@cli.command() | |
@click.option('-f', '--foo', required=True) | |
def do_foo(foo): | |
print(foo) | |
@cli.command() | |
@click.option('-b', '--bar', required=True) | |
def do_bar(bar): | |
print(bar) | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment