]> git.sthu.org Git - sitarba.git/commitdiff
adding cmdline args
authorStefan Huber <shuber2@gmx.at>
Sun, 13 May 2012 17:04:04 +0000 (19:04 +0200)
committerStefan Huber <shuber2@gmx.at>
Sun, 13 May 2012 17:04:04 +0000 (19:04 +0200)
shbackup.py

index 41b24fe125686121c4a082d788546d8a66b27dc7..f9f700719f4bc8c950fea2099062c57dbd0ce68a 100755 (executable)
@@ -51,6 +51,8 @@ class Config:
     def read(self, filename):
         """Read configuration from file"""
 
+        if not os.path.isfile(filename):
+            raise Config.ReadException("No file '" + filename + "'.")
 
         config = configparser.RawConfigParser()
         config.read(filename)
@@ -351,15 +353,51 @@ class BackupManager:
                 shutil.rmtree(os.path.join(basedir, d))
 
 
+def printUsage():
+    """Print --help text"""
+
+    print("shbackup - a simple backup solution.")
+    print("")
+    print("Usage:")
+    print("  " + sys.argv[0] + " [-C <configfile>")
+    print("  " + sys.argv[0] + " --help")
+    print("")
+    print("Options:")
+    print("  -C <configfile>        default: /etc/shbackup.conf")
+
+
 if __name__ == "__main__":
 
-    conffn = "shbackup.conf"
-    if len(sys.argv) > 1:
-        conffn = sys.argv[1]
+    conffn = "/etc/shbackup.conf"
+
+    i=0
+    while i < len(sys.argv)-1:
+        i += 1
+        opt = sys.argv[i]
+
+        if opt in ["-h", "--help"]:
+            printUsage()
+            exit(0)
+
+        elif opt in ["-C", "--config"]:
+            i += 1
+            conffn = sys.argv[i]
+            continue
+
+        else:
+            print("Unknown option: " + opt)
+            exit(1)
+
+    try:
+        man = BackupManager(conffn)
+        man.backup()
+        man.prune()
 
-    man = BackupManager(conffn)
-    man.backup()
-    man.prune()
+    except Config.ReadException as e:
+        print("Error reading config file: ", end="")
+        for a in e.args:
+            print(a, end=" ")
+        print()