[Tutor] docopt module: defaults appear to be ignored

Peter Otten __peter__ at web.de
Wed Oct 9 09:29:33 CEST 2013


Alex Kleider wrote:

> 
> A recent post recommended the docopt module so I've incorporated it into
> the code I'm using to learn SQLite. It's not behaving as I expected.
> Here's a snippet of code:
> 
> 
> #!/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
> 
> I would have expected the defaults (./uwomeds68.db and matesTb) to be
> populated but as it turns out, values are populated only of --db or --tb
> are specified on the command line; otherwise, None is assigned to the
> values keyed by "--db" and "--tb".
> 
> Here are two example runs:
> 
> alex at x301:~/Python/SQL/SQLite$ ./test show_data
> Arguments in effect (from <docopt>):
> {'--db': None,
>   '--tb': None,
>   '-d': False,
>   '-h': False,
>   '-v': False,
>   'FILE': None,
>   'new_data': False,
>   'show_data': True,
>   'text_entry': False}
> -------------------------------
> 
> alex at x301:~/Python/SQL/SQLite$ ./test show_data --db=new.db
> Arguments in effect (from <docopt>):
> {'--db': 'new.db',
>   '--tb': None,
>   '-d': False,
>   '-h': False,
>   '-v': False,
>   'FILE': None,
>   'new_data': False,
>   'show_data': True,
>   'text_entry': False}
> -------------------------------
> 
> My interpretation of the documentation is that the defaults are meant to
> be picked up.  Is that not correct?

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




More information about the Tutor mailing list