From e025aad748bfcd39891fcf5bfc706d3c6bbd3990 Mon Sep 17 00:00:00 2001 From: Stefan Huber Date: Sun, 13 May 2012 19:04:04 +0200 Subject: [PATCH] adding cmdline args --- shbackup.py | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) 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() -- 2.39.5