| 1 |
"""Usage: prog.py [--count=N] PATH FILE... |
| 2 |
|
| 3 |
Arguments: |
| 4 |
FILE input file |
| 5 |
PATH out directory |
| 6 |
|
| 7 |
Options: |
| 8 |
--count=N number of operations |
| 9 |
|
| 10 |
""" |
| 11 |
import os |
| 12 |
|
| 13 |
from docopt import docopt |
| 14 |
try: |
| 15 |
from schema import Schema, And, Or, Use, SchemaError |
| 16 |
except ImportError: |
| 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 |
|
| 21 |
|
| 22 |
if __name__ == '__main__': |
| 23 |
args = docopt(__doc__) |
| 24 |
|
| 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')}) |
| 30 |
try: |
| 31 |
args = schema.validate(args) |
| 32 |
except SchemaError as e: |
| 33 |
exit(e) |
| 34 |
|
| 35 |
print(args) |