]> git.sthu.org Git - dvrdb.git/commitdiff
dvr-manage: Restructure usage
authorStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 14:09:05 +0000 (15:09 +0100)
committerStefan Huber <shuber@sthu.org>
Fri, 17 Jan 2014 14:09:05 +0000 (15:09 +0100)
dvr-managedb

index fc8193066c99755f16c416c39873ca53147e2f2e..a47d956183aa2182b690cd8bc94dec5fd48716d6 100755 (executable)
@@ -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)