Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add example that uses voluptuous.

  • Loading branch information...
commit e0c88839c4646e2d619f27399a0e30845e98735d 1 parent 6b02b95
@keleshev keleshev authored
Showing with 32 additions and 0 deletions.
  1. +32 −0 examples/validation_example.py
View
32 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)
Please sign in to comment.
Something went wrong with that request. Please try again.