Created
February 21, 2014 20:37
-
-
Save happysundar/9142949 to your computer and use it in GitHub Desktop.
This snippet shows how to use https://github.com/halst/schema to validate command line arguments in a python script. You should use https://github.com/docopt/docopt to handle command line arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
schema = Schema( | |
{ | |
'--schema_dir': | |
And( | |
And( | |
os.path.exists, | |
os.path.isdir, | |
lambda s: os.access(s, os.R_OK), | |
error="The path '%s' should be an existing directory and be readable. Please check" % arguments['--schema_dir'] | |
), | |
And( | |
lambda s: os.path.exists(os.path.join(os.path.realpath(s), 'Rovi 2.0 Bound US')), | |
error="Expected the directory 'Rovi 2.0 Bound US' to exist under the schema directory: '%s'. Please check!" % arguments['--schema_dir'], | |
), | |
And( | |
lambda s: os.path.exists(os.path.join(os.path.realpath(s), 'Rovi 2.0 Unbound')), | |
error="Expected the directory 'Rovi 2.0 Unbound' to exist under the schema directory : '%s'. Please check!" % arguments['--schema_dir'], | |
) | |
), | |
'--output': | |
Or( | |
#Either the directory exists and is writeable.. | |
And(os.path.exists, | |
os.path.isdir, | |
lambda s: os.access(s, os.W_OK), | |
error="The path to the database output, '%s', doesn't exist, or doesn't point to a writeable directory. Please check! " % arguments['--output'] | |
), | |
#Or the directory should be creatable...(the existing root of the specified directory tree should be writeable) | |
And(lambda s: os.access(find_existing_root_dir(s), os.W_OK), | |
error="The path to the database output, '%s', has to be created, but the current process doesn't have permissions to create the directory tree. Please check!" % arguments['--output'] | |
), | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment