-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_arg_completion.py
More file actions
36 lines (28 loc) · 845 Bytes
/
cmd_arg_completion.py
File metadata and controls
36 lines (28 loc) · 845 Bytes
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
try:
import gnureadline
import sys
sys.modules["readline"] = gnureadline
except ImportError:
pass
import cmd
class HelloWorld(cmd.Cmd):
FRIENDS = ["Alice", "Adam", "Barbara", "Bob"]
def do_greet(self, person):
"""greet the person"""
if person and person in self.FRIENDS:
greeting = "hi, {}!".format(person)
elif person:
greeting = "hello, {}".format(person)
else:
greeting = "hello"
print(greeting)
def complete_greet(self, text, line, begidx, endidx):
if not text:
completions = self.FRIENDS[:]
else:
completions = [f for f in self.FRIENDS if f.startswith(text)]
return completions
def do_EOF(self, line):
return True
if __name__ == "__main__":
HelloWorld().cmdloop()