-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhe
executable file
·69 lines (62 loc) · 1.49 KB
/
he
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
#!/bin/sh
# he - print brief help about a single option or command
# Mikel Ward <[email protected]>
# Example Usage:
# he bash continue
# he rsync -v
scriptname=he
usage()
{
cat <<EOF 1>&2
Usage: $scriptname <command> [<option|section>]
Example:
$scriptname bash getopts (shows documentation for bash getopts)
$scriptname ssh -v (shows documentation for ssh -v flag)
$scriptname select (shows SYNOPSIS for select(2))
$scriptname 'open(2)' (shows SYNOPSIS for open(2))
EOF
}
if test $# -lt 1; then
usage
exit 2
fi
manpage="$1"
# show the SYNOPSIS section if no section or option was given
option="${2:-SYNOPSIS}"
# handle manpage(number)
case $manpage in *\(*\))
page=${manpage%\(*\)}
section=${manpage#$page}
section=${section#\(}
section=${section%\)}
manpage=$page
;;
esac
man ${section:+-s $section} "$manpage" | perl -n -e '
BEGIN {
$option = "'$option'";
$inside_option = 0;
}
if (!$inside_option) {
if (/^(\s*)\Q$option\E\b/p) {
# start of this option
$option_indentation = $1;
$inside_option = 1;
$saw_blank_line = 0;
print;
}
} else {
if (/^$/) {
$saw_blank_line = 1;
print;
} elsif (/^\Q$option_indentation\E\S/ and $saw_blank_line) {
# item at same indentation => start of next option
$inside_option = 0;
} elsif (/^\S/) {
# new heading => start of next section
$inside_option = 0;
} else {
print;
}
}
'