Giter Site home page Giter Site logo

Optional arguments about click HOT 6 CLOSED

pallets avatar pallets commented on July 23, 2024
Optional arguments

from click.

Comments (6)

mitsuhiko avatar mitsuhiko commented on July 23, 2024 24

Yes. Set required to false.

from click.

Wilfred avatar Wilfred commented on July 23, 2024 12

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.

from click.

chrisdembia avatar chrisdembia commented on July 23, 2024 5

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

from click.

cameronmaske avatar cameronmaske commented on July 23, 2024 3

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))

from click.

mitsuhiko avatar mitsuhiko commented on July 23, 2024

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

from click.

kennethreitz avatar kennethreitz commented on July 23, 2024

This helped me today. :)

from click.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.