2

I'm trying to learn using the module click to create CLI arguments parsing, and wanted to try something different from Argparse.

However, I tried to follow the official documentation, youtube examples and so forth and click just ignores anything I throw at it.

I've tried on Ubuntu 16.04 and Mac OSX, using:

[Python Version]
Python 2.7.13 :: Anaconda custom (x86_64)
[Click Version]
Version: 6.7

This is the code (copied from a tutorial):

import click

@click.command()
@click.option('--verbose', is_flag=True, help="Will print verbose messages.")
def cli(verbose):
    if verbose:
        click.echo("We are in the verbose mode.")
    click.echo("Hello World")

When I run it... nothing happens. No error. Just nothing. What can be wrong? I installed click using "pip install click", and tried on two OS'es.

screen

1 Answer 1

2

Add

if __name__ == '__main__':
    cli()

Otherwise all you've done is defined a function and then done nothing with it. Your answer is right there in the first example in the documentation :)

Sign up to request clarification or add additional context in comments.

7 Comments

Wow, thanks! That was fast and basic. I will accept this as the answer, as soon as stack allows me (10mins wait). :)
Since it seems you're new to Python you'll probably also want to see the answers to this question: stackoverflow.com/questions/419163/what-does-if-name-main-do
Right there in the first example in the documentation and never shown again :) I mean look at the options documentation. Not only do they omit this code, they make it seem like you can create a function named dots and run it on the command line using 'dots'.
Technically that's true. Since Python 3.(6, maybe?) you can define def foo(): ... and that's perfectly valid syntax, it's just a no-op
If you hard code cli() to run every time (in the __main__ bit), what's the point of using click? This answer will work to run that method - but what if you have lots, to be selected by different commands? Put them all in __main__?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.