def Expand(pArgs, pFilePath, pSearchPath, pCount, pSep='!!', pDefault='#!', pList=False, pWidth=15, pVerbose=False):
    '''
    Extract command from file and replace all macros
    pArgs        -- Args passed to program except file path
                    #!key and any arguments
    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
    pSep         -- String used to identify end of command, extra arguments will not be appended
    pList        -- True to display available commands
    pWidth       -- Number of columns to scan for #!
    '''
#   ---- Find command  
    if len(pArgs[1]) == 0:
        lKey = pDefault 
        lStart = 1
    elif pArgs[1].startswith(pDefault):
        lKey = pArgs[1]
        lStart = 2
    else:
        lKey = pDefault
        lStart = 1
            
    lCommand = FindCommand(lKey, pFilePath, pSearchPath, pCount, pDefault=pDefault, pList=pList, pWidth=pWidth, pVerbose=pVerbose)
    
    if lCommand == '':
        print 'pb.py Command', lKey, 'not found'
   
#   ---- Expand and insert/append any passed arguments   
#        Arguments on original pb.py command line will replace {} from left to right
#        otherwise they will be appended to the end of the command 
    if len(lCommand) > 0:
        if len(pArgs) > lStart:
            for lArg in pArgs[lStart:]:
                if lArg.find('{') >= 0:
                    lArg = Do2.ExpandArg(lArg, pFilePath, '')
                lPos = lCommand.find('{}')
                if lPos >= 0:
                    lCommand = lCommand[0:lPos] + lArg + lCommand[lPos+2:]
                else:
                    lCommand += ' ' + lArg
    
#   ---- Prevent unwanted arguments appended to command                    
    lPos = lCommand.rfind(pSep)
    if lPos > 0:
        lCommand = lCommand[0:lPos]
    
#   ---- Expand all remaining macros
    if lCommand.find('{') >= 0:
        lCommand = Do.Expand(lCommand, pFilePath)
    return lCommand

    Extract command from file and replace all macros
    pArgs        -- Args passed to program except file path
                    #!key and any arguments
    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
    pSep         -- String used to identify end of command, extra arguments will not be appended
    pList        -- True to display available commands
    pWidth       -- Number of columns to scan for #!
    
recipes/Python/577454_Extract_expexecute/recipe-577454.py