-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclip-ask.sh
161 lines (138 loc) · 4.13 KB
/
clip-ask.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env bash
# shellcheck disable=2016,2155,2181
declare -i SUCCESS=0
declare -i FAIL=1
declare RESET_COLOR="\e[0m"
declare ERROR_COLOR="\e[31m"
declare SUCCESS_COLOR="\e[32m"
help() {
echo "Placeholder explanator for Command Line Interface Pages.
Usage:
$0 (--help|-h)
$0 (--version|-v)
$0 (--author|-a)
$0 (--email|-e)
$0 <placeholder>
Notes:
Omit placeholder curly braces when passing placeholders.
Escaping and placeholders with alternatives are not recognized and treated literally."
}
version() {
echo "1.0" >&2
}
author() {
echo "Emily Grace Seville" >&2
}
email() {
echo "[email protected]" >&2
}
explain() {
declare placeholder="$1"
sed -nE '/^[^{}].+[^{}]$/! Q1' <<<"$placeholder" || {
echo -e "$0: $placeholder: ${ERROR_COLOR}no curly braces expected$RESET_COLOR" >&2
return "$FAIL"
}
if [[ "$placeholder" =~ ^(/?)([^ {}:*+?]+)([*+?]?)\ +([^{}:]+) ]]; then
declare placeholder_leading_slash="${BASH_REMATCH[1]}"
declare placeholder_type="${BASH_REMATCH[2]}"
declare placeholder_quantifier="${BASH_REMATCH[3]}"
declare placeholder_description="${BASH_REMATCH[4]}"
if [[ -n "$placeholder_leading_slash" ]] && [[ ! "$placeholder_type" =~ ^(file|directory|path)$ ]]; then
echo -e "$0: $placeholder: ${ERROR_COLOR}no leading slash expected$RESET_COLOR" >&2
return "$FAIL"
fi
declare placeholder_readable_type
case "$placeholder_type" in
bool)
placeholder_readable_type="Boolean"
;;
int)
placeholder_readable_type="Integer"
;;
float)
placeholder_readable_type="Float"
;;
char)
placeholder_readable_type="Character"
;;
string)
placeholder_readable_type="Text"
;;
command)
placeholder_readable_type="Command or subcommand"
;;
file)
placeholder_readable_type="Regular file, pipe or device"
;;
directory)
placeholder_readable_type="Directory"
;;
path)
placeholder_readable_type="Regular file, pipe, device or directory"
;;
any)
placeholder_readable_type="Anything"
;;
esac
echo "- $placeholder_readable_type expected to be used instead of this placeholder."
[[ "$placeholder_type" =~ ^(file|directory|path)$ ]] && {
if [[ -n "$placeholder_leading_slash" ]]; then
echo "- Absolute path expected."
else
echo "- Relative path expected."
fi
}
declare placeholder_readable_quantifier
case "$placeholder_quantifier" in
"?")
placeholder_readable_quantifier="Zero or one"
;;
"*")
placeholder_readable_quantifier="Zero or more"
;;
"+")
placeholder_readable_quantifier="One or more"
;;
esac
[[ -n "$placeholder_quantifier" ]] && echo "- $placeholder_readable_quantifier values can be substituted instead of this placeholder."
echo "- Placeholder can be described as $placeholder_description."
else
echo -e "'$placeholder': ${ERROR_COLOR}valid placeholder expected$RESET_COLOR" >&2
return "$FAIL"
fi
}
if (($# == 0)); then
help
fi
while [[ -n "$1" ]]; do
declare option="$1"
case "$option" in
--help | -h)
help
exit
;;
--version | -v)
version
exit
;;
--author | -a)
author
exit
;;
--email | -e)
email
exit
;;
*)
declare placeholder="$option"
declare explanation
explanation="$(explain "$placeholder")"
(($? == 0)) && {
echo -e "$placeholder: ${SUCCESS_COLOR}explained:$RESET_COLOR" >&2
echo "$explanation"
}
shift
;;
esac
done
exit "$SUCCESS"