[Tutor] docopt module: defaults appear to be ignored

Peter Otten __peter__ at web.de
Fri Oct 11 09:55:55 CEST 2013


Alex Kleider wrote:

> On 2013-10-09 00:29, Peter Otten wrote:

>> While I did not read the documentation I did try your code:
>> 
>> (docopt)$ cat test
>> #!/usr/bin/env python
>> # -*- coding : utf -8 -*-
>> # file: 'test'
>> """Usage: test [new_data | text_entry FILE | show_data ] [-hdv]
>> [--db=DATABASE] [--tb=TABLE]
>> 
>> -h --help       show this
>> -d --debug      show debugging statements
>> -v --verbose    shows table when not absolutely necessary.
>> --db DATABASE   specify database file to use [default: ./uwomeds68.db]
>> --tb TABLE      specify table to use [default: matesTb]
>> """
>> 
>> from docopt import docopt
>> 
>> cl_args = docopt(__doc__, version='testing v0.1')
>> print "Arguments in effect (from <docopt>):"
>> print(cl_args)
>> print "-------------------------------"
>> print
>> (docopt)$ ./test show_data
>> Arguments in effect (from <docopt>):
>> {'--db': './uwomeds68.db',
>>  '--debug': False,
>>  '--help': False,
>>  '--tb': 'matesTb',
>>  '--verbose': False,
>>  'FILE': None,
>>  'new_data': False,
>>  'show_data': True,
>>  'text_entry': False}
>> -------------------------------
>> 
>> So over here it works as you expected -- perhaps you need a newer
>> version of
>> docopt? I have
>> 
>> (docopt)$ python -c 'import docopt; print docopt.__version__'
>> 0.6.1
>> 
> 
> 
> Can you tell me please on which system you ran this?
> I am running Linux (Ubuntu12.04) and a friend who also runs Linux (I've
> yet to find out which flavour) got the same result as did I, so I am
> beginning to think it might be OS dependent.
> thks.

Uh, still struggling with that ;)

I'm using Linux Mint, but I think I've found a more promising cause:

$ python -c 'import docopt; print docopt.docopt("Usage: foo [--
bar=BAR]\n\n--bar BAR\twhatever [default: 42]")'
{'--bar': None}

$ python -c 'import docopt; print docopt.docopt("Usage: foo [--
bar=BAR]\n\n--bar BAR  whatever [default: 42]")'
{'--bar': '42'}

Did you spot the difference?

When "--bar BAR" is separated from "whatever" by a TAB the default is not 
honoured, you need two or more spaces. You probably have TABs in your source 
which were replaced by spaces in the email...



More information about the Tutor mailing list