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

Options with multiple argument #138

Closed
avdhoot opened this issue Sep 10, 2013 · 5 comments
Closed

Options with multiple argument #138

avdhoot opened this issue Sep 10, 2013 · 5 comments

Comments

@avdhoot
Copy link

avdhoot commented Sep 10, 2013

I am not sure. how i can do below with docopt. i need to pass multiple argument with single options. Is it possible with docopt.
my_program --in_folder='c:' 'd:' --out_folder='b:' 'g:'

@keleshev
Copy link
Member

No, it is not possible, and for a good reason. It is not possible to reliably decide which arguments belong to your options, and which—to positional argument.

There are 2 ways to achieve this.

  1. Allow to repeat an option:
Usage: prog (-o <arg>)...

Example: prog -o arg1 -o arg2
  1. Have a special separator character, for example comma:
Usage: prog -o <args>

Example: prog -o arg1,arg2,arg3

In the latter case you can write it for user's readability as -o <arg,arg> or -o <arg...>.

@avdhoot
Copy link
Author

avdhoot commented Sep 11, 2013

Thanks for quick response.
1st option looks good.
but 2nd options not work on windows.

PS C:\Users\Administrator> python.exe .\Desktop\arg.py -o a,b,c  -c a,b,c
Usage: prog -o <args> -c <args>
avdhoot@XXX:~$ python arg.py  -o a,b,c -c a,b,c
{'-c': True,
 '-o': True,
 '<args1>': 'a,b,c',
 '<args>': 'a,b,c'}

above output is expected behaviour it sholud like below

{'-c': ['a','b','c']
 '-o': ['a','b','c'] }

correct me if i am wrong.

@keleshev
Copy link
Member

but 2nd options not work on windows.

Ok? I don't know how Windows shell handles commas—is it a special character?

'<args1>': 'a,b,c' is correct, there is no special support for commas—it's up to you to split the string (and decide which character to use as a special character).

args['<args1>'] = args['<args1>'].split(',')

@keleshev
Copy link
Member

Or you could use schema for splitting the string and validating some properties about it:

>>> from schema import Schema, And, Use

>>> args = {'-c': True,
            '-o': True,
            '<args>': 'a,b,c'}

>>> schema = Schema({'<args>': And(Use(lambda s: s.split(',')),
                                   lambda l: 0 <= len(l) <= 5),
                     object: object})  # don't validate other keys

>>> schema.validate(args)
{'<args>': ['a', 'b', 'c'], '-o': True, '-c': True}

@avdhoot
Copy link
Author

avdhoot commented Sep 19, 2013

looks good Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants