""" TEXTSTATS

A program example to learn python programing.

This initial multi-line string is a comment which will show up in help.
"""

# This is a comment, used for in-line documentation

### This is a higher level comment. Format may vary do what you want.

### Setup
import sys

### Function definition: This function has one mandatory parameter
#       which is text and an optional parameter which is minwordlen.
def printstats(text, minwordlen=0):
    """ Prints statistics of a text. Parameters are:
            text: a string with the text to analyze
            minwordlen: only words longer than this are considered
        Return: The number of words longer than minwordlen-1 in the text
    """
    # Select all words longer than a certain number and add characters
    words = []
    nchars = 0
    for word in text.split():
        if len(word) > minwordlen:
            words.append(word)
            nchars += len(word)
    # sort words
    print("Total text length is %d characters" % len(text) )
    print("There are %d words with %d or more characters" % (len(words),minwordlen))
    print("    These words have a combined total of %d characters" % nchars)
    return len(words)

### Examples for using the function (uncomment one at a time)
#printstats('It was the best of times, it was the worst of times.')
#printstats()
#printstats('It was the best of times, it was the worst of times.',4)

### Main function
def main():
    # Print command line arguments
    print("Sys.argv = %s" % repr(sys.argv))
    # Use first command line argument as the text
    if len(sys.argv) == 2:
        printstats(sys.argv[1])
    elif len(sys.argv) > 2:
        printstats(sys.argv[1], int(sys.argv[2]))
    else:
        print("Missing arguments")
        text = raw_input("Please enter text:")
        printstats(text)

# If this file is the main program, call the main function
if __name__ == '__main__':
    main()
    pass
