Source

Clap / extra / vowelcount.py

# vowelcount.py

from clap import SingleBase, Command, Option, ScriptError

VOWELS = 'aeiouAEIOU'

class VowelCounterCommand(Command):
    name = 'vowelcount'
    usage = '[OPTIONS] WORD...'
    short_desc = "check words to see how many vowels they contain"
    long_desc = """\
This command will print words that it is given, with the number of vowels \
they contain.
"""
    options = (
        Option('s', 'separator', argument=True, desc="The separator to print "
                "in between the word and the vowel"),
        Option('W', 'no-words', target='words', desc="Whether to print the "
                "actual words or not", constant=False),
        Option('t', 'threshold', type=int, desc="How many vowels there must "
                "be in a word to display it (0 means display all words)"),
        Option('d', 'debug', desc="Print all options and arguments"),
        Option(long='explode', desc="Makes everything explode")
    )
    defaults = dict(separator=' ', words=True, threshold=0, explode=False)
    
    def run(self, options, arguments):
        if 'debug' in options:
            print options
            print arguments
        if options['explode']:
            raise ScriptError("boom", 21)
        for word in arguments:
            count = len(tuple(c for c in word if c in VOWELS))
            self.display(options, word, count)
    
    def display(self, options, word, count):
        if count < options['threshold']:
            return
        if options['words']:
            print "%s%s%d" % (word, options['separator'], count)
        else:
            print count

if __name__ == '__main__':
    base = SingleBase(VowelCounterCommand)
    base.run()