-
Notifications
You must be signed in to change notification settings - Fork 22
/
gen-man.sh
executable file
·104 lines (96 loc) · 2.19 KB
/
gen-man.sh
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
#!/bin/bash -e
## Generate man pages for git commands in this directory.
## just start this script in the directory of the commands
## and it will create a folder man/man1 and put the generated
## manpages there.
## call script --help
## parse help
## write man page
## get current date
date=`date +"%B %d, %Y"`
## mk man/man1 dir
mkdir -p man/man1
for CMD in git-*;
do
man="man/man1/$CMD.1"
name=
desc=
synopsis=
options=
usage=0
echo "Creating man page for $CMD..."
help=`$CMD --help`
while IFS="" read line; do
## remove one leading whitespace
line=${line# }
## remove trailing whitespaces
line=${line%% }
## remove any leading whitespaces
cline=`echo $line`
if [[ "$usage" -eq 3 ]]; then
## we're in options
if [[ "${cline:1:1}" == "-" ]]; then
options="$options
$cline"
else
options="$options $cline"
fi
elif [[ "$usage" -eq 2 ]]; then
## we're in Synopsis
if [[ "$cline" == "" ]]; then
usage=3
else
synopsis="$synopsis $cline"
fi
elif [[ "$usage" -eq 1 ]]; then
## we're in Description
if expr "$line" : 'Usage:' > /dev/null; then
usage=2
elif [[ "$cline" != "" ]]; then
if [[ "$cline" == "$line" ]]; then
desc="$desc
$line"
else
desc="$desc
$line
.br"
fi
fi
else
## we're in Name
if [[ "$cline" == "" ]]; then
usage=1
else
name="$name $cline"
fi
fi
done <<< "$help"
## remove leading whitespace, TODO: quote dashes
synopsis=${synopsis:1}
options=${options:1}
desc=${desc:1}
## remove old version if available
if [ -f "$man.gz" ]; then
rm "$man.gz"
fi
## write man page
echo ".TH $CMD 1 \"$date\"" > "$man"
echo ".SH NAME" >> "$man"
echo "$CMD -$name" >> "$man"
echo ".SH SYNOPSIS" >> "$man"
echo ".B $synopsis" >> "$man"
echo ".SH DESCRIPTION" >> "$man"
echo "$desc" >> "$man"
if [[ "$options" != "" ]]; then
echo ".SH OPTIONS" >> "$man"
while read line; do
echo ".TP" >> "$man"
echo "${line%% *}" >> "$man"
echo "${line#* }" >> "$man"
done <<< "$options"
fi
echo ".SH AUTHOR" >> "$man"
echo "Manuel Koller" >> "$man"
## gzip it
gzip "$man"
done