Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional arguments #94

Closed
cameronmaske opened this issue May 25, 2014 · 6 comments
Closed

Optional arguments #94

cameronmaske opened this issue May 25, 2014 · 6 comments

Comments

@cameronmaske
Copy link

Are optional arguments currently possible with click?

For example...

$ greet
hello all
$ greet john
hello john
$ greet john sally
hello john and sally
@mitsuhiko
Copy link
Contributor

Yes. Set required to false.

@cameronmaske
Copy link
Author

Answered my own question!
Seems you beat me too comment mitsuhiko :)

Just saw the required flag in http://click.pocoo.org/api/#parameters

If anyone else stumbles across this, as a quick ref, it's a simple as...

@cli.command()
@click.argument('names', nargs=-1, required=False)
def greet(names):
    if len(names) == 0:
        print "hello all"
    else:
       print "hello {}".format(" and ".join(names))

@mitsuhiko
Copy link
Contributor

JFTR: if nargs is -1 then the argument becomes implicitly "non required". An empty sequence still works.

@chrisdembia
Copy link

I think the documentation could make this flag more obvious, by putting a section on the Arguments page.

@Wilfred
Copy link

Wilfred commented Aug 7, 2017

I landed here looking for a way of specifying multiple optional arguments. Based on the examples above, I did:

@click.command()
@click.argument('size', required=False)
@click.argument('flavour', required=False)
def order_pizza(size, flavour):
    print "Ordering {} ({})".format(size, flavour)

This gives you two positional arguments, both of which are optional.

@kennethreitz
Copy link

This helped me today. :)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants