-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_template.py
109 lines (88 loc) · 2.5 KB
/
cli_template.py
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
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# she bangs well...
"""Unix Python Cli Template.
Usage:
cli_template.py test this <name>...
cli_template.py more test <x> <y>
cli_template.py (-h | --help)
cli_template.py --interactive
cli_template.py --version
Options:
-h --help Show this screen.
-i --interactive Goes into interactive mode
-v --version Show version.
"""
from docopt import docopt # for nifty arg parsing
# for the docopt syntax check http://docopt.org/
import readline
import completer # for tab completion
import sys
# sweet sweet colors
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def help_text(rest):
print "this is a help text in interactive mode"
return ''
def config(rest):
print "configuration options"
return 'inside'
def exit_program(rest):
print "exiting from interactive mode"
sys.exit(0)
return ''
def other(rest):
print "other"
return 'menu'
COMMANDS = {
'menu': {
'help': help_text,
'config': config,
'exit': exit_program
},
'inside': {
'help': help_text,
'other': other,
'exit': exit_program
}
}
DEFAULT_MODE = 'menu'
comp = completer.Completer(DEFAULT_MODE, COMMANDS)
readline.set_completer_delims(' \t\n;')
readline.parse_and_bind("tab:complete")
readline.set_completer(comp.complete)
def interactive_loop():
while 1:
sys.stdout.write(HEADER+comp.mode+OKGREEN+" > "+ENDC)
user_input = raw_input()
splitted_command = user_input.partition(' ')
command, rest = splitted_command[0], splitted_command[2]
if command in COMMANDS[comp.mode].keys():
next_mode = COMMANDS[comp.mode][command](rest)
if next_mode != '':
comp.mode = next_mode
if __name__ == '__main__':
arguments = docopt(__doc__, version='Unix Cli Template 0.1')
print "Going to print a debug of the arguments passed"
# returns a dictionary/hash with the options filled by the user
print(arguments)
print "End of printing"
"""
For instance:
python cli_template.py test this bob #will return
{'--help': False,
'--version': False,
'<name>': ['bob'],
'<x>': None,
'<y>': None,
'more': False,
'test': True,
'this': True}
"""
# here goes the parsing of arguments
if arguments['--interactive'] is True:
print "Entering interactive mode, press <TAB> for completion"
interactive_loop()