From: Stefan Huber Date: Sun, 13 May 2012 17:04:04 +0000 (+0200) Subject: adding cmdline args X-Git-Tag: v1.0~19 X-Git-Url: https://git.sthu.org/?a=commitdiff_plain;h=e025aad748bfcd39891fcf5bfc706d3c6bbd3990;p=sitarba.git adding cmdline args --- diff --git a/shbackup.py b/shbackup.py index 41b24fe..f9f7007 100755 --- a/shbackup.py +++ b/shbackup.py @@ -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 ") + print(" " + sys.argv[0] + " --help") + print("") + print("Options:") + print(" -C 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()