Click: variadic options

Created on 23 Aug 2016  路  4Comments  路  Source: pallets/click

Is it possible to do similar as is possible with argparse with variadic options?

parser = argparse.ArgumentParser()
parser.add_argument('--options', nargs='+')
args = parser.parse_args()

test.py --options a b c

but it seems variadic options aren't supported

@click.command('test')
@click.option('--options', nargs=-1)
def test(options):
    pass

TypeError: Options cannot have nargs < 0

Most helpful comment

If we had @click.option('--foo', greedy=True), that could indicate to the parser that no arguments follow this option, but arguments can follow the next non-greedy option. This might also be a place to consider adding the -- token to signal the end of a greedy list.

All 4 comments

You need to specify multiple=True, but this will result in syntax like --options a --options b --options c.

the multiple options isn't inconvenient though.

Unfortunately this is the only way because Click wouldn't be able to tell what is an argument and what is an option (explicit is better than implicit). This is not a problem with a fixed nargs.

On 23 August 2016 16:24:25 CEST, Robbin Bonthond [email protected] wrote:

the multiple options isn't inconvenient though.

You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub:
https://github.com/pallets/click/issues/641#issuecomment-241747906

Sent from my Android device with K-9 Mail. Please excuse my brevity.

If we had @click.option('--foo', greedy=True), that could indicate to the parser that no arguments follow this option, but arguments can follow the next non-greedy option. This might also be a place to consider adding the -- token to signal the end of a greedy list.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dploeger picture dploeger  路  18Comments

ivankravets picture ivankravets  路  27Comments

gtristan picture gtristan  路  20Comments

flying-sheep picture flying-sheep  路  34Comments

vlcinsky picture vlcinsky  路  14Comments