drime

French rhyme dictionary with web and CLI interface
git clone https://a3nm.net/git/drime/
Log | Files | Refs | README

commit e01ddcf8c5218bab17ccd41ce6fda6674ce20b36
parent 94b9b60bd40722e2613ba7c7488e819d5b9e51e8
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Fri, 30 Dec 2011 14:15:47 +0100

improve cli interface

Diffstat:
query.py | 28+++++++++++++++++++++++++---
1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/query.py b/query.py @@ -25,7 +25,10 @@ def query(q, nsyl='', gender=True, page=0): if not page: page = 0 else: - page = int(page) + try: + page = int(page) + except ValueError: + raise BadValues if not nsyl: nsyl = '' if not q: @@ -168,6 +171,25 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): return {'keys': [key], 'result': result2}, count if __name__ == '__main__': - r, c = query(*sys.argv[1:]) - print(json.dumps(r, indent=4)) + def usage(): + print ("Usage: %s QUERY [NSYL [GENDER]]" % sys.argv[0]) + try: + r, c = query(*sys.argv[1:]) + except BadValues: + print ("Bad values passed as arguments.") + usage() + sys.exit(4) + except TypeError: + usage() + sys.exit(4) + if len(r['keys']) == 0: + print ("No interpretation found for %s" % sys.argv[1]) + sys.exit(1) + if len(r['keys']) > 1: + print ("Multiple interpretations found for %s:" % sys.argv[1]) + for k in r['keys']: + print (" - %s" % k[2]) + print ("Please rerun with a more specific query") + sys.exit(2) + print(json.dumps(r['result'], indent=4))