From b637461c9e565ea276d6eda0893a5799bdd10739 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Fri, 17 Jan 2014 15:09:05 +0100 Subject: [PATCH] dvr-manage: Restructure usage --- dvr-managedb | 69 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/dvr-managedb b/dvr-managedb index fc81930..a47d956 100755 --- a/dvr-managedb +++ b/dvr-managedb @@ -148,25 +148,6 @@ class Database: self.conn.commit() -def usage(): - """Print help text.""" - - print(""" -USAGE: - {0} -d FILE [OPTIONS] - {0} -h - -OPTIONS: - -a PATH Read webservers response and add data to the database. If PATH is - "-" then read from stdin. If PATH is a file, read from file. If - file ends with ".gz" then transparently decompress it. If PATH is - a directory, read all files ending with ".html" or ".html.gz". - -d FILE Use given sqlite3 database. - -h Print this help text. - -q COND Query for datasets where the given WHERE SQL-condition holds. -""") - - def printDataset(db, id): """Print info for a dataset of given ID.""" @@ -303,18 +284,42 @@ def parseAndInsert(db, htmldata): db.add_purpose(id, num, purpose, date, status) +def usage(): + """Print help text.""" + + print(""" +USAGE: + {0} [OPTIONS] COMMAND [ARGS...] + {0} -d FILE -a PATH [PATH...] + {0} -d FILE -q COND -l + {0} -h + +COMMAND: + -a Read webservers response and add data to the database. If PATH is + "-" then read from stdin. If PATH is a file, read from file. If + file ends with ".gz" then transparently decompress it. If PATH is + a directory, read all files ending with ".html" or ".html.gz". + -h Print this help text. + -l List the selected datasets. + +OPTIONS: + -d FILE Use given sqlite3 database. + -q COND Use this WHERE SQL-condition to select some datasets. +""") + + if __name__ == "__main__": - add = None dbfn = None query = None + cmd = None try: - opts, args = getopt.getopt(sys.argv[1:], "a:d:hq:") + opts, args = getopt.getopt(sys.argv[1:], "ad:hq:l") for opt, arg in opts: - if opt == "-a": - add = arg + if opt in ["-a", "-l"]: + cmd = opt elif opt == "-d": dbfn = arg elif opt == "-h": @@ -331,18 +336,24 @@ if __name__ == "__main__": usage() sys.exit(os.EX_USAGE) + if cmd is None: + print("No command given.", file=sys.stderr) + sys.exit(os.EX_USAGE) + if dbfn is None: print("No database given.", file=sys.stderr) sys.exit(os.EX_USAGE) db = Database(dbfn) - if add is not None: - processAdd(db, add) + if cmd == "-a": + for arg in args: + processAdd(db, arg) - if query is not None: - success = processQuery(db, query) - if not success: - sys.exit(1) + if cmd == "-l": + if query is None: + print("No query option given.", file=sys.stderr) + sys.exit(os.EX_USAGE) + processQuery(db, query) sys.exit(os.EX_OK) -- 2.39.5