All pastes #1014506 Raw Edit

Miscellany

public text v1 · immutable
#1014506 ·published 2008-05-11 07:03 UTC
rendered paste body
#!/usr/bin/python

# backup_partition.py
# Daniel Dalton <d.dalton@iinet.net.au>

import sys
global version
version = "backup_partition 0.3 Rev 3 (http://members.iinet.net.au/~ddalton/projects/python/backup_partition/)"

def count_argvs(argvs):
    """Returns the number of arguments supplied to the script.

    Note: that the counting starts at 1 and the filename is also
    included.
    So if you do
    mypy.py
    it will return one, but mypy.py -j will return 2 etc.
    """
    count = 0
    for i in argvs:
        count =count+1
    return count

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."""

    if count_argvs(sys.argv) == 1: # No arguments supplied
        print "Please supply some arguments"
        print "Usage: -b <partition> -v version -h help."
        sys.exit() #Tell the user a descriptive error and exit

    i = count_argvs(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:
        if i == "-h": #print help
            print (usage())
            sys.exit()
        elif i == "-v": #print version info
            print version
            sys.exit()

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