Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python click: argument coming from default or user

How to tell whether an argument in click coming from the user or it’s the default value.

For example:

import click

@click.command()
@click.option('--value', default=1, help='a value.')
def hello(value):
    print(value)

if __name__ == "__main__":
    hello()

Now if I run python script.py --value 1, the value is now coming from user input as opposed to default value (which is set to 1). Is there any way to discern where this value is coming from?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can check with is_default() method like below:

import click

@click.command()
@click.option('--value', default=1, help='a value.')
def hello(value):
    if click.Option.is_default(value):
        print("The value is not from user input")
    else:
        print("The value is from user input")

if __name__ == "__main__":
    hello()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading