I would do the following:
"""Command.
Usage:
command start ID
command finish ID FILE
command (-h | --help)
command (-v | --version)
Arguments:
ID
FILE input file
Options:
-h --help Show this screen.
-v --version Show version.
"""
from docopt import docopt
from schema import Schema, Use, Or, SchemaError
if __name__ == '__main__':
args = docopt(__doc__, version='command alpha')
id_schema = Use(int, error='ID should be an int')
file_schema = Or(None, Use(open, error='FILE should be readable'))
try:
args['ID'] = id_schema.validate(args['ID'])
args['FILE'] = file_schema.validate(args['FILE'])
except SchemaError as e:
exit(e)
print(args)
Although I would like the circuit to be able to express the same thing using a single circuit, not two. I will try to do this in the future to make circuits like:
schema = Schema({'ID': Use(int, error='ID should be an int'),
'FILE': Or(None, Use(open, error='FILE should be readable')),
object: object})
to object: objectmean that I only care about 'ID'and 'FILE'and that all the other keys / values can be arbitrary objects.
Update
Starting with version 0.2.0, the scheme can now correctly handle this case:
schema = Schema({'ID': Use(int, error='ID should be an int'),
'FILE': Or(None, Use(open, error='FILE should be readable')),
object: object})
Halst source
share