#!/usr/bin/python3

import re
from os import listdir
from os import path
from sys import argv

def printHelp(name, defaultLists):
    name = path.basename(name)
    print("{} - Detect troublesome words".format(name))
    print("{} [options] <file1> [<file2> ...]".format(name))
    print("\nOPTIONS")
    print("  --help, -h       Display this help message and exit")
    print("  --lists=<lists>  Change the set of word lists.  <lists> should be a")
    print("                   comma-separated list of *builit-in* options to use.")
    print("                   [Default: {}]".format(", ".join(str(l) for l in defaultLists)))
    print("  --list=<list>    Use a custom word list.  <list> should be a path to a text")
    print("                   file with one word per line.")
    print("  --               Stop processing options (required if files to parse match")
    print("                   any of these options)")
    print("\nEXAMPLES")
    print("  {} foo.txt".format(name))
    print("  {} foo.txt bar.txt".format(name))
    print("  {} --lists=filter,weasel foo.txt".format(name))
    print("  {} --list=my_list.txt foo.txt".format(name))
    print("  {} --list=my_list.txt --list=another_list.txt foo.txt".format(name))
    print("  {} --lists=filter --list=mylist.txt foo.txt".format(name))

def addWords(path, words):
    with open(path, "r") as inputList:
        for line in inputList:
            word = line[:-1]
            pattern = re.compile("\\b{}\\b".format(word), re.IGNORECASE)
            words[word] = pattern

def parseWordLists(wordLists):
    global path

    words = { }
    listDir = "{}/../../share/writing-tools/filter-lists".format(path.abspath(path.dirname(argv[0])))

    for wordList in wordLists:
        filePath = "{}/{}-words.txt".format(listDir, wordList)
        addWords(filePath, words)
    return words

def readDefaults():
    global path

    listDir = "{}/../../share/writing-tools/filter-lists".format(path.abspath(path.dirname(argv[0])))
    files = listdir(listDir)
    pattern = re.compile("^([a-z]+)-words.txt$")
    ret = [ ]
    for file in files:
        m = pattern.search(file)
        ret.append(m.group(1))
    ret.sort()
    return ret

if len(argv) > 1:
    # default word lists
    defaultWordLists = readDefaults()
    wordLists = defaultWordLists
    extraLists = [ ]

    index = 1
    # parse optoins
    while index < len(argv):
        if argv[index] == "--help":
            printHelp(argv[0], defaultWordLists)
            exit(0)
        elif argv[index] == "-h":
            printHelp(argv[0], defaultWordLists)
            exit(0)
        elif argv[index] == "--":
            break
        elif re.match("^--lists=", argv[index]):
            newList = argv[index].split('=')[1]
            if len(newList) > 0:
                wordLists = newList.split(",")
            else:
                wordLists = []
        elif re.match("^--list=", argv[index]):
            extraLists.append(argv[index].split('=')[1])
        else:
            # not an option
            break
        index += 1

    words = parseWordLists(wordLists)
    for wordList in extraLists:
        addWords(wordList, words)

    # We should only have files at this point
    while index < len(argv):
        with open(argv[index], 'r') as readFile:
            lineNumber = 0
            for line in readFile:
                lineNumber += 1
                for word, pattern in words.items():
                    match = pattern.search(line)
                    while match:
                        print("{} {} ({}:{})".format(argv[index], word, lineNumber, match.start()))
                        match = pattern.search(line, match.end())
        index += 1
else:
    printHelp(argv[0])

@@ -86,16 +86,39 @@
 ort argv
+%0Afrom sys import stderr
 %0A%0Adef pr
@@ -533,22 +533,20 @@
 lit-in* 
-option
+list
 s to use
@@ -2157,16 +2157,34 @@
 h(file)%0A
+        if m:%0A    
         
@@ -2241,30 +2241,8 @@
 et%0A%0A
-if len(argv) %3E 1:%0A    
 # de
@@ -2258,20 +2258,16 @@
 d lists%0A
-    
 defaultW
@@ -2291,16 +2291,35 @@
 faults()
+%0A%0Aif len(argv) %3E 1:
 %0A    wor
@@ -3205,16 +3205,39 @@
 words)%0A%0A
+    missingFiles = %5B %5D%0A
     # We
@@ -3295,32 +3295,49 @@
 ex %3C len(argv):%0A
+        try:%0A    
         with ope
@@ -3373,32 +3373,36 @@
 le:%0A            
+    
 lineNumber = 0%0A 
@@ -3400,16 +3400,20 @@
 ber = 0%0A
+    
         
@@ -3446,32 +3446,36 @@
                 
+ 
+   
 lineNumber += 1%0A
@@ -3470,24 +3470,28 @@
 Number += 1%0A
+    
             
@@ -3542,32 +3542,36 @@
                 
+ 
+   
 match = pattern.
@@ -3599,24 +3599,28 @@
             
+    
 while match:
@@ -3640,24 +3640,28 @@
             
+  
+  
 print(%22%7B%7D %7B%7D
@@ -3740,32 +3740,36 @@
                 
+    
 match = pattern.
@@ -3802,26 +3802,227 @@
         
-index += 1
+except FileNotFoundError:%0A            missingFiles.append(argv%5Bindex%5D)%0A        index += 1%0A%0A    if len(missingFiles) %3E 0:%0A        print(%22Error opening files: %7B%7D%22.format(missingFiles), file=stderr)%0A        exit(1)
 %0Aelse:%0A 
@@ -4041,10 +4041,28 @@
 (argv%5B0%5D
+, defaultWordLists
 )%0A

Fix a few errors