3

I am adding validation using schema for CLI that uses docopt, but I cannot seem to get optional to work. I want to validate that:

  • the input file exists
  • valid options are used
  • if the PATH is added that the directory exists.

Here is app so far

"""DVget

Usage:
    DVget [-s] FILE [PATH]

    Process a file, return data based on selection
    and write results to PATH/output-file

Arguments:
    FILE        specify input file
    PATH        specify output directory (default: ./)

Options:
    -s          returns sections
    -p          returns name-sets
    -m          returns modules

"""
import os

from docopt import docopt
from schema import Schema, And, Use, Optional, SchemaError

# START OF SCRIPT
if __name__ == "__main__":

    arguments = docopt(__doc__, version="0.1")

    #print(arguments)

    schema = Schema({
        'FILE': [Use(open, error='FILE should be readable')],
        Optional('PATH'): And(os.path.exists, error='PATH should exist'),
        '-': And(str, lambda s: s in ('s', 'p', 'm'))})

    try:
        arguments = schema.validate(arguments)
        # process(arguments)
    except SchemaError as e:
        exit(e)

running DVget -s "c:\test.txt" gives me the error message 'PATH should exist' even when using Optional in schema and docopt. Any suggestions?

J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
edbeck
  • 61
  • 5
  • It seems like you have used `And` instead of `Use` for the `Optional` key. Also the `FILE` keys value should not be a list. – J. P. Petersen Feb 19 '14 at 14:51

0 Answers0