-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp
More file actions
executable file
·92 lines (75 loc) · 3.05 KB
/
help
File metadata and controls
executable file
·92 lines (75 loc) · 3.05 KB
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
#!/usr/bin/env python3
# Prints the help message for a Rox workflow command.
# Usage: roxhelp <command_name> OR roxhelp --list-all to list all commands.
from __future__ import print_function
import os
import sys
from distutils.spawn import find_executable
def print_not_found_and_exit(command):
print("Couldn't find command: {}".format(command))
print("Either the command doesn't exist, or you haven't added the bin folder to your path.")
sys.exit(1)
def list_commands():
dirname = os.path.abspath(os.path.dirname(__file__))
# Be resilient to the script being invoked directly without the symlink.
if dirname.endswith("workflow/scripts"):
dirname = dirname[:-len("scripts")] + "bin"
if not dirname.endswith("workflow/bin"):
print("Error finding the directory with commands. "
"Is this script inside the workflow repository?")
sys.exit(1)
commands = os.listdir(dirname)
if len(commands) == 0:
print("Couldn't find any commands in {}.".format(dirname))
sys.exit(1)
for command in sorted(commands):
help_lines = get_help_lines(os.path.join(dirname, command))
first_help_line = ""
if len(help_lines) > 0:
first_help_line = " - {}".format(help_lines[0])
print("{}{}".format(command, first_help_line))
def get_help_lines(command):
# The path to the symlink in bin
command_file = find_executable(command)
if command_file is None:
print_not_found_and_exit(command)
# Dereference the symlink
real_path = os.path.realpath(command_file)
if not os.path.isfile(real_path):
print_not_found_and_exit(command)
with open(real_path, 'r') as f:
help_lines = []
prev_line_empty = False
for line in f:
s_line = line.strip()
if len(s_line) == 0:
if prev_line_empty:
break
prev_line_empty = True
continue
prev_line_empty = False
if s_line.startswith('#!'):
continue
if not s_line.startswith('#'):
break
s_line = s_line[1:] # Strip '#'
if s_line.startswith(' '): # Strip space following '#', if any
s_line = s_line[1:]
help_lines.append(s_line)
return help_lines
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage:\n"
"{} <command_name> (to print help for command <command_name>)\n"
"{} --list-all (to list all available Rox commands)".format(sys.argv[0], sys.argv[0]))
sys.exit(1)
if sys.argv[1] == "--list-all":
list_commands()
sys.exit(0)
command = sys.argv[1]
help_lines = get_help_lines(command)
if len(help_lines) == 0:
print("Command {} doesn't appear to have an explanation. Please consider adding one.\n"
"Comments at the top of the script are interpreted as explanations.".format(command))
sys.exit(1)
print("Help for {}:\n{}".format(command, '\n'.join(help_lines)))