-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathch
91 lines (77 loc) · 2.68 KB
/
ch
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
#!/bin/bash
# catch uninitialized variables
set -u
###############################################
# extract from line starting with search word
# upto newline or line starting with -
# f=2 patch for
# options spread over 2 lines (ex: ch wget -o, ch awk -v)
# sed which has an empty line in between option and description
# builtin command help pages usually do not
# have newline separating option descriptions
###############################################
extract_text ()
{
awk -v cmd="$cmd" -v regex="^\\\s*$1\\\>" '
$0 ~ regex{f=2; print; next}
f==2 && /^\s*--/{f=1; print; next}
f==2 && cmd=="sed" && /^\s*$/{f=1; print; next}
(/^\s*$/ || /^\s*-/) && f{exit}
f' "$file"
}
###############################################
# variable to save command name
# shift, for rest of arguments to be looped
# variable for temporary file
###############################################
cmd="$1"
shift
file='/tmp/command_help.txt'
###############################################
# Save help/man page to file variable
###############################################
cmd_type=$(type -t "$cmd")
if [[ "$cmd_type" == 'builtin' ]]; then
help -m "$cmd" > "$file"
elif [[ "$cmd_type" == 'file' ]]; then
man "$cmd" | col -bx > "$file"
else
echo "Error: $cmd is not a valid command" 1>&2 && exit 1
fi
###############################################
# extract NAME section, equivalent to whatis
# extract relevant text for arguments
###############################################
awk '/^NAME$/{f=1; next} /^\s*$/ && f{exit} f' "$file"
echo
for arg in "$@" ; do
# options starting with --
if [[ $arg =~ ^-- ]] ; then
# for options like --color=auto, remove string after =
# add regex for options like ls -a, --all; grep -e PATTERN, --regexp=PATTERN; etc
arg_mod="(-[a-zA-Z](\\\s*[^,]*)?,\\\s*)?${arg%%=*}"
extract_text "$arg_mod"
# for cases like find -path
# won't work for cases like \a %% for printf
elif grep -q "^\s*$arg\b" "$file" ; then
extract_text "$arg"
# perl based rename has -f, -force; -n, -nono; etc
elif grep -q "^\s*-[a-zA-Z],\s*$arg\b" "$file" ; then
arg_mod="-[a-zA-Z],\\\s*${arg%%=*}"
extract_text "$arg_mod"
# single letter options starting with -
elif [[ $arg =~ ^-[^-] ]] ; then
while read -n1 char; do
extract_text "-$char"
echo
done < <(echo -n "${arg:1}")
# case-insensitive word search
# probably try to match closest to an option?
else
grep -iw -C3 "$arg" "$file"
fi
done
###############################################
# remove temporary file
###############################################
rm "$file"