Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save happysundar/9142949 to your computer and use it in GitHub Desktop.
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
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