summaryrefslogtreecommitdiff
path: root/scripts/help2man.lua
blob: ab23733e792f548983bce965330d7716be4ec572 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env lua

local format_br = '\n.br\n'
local tools = {
	"wasm-decompile",	"wasm-interp",	"wasm-objdump",	"wasm-stats",
	"wasm-strip",	"wasm-validate",	"wasm2c",	"wasm2wat",
	"wast2json",	"wat-desugar",	"wat2wasm",	"spectest-interp"
}

local name, mainarg, argvs
local usage
local short = ""
local full = {}
local examples = {}
local options = {}

local last = { }

do -- parse usage
	local usage = io.read('*line');
	name, mainarg, argvs = usage:match("usage: ([^%s%c]+) %[options%] ([^%s%c]+) %[([^%s%c]+)%]%.%.%.")
	if not argvs then
		name, mainarg = usage:match("usage: ([^%s%c]+) %[options%] ([^%s%c]+)")
	end
end

do -- read short description
	repeat line = io.read('*line');
		short = short .. ' ' .. line
	until line == ""
end

do -- read full description
	repeat line = io.read('*line');
		table.insert(full, line)
	until line == "examples:"
	do -- format full description
		table.remove(full, #full) -- remove "examples:"
		full = table.concat(full, "") -- concat everything with a space
		full = full:gsub("^  ", "")
		full = full:gsub("\n  ", "")
		-- full = full:gsub("  +", "\n")
		full:gsub('\n', format_br)
	end
end


do -- parse examples
	line = io.read('*line'); -- skip "examples:"
	last = { descr = {} } -- prepare new example
	while line ~= "options:" do
		-- empty lines are between examples
		if line:match("^%s*$") then
			-- format last
			local descr = last.descr
			descr = table.concat(descr, '\n')
			last.descr = descr
			-- push last
			table.insert(examples, last)
			-- prepare new example
			last = { descr = {} }
		end
		
		-- found the sample usage
		if line:match("^%s*%$ ") then
			last.input = line:match("^ *%$ (.+)")
		end
		-- found some description
		if line:match("^ *# ") then
			table.insert(last.descr, (line:gsub("^ *# ", "")))
		end
		
		line = io.read('*line');
	end
end


do -- parse options
	last = { }
	for line in io.stdin:lines() do
		if line:match("^ +%-") then
			table.insert(options, last)
			
			local short, long, descr = line:match("^  %-(%w), %-%-([^%s%c]+) +(.+)")
			if not short then
				long, descr = line:match("^      %-%-([^%s%c]+) +(.+)")
			end
			last = {
				short = short,
				long = long,
				descr = descr,
			}
		else
			last.descr = last.descr .. line:gsub("^ +", "\n")
		end
	end
	table.remove(options, 1)
	last = { }
end


-- remove this tool from the SEE ALSO section
for index, tool in pairs(tools) do
	if tool == name then
		table.remove(tools, index)
		break
	end
end



do -- print the man page
	print(".Dd $Mdocdate$")
	print(".Os")
	print(".Sh NAME")
	print((".Nm %s"):format(name))
	print((".Nd %s"):format(short))
	print(".Sh SYNOPSIS")
	print((".Nm %s"):format(name))
	print(".Op options")
	print((".Ar %s"):format(mainarg))
	if argvs then
		print((".Ar [%s]..."):format(argvs))
	end
	print(".Sh DESCRIPTION")
	print(".Nm")
	print(full)
	print(".Pp")
	print("The options are as follows:")
	print(".Bl -tag -width Ds")
	for index, option in pairs(options) do
		if option.short then
			print((".It Fl %s , Fl Fl %s"):format(option.short, option.long))
		else
			print((".It Fl Fl %s"):format(option.long))
		end
		print(option.descr)
	end
	print(".El")
	print(".Sh EXAMPLES")
	for index, example in pairs(examples) do
		print(example.descr)
		print(".Pp")
		print((".Dl $ %s"):format(example.input))
	end
	print(".Sh SEE ALSO")
	for idx, tool in pairs(tools) do
		print((".Xr %s 1 %s"):format(tool, idx == #tool and "" or ","))
	end
	print(".Sh BUGS")
	print("If you find a bug, please report it at")
	print(".br")
	print(".Lk https://github.com/WebAssembly/wabt/issues .")
end