-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
manop
executable file
·118 lines (104 loc) · 2.7 KB
/
manop
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
#!/usr/bin/env bash
########################
# Author: Shinichi Okada
# Date: 2021-05-26
###########################
# set defaults
version="v0.1.6"
script_name=$(basename "$0")
usage() {
cat <<EOF
Name: $script_name
Version: $version
Description: This script outputs a command description or an option description from the man page.
Usage:
$script_name [ -t | -h | -v][ command name ][ command option ]
-t | --trim Trim spaces
-v | --version Show version
-h | --help Help
Examples:
Shows wget -b option description.
$ manop wget -b
Shows the ls command description.
$ manop ls
Shows builtin commands description.
$ manop cd
Shows builtin cd -e option description.
$ manop cd -e
Trim spaces
$ manop -t grep -r
To display builtin command help use help
$ help cd
EOF
exit 2
}
unset trim
if (($# > 0)); then
case "$1" in
-t | --trim)
trim=true
shift
;;
-v | --version)
echo $version
exit 0
;;
-h | --help)
usage
exit 0
;;
esac
else
usage
exit 0
fi
# find all builtin commands and decide the command name either
# man or help.
if type "$1" | grep -q "builtin"; then
use=help
else
use=man
fi
# help doesn't have Description nor empty lines after
# option description.
if [[ $use == "man" ]]; then
if [[ $# -eq 2 ]]; then
if [[ $trim == true ]]; then
# If two args given, output the option description
$use "$1" | col -bx | sed -n "/^ *$2/,/^$/p" | sed 's/ */ /g'
exit 0
else
# If two args given, output the option description
$use "$1" | col -bx | sed -n "/^ *$2/,/^$/p"
exit 0
fi
else
# otherwise display the command description
if [[ $trim == true ]]; then
$use "$1" | col -bx | sed -n '/^DESCRIPTION/,/^$/p' | sed 's/ */ /g'
exit 0
else
$use "$1" | col -bx | sed -n '/^DESCRIPTION/,/^$/p'
fi
fi
else
if [[ $# -eq 2 ]]; then
if [[ $trim = true ]]; then
# If two args given, output from the option description to the end of line since there is no empty lines in help outputs.
$use "$1" | col -bx | sed -n "/^ *$2/,/$/p" | sed 's/ */ /g'
exit 0
else
$use "$1" | col -bx | sed -n "/^ *$2/,/$/p"
exit 0
fi
else
if [[ $trim = true ]]; then
# output from the first line to before Option
$use "$1" | col -bx | sed '/Option/,$d' | sed 's/ */ /g'
exit 0
else
$use "$1" | col -bx | sed '/Option/,$d'
exit 0
fi
fi
fi