Skip to content
Newer
Older
100644 36 lines (27 sloc) 887 Bytes
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
1 """Usage: prog.py [--count=N] PATH FILE...
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
2
3 Arguments:
4 FILE input file
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
5 PATH out directory
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
6
7 Options:
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
8 --count=N number of operations
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
9
10 """
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
11 import os
12
13 from docopt import docopt
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
14 try:
1c0a532 @keleshev Typo.
keleshev authored Aug 24, 2012
15 from schema import Schema, And, Or, Use, SchemaError
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
16 except ImportError:
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
17 exit('This example requires that `schema` data-validation library'
18 ' is installed: \n pip install schema\n'
19 'https://github.com/halst/schema')
20
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
21
22 if __name__ == '__main__':
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
23 args = docopt(__doc__)
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
24
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
25 schema = Schema({
26 'FILE': [Use(open, error='FILE should be readable')],
27 'PATH': And(os.path.exists, error='PATH should exist'),
28 '--count': Or(None, And(Use(int), lambda n: 0 < n < 5),
29 error='--count=N should be integer 0 < N < 5')})
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
30 try:
3cb7771 @keleshev Use schema instead of voluptuous.
keleshev authored Aug 24, 2012
31 args = schema.validate(args)
32 except SchemaError as e:
33 exit(e)
e0c8883 @keleshev Add example that uses voluptuous.
keleshev authored Aug 19, 2012
34
35 print(args)
Something went wrong with that request. Please try again.