#!/usr/bin/python # backup_partition.py # Daniel Dalton import sys import os global version version = "backup_partition 0.3 Rev 4 (http://members.iinet.net.au/~ddalton/projects/python/backup_partition/)" def cmdoptions(): """ Checks how many argvs there are using the help of the function count_argvs(). Then it looks at each argument and does a certain action depending on the argument. Uses a loop to do this, see below for details.""" backup = False #Don't yet create a backup since -b has not been entered if len(sys.argv) == 1: # No arguments supplied print "Please supply some arguments" print "Usage: -b -v version -h help." sys.exit() #Tell the user a descriptive error and exit i = len(sys.argv) #Assign number of arguments to i # Following loop checks each arg (stops when it runs out of argvs, # by looking at i and #Choosing an action in the if block. #Remember i has the number of argvs returned from count_argvs() for i in sys.argv[1:]: if i == "-h": #print help print (usage()) sys.exit() elif i == "-v": #print version info print version sys.exit() elif i == "-b": #Get info for backing up backup = True continue elif backup == True: if not os.path.exists(i): print "The path", i ,"does not exist." sys.exit() elif os.path.exists(i): backup = False #So the right messages are printed and the loop doesn't execute this code all the time print "The path", i ,"does exist" if backup == True: #Should be executed if not partition path is supplied... print "Please enter the path to a partition if you are using -b" def usage(): """Prints the command line arguments available. One of the main documentation functions available. Accessed with -h and maybe from other places.""" #Assign a long string to the variable help return the variable help #and then the function calling us can do whatever it likes with it. help = """ Still needs a bit of work... Perhaps docstrings should be used, but not up to documentation yet... """ return help cmdoptions() #Call the main function to choose an action for each #argument supplied, also tells user about errors