-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickman.py
102 lines (88 loc) · 3.72 KB
/
quickman.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
import subprocess
class QuickMan:
def __init__(self):
pass
def print_help(self):
print("Usage: ./qm.py command_name [flags/options]")
def print_command_info(self, man_output):
end_on_empty_line = False
for line in man_output[2:]:
if line.startswith("DESCRIPTION"):
end_on_empty_line = True
print(line)
if not line and end_on_empty_line:
break
def print_flag_option(self, man_output, line_index):
if self.print_flag_option_header:
print("SPECIFIED FLAGS AND OPTIONS")
self.print_flag_option_header = False
first_line = True
while line_index < len(man_output) and man_output[line_index]:
line = man_output[line_index].strip()
if first_line:
print(" " * 5, line, sep='')
first_line = False
else:
print(" " * 12, line, sep='')
line_index += 1
print()
def get_separated_args(self, args):
actual_args = []
for arg in args:
if arg.startswith("--") and len(arg) > 3:
index = 0
for arg_char in arg:
if not arg_char.isalpha() and arg_char != '-':
break
index += 1
actual_args.append(arg[2:index])
elif arg[0] == '-':
sub_arg_list = [sub_arg for sub_arg in arg[1:] if sub_arg.isalpha()]
if len(sub_arg_list) == len(arg) - 1:
actual_args += sub_arg_list
return actual_args
def check_flag_option(self, man_output, line_indices, args):
self.print_flag_option_header = True
args = self.get_separated_args(args)
for arg in args:
for index in line_indices:
next_arg = False
man_args = man_output[index].split(" ")
for man_arg in man_args:
man_arg = man_arg.strip(', ')
if not man_arg:
continue
elif man_arg[0] != '-':
break
if (len(man_arg) > 1 and
(len(arg) == 1 and arg == man_arg[1]) or
(len(arg) != 1 and man_arg[2:].startswith(arg)
and (len(man_arg) <= (len(arg) + 2)
or not man_arg[len(arg) + 2].isalpha()
and man_arg[len(arg) + 2] != '-'))):
self.print_flag_option(man_output, index)
next_arg = True
break
if next_arg:
break
def get_flag_option_indices(self, man_output):
line_indices = []
for line_index, man_output in enumerate(man_output):
stripped_line = man_output.strip()
if stripped_line and stripped_line[0] == '-':
line_indices.append(line_index)
return line_indices
def parse_command(self, command):
if not command or command[0] == "--help":
self.print_help()
return
man_command_output = subprocess.run(['env', 'COLUMNS=32767', 'man', command[0]],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
man_output = man_command_output.stdout.decode('utf-8').strip().split("\n")
man_error = man_command_output.stderr.decode('utf-8').strip()
if man_error.startswith("No manual"):
print(man_error)
return
self.print_command_info(man_output)
line_indices = self.get_flag_option_indices(man_output)
self.check_flag_option(man_output, line_indices, command[1:])