CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM

Bash complete bindings

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By josp

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 3, 2010, 09:00
Default Bash complete bindings
  #1
Member
 
Johan Spång
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 35
Rep Power: 17
josp is on a distinguished road
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.
wyldckat likes this.

Last edited by josp; May 3, 2010 at 10:00. Reason: The script was blocking file completion
josp is offline   Reply With Quote

Old   May 4, 2010, 04:02
Default
  #2
Senior Member
 
akidess's Avatar
 
Anton Kidess
Join Date: May 2009
Location: Germany
Posts: 1,377
Rep Power: 29
akidess will become famous soon enough
Great, thanks for sharing! Instead of having to manually specify the rootdirs, you can use the environment variables $FOAM_APPBIN and $FOAM_USER_APPBIN.
akidess is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
mesh a complete plane horsee FLUENT 13 April 17, 2010 20:20
how to view the complete results shokry FLOW-3D 5 February 3, 2009 13:56
FLOW OVER COMPLETE AIRCRAFT MATT Phoenics 11 November 1, 2005 11:07
Ask for complete theory of T-omega formulation! Hongtao Wang Main CFD Forum 0 May 11, 2005 21:26
Star-HPC, Bash Wak Siemens 1 June 2, 2004 15:44


All times are GMT -4. The time now is 06:33.