1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
"""
Customizations of Python cmd module.
$Id$
Copyright (C) 2010 Internet Systems Consortium ("ISC")
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
import cmd, glob
try:
import readline
have_readline = True
except ImportError:
have_readline = False
class Cmd(cmd.Cmd):
emptyline_repeats_last_command = False
EOF_exits_command_loop = True
identchars = cmd.IDENTCHARS + "/-."
histfile = None
def __init__(self, argv = None):
cmd.Cmd.__init__(self)
if argv:
self.onecmd(" ".join(argv))
else:
self.cmdloop_with_history()
def do_EOF(self, arg):
if self.EOF_exits_command_loop and self.prompt:
print
return self.EOF_exits_command_loop
def do_exit(self, arg):
return True
do_quit = do_exit
def emptyline(self):
if self.emptyline_repeats_last_command:
cmd.Cmd.emptyline(self)
def filename_complete(self, text, line, begidx, endidx):
return glob.glob(text + "*")
if have_readline:
def cmdloop_with_history(self):
old_completer_delims = readline.get_completer_delims()
if self.histfile is not None:
try:
readline.read_history_file(self.histfile)
except IOError:
pass
try:
readline.set_completer_delims("".join(set(old_completer_delims) - set(self.identchars)))
self.cmdloop()
finally:
if self.histfile is not None and readline.get_current_history_length():
readline.write_history_file(self.histfile)
readline.set_completer_delims(old_completer_delims)
else:
cmdloop_with_history = cmd.Cmd.cmdloop
def yes_or_no(prompt, full_word_required = False):
"""
Ask a yes-or-no question.
"""
while True:
answer = raw_input(prompt).strip().lower()
if answer == "yes" or (not full_word_required and answer[0] == "y"):
return True
if answer == "no" or (not full_word_required and answer[0] == "n"):
return False
print 'Please answer "yes" or "no"'
|