Package flumotion :: Package extern :: Package command :: Package command :: Module help2man
[hide private]

Source Code for Module flumotion.extern.command.command.help2man

 1  # -*- Mode: Python -*- 
 2  # vi:si:et:sw=4:sts=4:ts=4 
 3   
 4  # This file is released under the standard PSF license. 
 5   
 6  # Copyright (C) 2009 Thomas Vander Stichele 
 7   
 8  import sys 
 9  import StringIO 
10   
11   
12 -def walk(command, level=0):
13 # print "%s%s: %s" % (" " * level, command.name, command.summary) 14 for name, c in command.subCommands.items(): 15 walk(c, level + 2)
16 17
18 -def manwalk(command):
19 ret = [] 20 21 ret.append(".SH %s" % command.getFullName()) 22 # avoid printing the summary twice; once here and once in usage 23 if command.summary and command.description: 24 ret.append(command.summary) 25 ret.append("") 26 27 s = StringIO.StringIO() 28 command.outputHelp(file=s) 29 ret.append(s.getvalue()) 30 ret.append("") 31 32 names = command.subCommands.keys() 33 names.sort() 34 for name in names: 35 c = command.subCommands[name] 36 ret.extend(manwalk(c)) 37 38 return ret
39 40
41 -def main():
42 # inspired by twisted.python.reflect.namedAny 43 names = sys.argv[1].split('.') 44 im = ".".join(names[:-1]) 45 top = __import__(im) 46 obj = top 47 for n in names[1:]: 48 obj = getattr(obj, n) 49 50 # ugly hack so that help output uses first argument for %prog instead of 51 # 'doc.py' 52 sys.argv[0] = sys.argv[2] 53 54 55 man = [] 56 man.append('.TH "rip" "1" "October 2009"') 57 man.extend(manwalk(obj(width=70))) 58 59 print "\n".join(man)
60 61 if __name__ == '__main__': 62 main() 63