With docopt use [default: 0] in docstring
Deliberately ignoring the argparse part of your question, here is how you could define default using docopt.
With docopt you define default value (and almost all the rest) as part of docstring.
First, install docopt and for validating values also schema
$ pip install docopt schema
Then write the script a.py:
"""
Usage:
a.py [--quality <qlimit>]
a.py -h
Options:
--quality=<qlimit> Quality limit [default: 0]
"""
def main(quality):
print "FROM MAIN: minimal quality was set to", quality
if __name__ == "__main__":
from docopt import docopt
from schema import Schema, And, Use, SchemaError
args = docopt(__doc__)
print args
schema = Schema({
"--quality":
And(Use(int), lambda n: 0 <= n, error="<qlimit> must be non-negative integer"),
"-h": bool
})
try:
args = schema.validate(args)
except SchemaError as e:
exit(e)
quality = args["--quality"]
main(quality)
and use the script, first asking for help string:
$ python a.py -h
Usage:
a.py [--quality <qlimit>]
a.py -h
Options:
--quality=<qlimit> Quality limit [default: 0]
Then use it using default value:
$ python a.py
{'--quality': '0',
'-h': False}
FROM MAIN: minimal quality was set to 0
setting non-default correct one to 5:
$ python a.py --quality 5
{'--quality': '5',
'-h': False}
FROM MAIN: minimal quality was set to 5
trying not allowed negative value:
$ python a.py --quality -99
{'--quality': '-99',
'-h': False}
<qlimit> must be non-negative integer
or non integer one:
$ python a.py --quality poor
{'--quality': 'poor',
'-h': False}
<qlimit> must be non-negative integer
Note, that as soon as the validate step passes, the value for "--quality" key is already converted to int.