1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{help [OSH_COMMAND]}
19
20 If C{OSH_COMMAND} is specified, print usage information for the named osh command. Otherwise, list
21 the builtin commands.
22 """
23
24 import sys
25
26 import osh.core
27
28
31
32
33 -def help(osh_command = None):
34 """Prints usage information on osh commands. If C{osh_command} is specified,
35 print usage information for the named osh command. Otherwise, list the builtin commands.
36 """
37 return _Help()
38
39 -class _Help(osh.core.Generator):
40
41 _command_name = None
42
43
44
45
48
49
50
51
54
56 args = self.args()
57 if args.has_next():
58 self._command_name = args.next_string()
59 if args.has_next():
60 self.usage()
61
62
63
65 if self._command_name:
66 try:
67 top_level_module = __import__('osh.command.' + self._command_name)
68 module = getattr(top_level_module, 'command')
69 module = getattr(module, self._command_name)
70 print _remove_epydoc_markup(module.__doc__)
71 except Exception, e:
72 print >>sys.stderr, "Can't find documentation for %s: %s" % (self._command_name, e)
73 else:
74 import osh.command
75 import osh.command.help
76 print _remove_epydoc_markup(__doc__)
77 print 'Builtin commands:'
78 for command in osh.command.cli:
79 print '\t', command
80
81
82
83
84 _MARKUP = ['B{', 'C{', 'i{', '::']
85
87 nearest_markup = None
88 nearest_start = len(text)
89 for i in xrange(len(_MARKUP)):
90 markup = _MARKUP[i]
91 position = text.find(markup, start)
92 if position != -1 and position < nearest_start:
93 nearest_markup = markup
94 nearest_start = position
95 if nearest_markup:
96 if '{' in nearest_markup:
97 markup_end = text.find('}', nearest_start)
98 assert markup_end > nearest_start
99 else:
100 markup_end = None
101 return nearest_start, nearest_markup, markup_end
102 else:
103 return None, None, None
104
106 plain = []
107 plain_start = 0
108 while plain_start < len(text):
109 markup_start, markup, markup_end = _nearest_markup(text, plain_start)
110 if markup_start is None:
111
112 plain.append(text[plain_start :])
113 plain_start = len(text)
114 else:
115
116 plain.append(text[plain_start : markup_start])
117 if '{' in markup:
118
119 assert markup_end is not None, markup_end
120 plain.append(text[markup_start + len(markup) : markup_end])
121 plain_start = markup_end + 1
122 else:
123 plain.append(':')
124 plain_start = markup_start + len(markup)
125 return ''.join(plain)
126