def FindCommand(pKey, pFilePath, pSearchPath, pCount=10, pDefault='#!', pList=False, pWidth=10, pVerbose=False):
    '''
    Find command keyword and extract containing line
    pKey         -- String identifying line to use as command template
    pFilePath    -- File to use in macro expansion
    pSearchPath  -- File to scan for command '
    pCount       -- Number of lines at the start of the file to scan
    pDefault     -- Prefix for keys, from UNIX #!
    pList        -- True to display available commands
    pWidth       -- Number of columns at start of line in which key must appear
    '''
    lShortest = 6         # A line shorter than this will abort scan
    
    if not os.path.exists(pSearchPath):
        lCommand = ''
        print 'pb.py Could not find search file', pSearchPath
        
    else:
#       ---- Check allowed key values    
        if pKey == '' or pKey == pDefault:          #   #!
            lKey = pDefault 
        elif pKey.find('{') >= 0:                   #   #!{e}
            lKey = pDefault + Do.Expand(pKey, pFilePath)
            if not lKey.startswith(pDefault):
                lKey = pDefault + lKey
        elif pKey.startswith(pDefault):             #   #!text
            lKey = pKey
        else:
            lKey = pDefault + pKey                  #   text
            lKey = lKey.lower()
#        lKey += ' '                                #   Full key only

#       ---- read maximum scan lines
        lFile = open(pSearchPath, 'r')
        lText = lFile.readlines(pCount)
        lText = lText[0:pCount]                     #???
        lFile.close()
        
        lCommand = ''

#       ---- list available commands
        if pList:
            lText = [x for x in lText if x.find(pDefault) >= 0 ]
            lText = [x for x in lText if x.find(pDefault) < pWidth ]
            print 'Available Commands in', pSearchPath
            for (lCount, lLine) in enumerate(lText):
                lLine = lLine.strip()
                print "%5d: %s" % (lCount, lLine)

#       ---- Scan for selected command
#            Since comments in some languages do not start with first character of key,
#            the key need not appear at start of line but must appear within first pWidth
#            columns to allow for skipping lines with key that are not to be matched
        else:   
            lCount = 0     
            for lLine in lText:
                if len(lLine.strip()) < lShortest:              # blank or short line will stop scan
                    break
                lLine = lLine.lower()
                lPos = lLine.find(lKey.lower()) 
                if pVerbose:
                    lCount += 1
                    print "pb.py %5d: %s" % (lCount, lLine),
                    if lPos >= 0:
                        lAt = lPos + 13
                        print ' ' * lAt + '^'                
                if  lPos >= 0 and lPos < pWidth:  
#                   ---- Full key only                
#                    lCommand = lLine[lPos+len(lKey):]           
#                   ---- Partial keys allowed                     
                    lCommand = lLine[lPos+len(lKey):]          
                    if lCommand[0] == ' ':                 # matched full key
                        lCommand = lCommand.lstrip()
                    else:
                        lPos = lCommand.find(' ')          # matched partial key
                        lCommand = lCommand[lPos:].lstrip()
                        
                    break
    return lCommand[0:-1]

    Find command keyword and extract containing line
    pKey         -- String identifying line to use as command template
    pFilePath    -- File to use in macro expansion
    pSearchPath  -- File to scan for command '
    pCount       -- Number of lines at the start of the file to scan
    pDefault     -- Prefix for keys, from UNIX #!
    pList        -- True to display available commands
    pWidth       -- Number of columns at start of line in which key must appear
    
recipes/Python/577454_Extract_expexecute/recipe-577454.py