-
Notifications
You must be signed in to change notification settings - Fork 2
/
usages
executable file
·89 lines (76 loc) · 2.46 KB
/
usages
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
#!/bin/bash
# Prints out documentation and usages for utilities.
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
(
echo "usage: ${0##*/} [-t|--table|-v|--verbose|-h|--help]"
echo "Prints out documentation and usage information for everything in current directory."
echo
echo "-t,--table: prints out information in a markdown table."
echo "-v,--verbose: prints out all usage information in markdown format."
echo "-v,--help: prints out this help message."
) 1>&2
exit 1
fi
# Some help messages are adjusted to fit terminal columns, so setting a
# default constant value here avoids usages producing different output
# when run in different terminal windows.
export COLUMNS=120
stty columns 120 2>/dev/null
# -t or --table enabled table mode for generating markdown output
TABLE=0
if echo "$*" | grep -Eq -- "--table|-t"; then
TABLE=1
echo "| Tool | Usage |"
echo "| --- | --- |"
fi
VERBOSE=0
if echo "$*" | grep -Eq -- "--verbose|-v"; then
VERBOSE=1
fi
for I in *; do
if [[ $I == ".gitignore" || $I == "README.md" ]]; then
continue
fi
if grep -Eq "^$I\$" .gitignore; then
continue
fi
# tool name
if [[ $VERBOSE == 1 ]]; then
echo "* * *"
echo -en "### [\`$I\`]($I)"
elif [[ $TABLE == 1 ]]; then
echo -en "| [\`$I\`](bin/$I) | "
else
echo -n "$I - "
fi
# describe tool or symbolic link
DESC=""
if [[ $I == "macosver" ]]; then
if [[ $VERBOSE == 1 ]]; then
DESC=$'\n```\n'"usage: macosver"$'\n\n'"Simply prints out the macOS major and minor version."$'\n```\n'
else
DESC="Prints current macOS version information."
fi
elif file -h "$I" | grep -q 'symbolic link to'; then
if [[ $VERBOSE == 1 ]]; then
echo
fi
# shellcheck disable=SC2012
DESC="Symbolic link to '$(ls -l "$I" | sed 's@.* -> \(.*\)@\1@')'"
elif [[ $VERBOSE == 0 ]]; then
DESC="$(head -n 3 "$I" | tail -n 1 | sed 's@[^ ]* \(.*\)@\1@')"
fi
if [[ $VERBOSE == 1 && $I != "macosver" ]]; then
DESC="$DESC"$'\n```\n'"$("./$I" --help 2>&1)"$'\n```\n'
fi
# truncate long descriptions
if [[ $TABLE == 1 ]]; then
DESC="$(echo "$DESC" | awk -v len=70 '{ if (length($0) > len) print substr($0, 1, len-3) "..."; else print; }')"
fi
echo -n "$DESC"
if [[ $TABLE == 1 ]]; then
echo " |"
else
echo
fi
done