[Tutor] docopt module: defaults appear to be ignored
Alex Kleider
akleider at sonic.net
Wed Oct 9 02:50:15 CEST 2013
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?
More information about the Tutor
mailing list