CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   Bash complete bindings (https://www.cfd-online.com/Forums/openfoam/75692-bash-complete-bindings.html)

josp May 3, 2010 09:00

Bash complete bindings
 
Hi, I created a simple python script (40 lines..) for generating bash complete bindings for application arguments. Example:

~/OpenFOAM/OpenFOAM-1.6.x/applications/bin> foamToVTK - [TAB]
-allPatches -case -constant -excludePatches -fields -latestTime -noFaceZones -noLinks -noZero -pointSet -srcDoc -time
-ascii -cellSet -doc -faceSet -help -nearCellValue -noInternal -noPointValues -parallel -region -surfaceFields -useTimeName

To get foamToVTK -latestTime, only foamToVTK -l [TAB] has to be typed in.

The bindings are really basic and only concerns arguments starting with a ' - '

Code:

#basic script for generating bash complete bindings.
import os, subprocess
rootdirs=['/home/josp/OpenFOAM/OpenFOAM-1.6.x/applications/bin/linux64GccDPOpt/',
'/home/josp/OpenFOAM/josp-1.6.x/applications/bin/linux64GccDPOpt']
outFile='./complete_OpenFOAM'

def createFunction(file, arguments):
    return '_OpenFOAMcomplete'+file+'()\n{\n    local cur prev opts\n    COMPREPLY=()\n    cur="${COMP_WORDS[COMP_CWORD]}"\n    \
prev="${COMP_WORDS[COMP_CWORD-1]}"\n    opts="' + arguments+'"\n    if [[ ${cur} == -* ]] ; then\n        COMPREPLY\
=( $(compgen -W "${opts}" -- ${cur}) )\n        return 0\n    fi\n\
    if [[ ${prev} == * ]] ; then\n        COMPREPLY=( $(compgen -f ${cur}) )\n        return 0\n    fi\n}\n'

text = []
execs = []
all=[]

# Grab executable output on -help argument
for rootdir in rootdirs:
    for subdir, dirs, files in os.walk(rootdir):
        for file in files:
            print file
            p =subprocess.Popen([file, '-help'], stdout = subprocess.PIPE)
            if p.wait() == 0: # Check that the executable makes a clean exit, otherwise skip
                execs.append(file)
                text.append(p.communicate()[0].strip('\n'))

# Parse options, really messy. It's probably better to use an xml parser but this works for now..
for i in execs:
    parsed = text[execs.index(i)].split(i)[-1].split('>')[-1].lstrip(' ').replace('  ', ' ').split('] ')
    result = ''
    for k in parsed:
        j = k.split(' ')[0]
        for q in j:
            result += q.lstrip('[').rstrip(']').strip(' ')
    all.append([i, result.replace('-', ' -').lstrip(' '), len(result)])

# Write the bindings to specified outFile
f = open(outFile, 'w')
for i in all:
    f.write(createFunction(str(all.index(i)), i[1])+"complete -F _OpenFOAMcomplete"+str(all.index(i)) + " " + i[0] + "\n\n")
f.close()

Remember to source (i.e. type ".complete_OpenFOAM") the bindings.
Feel free to modify and improve.

akidess May 4, 2010 04:02

Great, thanks for sharing! Instead of having to manually specify the rootdirs, you can use the environment variables $FOAM_APPBIN and $FOAM_USER_APPBIN.


All times are GMT -4. The time now is 21:15.