40
40
`self.undoc_header' set the headers used for the help function's
41
41
listings of documented functions, miscellaneous topics, and undocumented
42
42
functions respectively.
43
+
44
+ ----------------------------------------------------------------------------
45
+ This is a copy of python's Cmd, but leaves out features that aren't relevant
46
+ or can't currently be implemented for MicroPython.
47
+
48
+ One of the notable deviations is that since MicroPython strips doc strings,
49
+ this means that that help by doc string feature doesn't work.
50
+
51
+ completions have also been stripped out.
43
52
"""
44
53
45
- import string , sys
54
+ #import string, sys
55
+ import sys # MiroPython doesn't yet have a string module
46
56
47
57
__all__ = ["Cmd" ]
48
58
49
59
PROMPT = '(Cmd) '
50
- IDENTCHARS = string .ascii_letters + string .digits + '_'
60
+ #IDENTCHARS = string.ascii_letters + string.digits + '_'
61
+ IDENTCHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
51
62
52
63
class Cmd :
53
64
"""A simple framework for writing line-oriented command interpreters.
@@ -73,13 +84,10 @@ class Cmd:
73
84
nohelp = "*** No help on %s"
74
85
use_rawinput = 1
75
86
76
- def __init__ (self , completekey = 'tab' , stdin = None , stdout = None ):
87
+ def __init__ (self , stdin = None , stdout = None ):
77
88
"""Instantiate a line-oriented interpreter framework.
78
89
79
- The optional argument 'completekey' is the readline name of a
80
- completion key; it defaults to the Tab key. If completekey is
81
- not None and the readline module is available, command completion
82
- is done automatically. The optional arguments stdin and stdout
90
+ The optional arguments stdin and stdout
83
91
specify alternate input and output file objects; if not specified,
84
92
sys.stdin and sys.stdout are used.
85
93
@@ -93,7 +101,6 @@ def __init__(self, completekey='tab', stdin=None, stdout=None):
93
101
else :
94
102
self .stdout = sys .stdout
95
103
self .cmdqueue = []
96
- self .completekey = completekey
97
104
98
105
def cmdloop (self , intro = None ):
99
106
"""Repeatedly issue a prompt, accept input, parse an initial prefix
@@ -103,14 +110,6 @@ def cmdloop(self, intro=None):
103
110
"""
104
111
105
112
self .preloop ()
106
- if self .use_rawinput and self .completekey :
107
- try :
108
- import readline
109
- self .old_completer = readline .get_completer ()
110
- readline .set_completer (self .complete )
111
- readline .parse_and_bind (self .completekey + ": complete" )
112
- except ImportError :
113
- pass
114
113
try :
115
114
if intro is not None :
116
115
self .intro = intro
@@ -139,13 +138,7 @@ def cmdloop(self, intro=None):
139
138
stop = self .postcmd (stop , line )
140
139
self .postloop ()
141
140
finally :
142
- if self .use_rawinput and self .completekey :
143
- try :
144
- import readline
145
- readline .set_completer (self .old_completer )
146
- except ImportError :
147
- pass
148
-
141
+ pass
149
142
150
143
def precmd (self , line ):
151
144
"""Hook method executed just before the command line is
@@ -235,74 +228,18 @@ def default(self, line):
235
228
"""
236
229
self .stdout .write ('*** Unknown syntax: %s\n ' % line )
237
230
238
- def completedefault (self , * ignored ):
239
- """Method called to complete an input line when no command-specific
240
- complete_*() method is available.
241
-
242
- By default, it returns an empty list.
243
-
244
- """
245
- return []
246
-
247
- def completenames (self , text , * ignored ):
248
- dotext = 'do_' + text
249
- return [a [3 :] for a in self .get_names () if a .startswith (dotext )]
250
-
251
- def complete (self , text , state ):
252
- """Return the next possible completion for 'text'.
253
-
254
- If a command has not been entered, then complete against command list.
255
- Otherwise try to call complete_<command> to get list of completions.
256
- """
257
- if state == 0 :
258
- import readline
259
- origline = readline .get_line_buffer ()
260
- line = origline .lstrip ()
261
- stripped = len (origline ) - len (line )
262
- begidx = readline .get_begidx () - stripped
263
- endidx = readline .get_endidx () - stripped
264
- if begidx > 0 :
265
- cmd , args , foo = self .parseline (line )
266
- if cmd == '' :
267
- compfunc = self .completedefault
268
- else :
269
- try :
270
- compfunc = getattr (self , 'complete_' + cmd )
271
- except AttributeError :
272
- compfunc = self .completedefault
273
- else :
274
- compfunc = self .completenames
275
- self .completion_matches = compfunc (text , line , begidx , endidx )
276
- try :
277
- return self .completion_matches [state ]
278
- except IndexError :
279
- return None
280
-
281
231
def get_names (self ):
282
232
# This method used to pull in base class attributes
283
233
# at a time dir() didn't do it yet.
284
234
return dir (self .__class__ )
285
235
286
- def complete_help (self , * args ):
287
- commands = set (self .completenames (* args ))
288
- topics = set (a [5 :] for a in self .get_names ()
289
- if a .startswith ('help_' + args [0 ]))
290
- return list (commands | topics )
291
-
292
236
def do_help (self , arg ):
293
237
'List available commands with "help" or detailed help with "help cmd".'
294
238
if arg :
295
239
# XXX check arg syntax
296
240
try :
297
241
func = getattr (self , 'help_' + arg )
298
242
except AttributeError :
299
- try :
300
- doc = getattr (self , 'do_' + arg ).__doc__
301
- if doc :
302
- self .stdout .write ("%s\n " % str (doc ))
303
- return
304
- except AttributeError :
305
- pass
306
243
self .stdout .write ("%s\n " % str (self .nohelp % (arg ,)))
307
244
return
308
245
func ()
@@ -326,8 +263,6 @@ def do_help(self, arg):
326
263
if cmd in help :
327
264
cmds_doc .append (cmd )
328
265
del help [cmd ]
329
- elif getattr (self , name ).__doc__ :
330
- cmds_doc .append (cmd )
331
266
else :
332
267
cmds_undoc .append (cmd )
333
268
self .stdout .write ("%s\n " % str (self .doc_leader ))
@@ -397,5 +332,6 @@ def columnize(self, list, displaywidth=80):
397
332
while texts and not texts [- 1 ]:
398
333
del texts [- 1 ]
399
334
for col in range (len (texts )):
400
- texts [col ] = texts [col ].ljust (colwidths [col ])
335
+ #texts[col] = texts[col].ljust(colwidths[col])
336
+ texts [col ] = '%-*s' % (colwidths [col ], texts [col ])
401
337
self .stdout .write ("%s\n " % str (" " .join (texts )))
0 commit comments