Skip to content Skip to sidebar Skip to footer

Python Click Module Input For Each Function

I'm a new bee for python currently working on the Click module. So here I have a doubt to providing input for the main cli function only. But I want to provide the input for my all

Solution 1:

Sounds to me like you want different commands based on input provided to your hello cli. For that reason, Click has the useful notion of a group, a collection of commands that can be invoked.

You can reorganize your code as follows:

@click.group()defcli():
    pass@cli.command()defcreate():
    click.echo('create called')
    os.system('curl http://127.0.0.1:5000/create')

@cli.command()defconn():
    click.echo('conn called')
    os.system('curl http://127.0.0.1:5000/')

defmain():
    value = click.prompt('Select a command to run', type=click.Choice(list(cli.commands.keys()) + ['exit']))
    while value != 'exit':
        cli.commands[value]()

if __name__ == "__main__":
    main()

and the calls would be:

$ hello con
$ hello create

It doesn't seem like you need the options, as you don't change the behaviour of each command based on the option being passed in or not. For more information, please refer to the commands and groups Click documentation

Post a Comment for "Python Click Module Input For Each Function"