Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass several list of arguments to @click.option

I want to call a python script through the command line with this kind of parameter (list could be any size, eg with 3):

python test.py --option1 ["o11", "o12", "o13"] --option2 ["o21", "o22", "o23"] 

using click. From the docs, it is not stated anywhere that we can use a list as parameter to @click.option

And when I try to do this:

#!/usr/bin/env python import click  @click.command(context_settings=dict(help_option_names=['-h', '--help'])) @click.option('--option', default=[]) def do_stuff(option):      return  # do stuff if __name__ == '__main__':     do_stuff() 

in my test.py, by calling it from the command line:

python test.py --option ["some option", "some option 2"] 

I get an error:

Error: Got unexpected extra argument (some option 2])

I can't really use variadic arguments as only 1 variadic arguments per command is allowed (http://click.pocoo.org/5/arguments/#variadic-arguments)

So if anyone can point me to the right direction (using click preferably) it would be very much appreciated.

like image 786
downstroy Avatar asked Dec 04 '17 11:12

downstroy


People also ask

Can I pass a list in * args?

We can pass a string, integers, lists, tuples, a dictionary etc. as function arguments during a function call.

How do I grab command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.


1 Answers

If you don't insist on passing something that looks like a list, but simply want to pass multiple variadic arguments, you can use the multiple option.

From the click documentation

@click.command() @click.option('--message', '-m', multiple=True) def commit(message):     click.echo('\n'.join(message)) 
$ commit -m foo -m bar foo bar 
like image 98
Jarno Avatar answered Sep 22 '22 03:09

Jarno