Permalink
Browse files

Add example that uses voluptuous.

  • Loading branch information...
1 parent 6b02b95 commit e0c88839c4646e2d619f27399a0e30845e98735d @keleshev keleshev committed Aug 19, 2012
Showing with 32 additions and 0 deletions.
  1. +32 −0 examples/validation_example.py
@@ -0,0 +1,32 @@
+"""Usage: prog.py --count=N OUTDIR FILE
+
+Arguments:
+ FILE input file
+ OUTDIR out directory
+
+Options:
+ --count NUM number of operations [default: 1]
+
+"""
+import docopt
+try:
+ import voluptuous as v
+except ImportError:
+ exit('This example assumes that `voluptuous` data-validation library\n'
+ 'is installed: pip install voluptuous\n'
+ 'https://github.com/alecthomas/voluptuous')
+
+if __name__ == '__main__':
+ args = docopt.docopt(__doc__)
+
+ schema = v.Schema({
+ 'FILE': v.isfile('FILE does not exist.'),
+ 'OUTDIR': v.isdir('OUTDIR directory does not exist.'),
+ '--count': v.all(v.coerce(int, '--count should be integer.'),
+ v.clamp(min=1, max=5))})
+ try:
+ args = schema(args)
+ except v.Invalid as ex:
+ exit('\n'.join(['error: ' + e.msg.split('.')[0] for e in ex.errors]))
+
+ print(args)

0 comments on commit e0c8883

Please sign in to comment.