iNoob
iNoob

Reputation: 1395

Argument error handeling docopt python

Im trying to work out how to print usage info if my script isnt parsed any arguments. Im trying to achieve this with docopt. Below is the code I have and it works as expected but I just cant work out what I need to add to print the usage if no arguments are parsed or incorrect arguments. (blank argument etc) Any help would be appreciated.

#!/usr/bin/python

"""
Description:

Compare 2 lists and print the common values.

Usage: Checker.py
  Checker.py [(-a <list1>) (-g <list2>)]
  Checker.py -h | --help
  Checker.py --version


Options:
   -a --list1         List1
   -g --list2         List2
   -h --help          Show this screen.
   --version          Show version.
"""

def getlist1(one):
    first_list = open(one).readlines()
    return [s.replace('\n', '') for s in first_list]

def getlist2(two):
    second_list = open(two).readlines()
    return [s.replace('\n', '') for s in second_list]

def checklist(list1, list2):
    return list(set(one_set) & set(two_set))    


if __name__ == "__main__":
    from docopt import docopt
    arguments = docopt(__doc__, version= '1')

    list_one = getlist1(arguments['<list1>'])
    list_two = getlist2(arguments['<list2>'])   
    found_items = checklist(list1, list2)
    found_items.sort()
    for item in found_items:
        print item              

Upvotes: 0

Views: 722

Answers (2)

Jeffrey Tang
Jeffrey Tang

Reputation: 793

You're allowing Checker.py (without any arguments) as a usage pattern. Also, surounding (-a <list1>) (-g <list2>) with square brackets allows them to be omitted.

Description:

Compare 2 lists and print the common values.

Usage:
  Checker.py (-a <list1>) (-g <list2>)
  Checker.py -h | --help
  Checker.py --version


Options:
   -a --list1         List1
   -g --list2         List2
   -h --help          Show this screen.
   --version          Show version.

Scroll down to (required elements) in the documentation to see the case where if one element is present, then another one is required.

Another use-case, is when you need to specify that if one element is present, then another one is required, which you can achieve as:

Usage: my_program [(<one-argument> <another-argument>)]

In this case, a valid program invocation could be with either no arguments, or with 2 arguments.

Upvotes: 0

dejay
dejay

Reputation: 768

I'm not sure this completely answers your question, but I hope this helps.

If you run your script without any arguments, arguments['<list1>'] and arguments['<list2>'] both evaluate to None. So:

# ...

if __name__ == "__main__":
    from docopt import docopt
    arguments = docopt(__doc__, version= '1')

    if arguments['<list1>'] == None or arguments['<list2>'] == None:
        print __doc__
        exit(0)

    list_one = getlist1(arguments['<list1>'])
    list_two = getlist2(arguments['<list2>'])   
    found_items = checklist(list1, list2)
    found_items.sort()
    for item in found_items:
        print item    

Upvotes: 2

Related Questions