Skip to content

Commit 1d0cd0b

Browse files
committed
feat(coat/taoc): add --help/--version options 🚩
1 parent 3d57c0d commit 1d0cd0b

File tree

3 files changed

+104
-40
lines changed

3 files changed

+104
-40
lines changed

bin/coat

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,49 @@
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212
set -eEuo pipefail
1313

14+
# NOTE: DO NOT declare var PROG as readonly in ONE line!
15+
PROG=$(basename -- "$0")
16+
readonly PROG
17+
readonly PROG_VERSION='2.x-dev'
18+
19+
################################################################################
20+
# parse options
21+
################################################################################
22+
23+
progVersion() {
24+
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
25+
printf 'cat executable: %s\n' "$(command -v cat)"
26+
exit
27+
}
28+
29+
usage() {
30+
cat <<EOF
31+
Usage: $PROG [OPTION]... [FILE]...
32+
cat lines colorfully.
33+
34+
Support options:
35+
--help display this help and exit
36+
--version output version information and exit
37+
All other options and arguments are delegated to command cat,
38+
more info see the help/man of command cat(e.g. cat --help).
39+
cat executable: $(command -v cat)
40+
EOF
41+
42+
exit
43+
}
44+
45+
declare -a args=("$@")
46+
# check arguments in reverse, so last option wins.
47+
for ((idx = $# - 1; idx >= 0; --idx)); do
48+
[ "${args[idx]}" = --help ] && usage
49+
[ "${args[idx]}" = --version ] && progVersion
50+
done
51+
unset args idx
52+
53+
################################################################################
54+
# biz logic
55+
################################################################################
56+
1457
# if stdout is not a terminal, use `cat` directly.
1558
# '-t' check: is a terminal?
1659
# check isatty in bash https://stackoverflow.com/questions/10022323

bin/taoc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,49 @@
1111
# @author Jerry Lee (oldratlee at gmail dot com)
1212
set -eEuo pipefail
1313

14+
# NOTE: DO NOT declare var PROG as readonly in ONE line!
15+
PROG=$(basename -- "$0")
16+
readonly PROG
17+
readonly PROG_VERSION='2.x-dev'
18+
19+
################################################################################
20+
# parse options
21+
################################################################################
22+
23+
progVersion() {
24+
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
25+
printf 'tac executable: %s\n' "$(command -v tac)"
26+
exit
27+
}
28+
29+
usage() {
30+
cat <<EOF
31+
Usage: $PROG [OPTION]... [FILE]...
32+
tac lines colorfully.
33+
34+
Support options:
35+
--help display this help and exit
36+
--version output version information and exit
37+
All other options and arguments are delegated to command tac,
38+
more info see the help/man of command tac(e.g. tac --help).
39+
tac executable: $(command -v tac)
40+
EOF
41+
42+
exit
43+
}
44+
45+
declare -a args=("$@")
46+
# check arguments in reverse, so last option wins.
47+
for ((idx = $# - 1; idx >= 0; --idx)); do
48+
[ "${args[idx]}" = --help ] && usage
49+
[ "${args[idx]}" = --version ] && progVersion
50+
done
51+
unset args idx
52+
53+
################################################################################
54+
# biz logic
55+
################################################################################
56+
1457
# if stdout is not a terminal, use `tac` directly.
1558
# '-t' check: is a terminal?
1659
# check isatty in bash https://stackoverflow.com/questions/10022323

docs/shell.md

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -154,48 +154,26 @@ line2 of file2
154154
# 帮助信息
155155
# 可以看到本人机器上实现代理的`cat`/`tac`命令是GNU的实现。
156156
$ coat --help
157-
Usage: cat [OPTION]... [FILE]...
158-
Concatenate FILE(s) to standard output.
159-
160-
With no FILE, or when FILE is -, read standard input.
161-
162-
-A, --show-all equivalent to -vET
163-
-b, --number-nonblank number nonempty output lines, overrides -n
164-
-e equivalent to -vE
165-
-E, --show-ends display $ at end of each line
166-
-n, --number number all output lines
167-
-s, --squeeze-blank suppress repeated empty output lines
168-
-t equivalent to -vT
169-
-T, --show-tabs display TAB characters as ^I
170-
-u (ignored)
171-
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
172-
--help display this help and exit
173-
--version output version information and exit
174-
175-
Examples:
176-
cat f - g Output f's contents, then standard input, then g's contents.
177-
cat Copy standard input to standard output.
178-
179-
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
180-
Full documentation at: <https://www.gnu.org/software/coreutils/cat>
181-
or available locally via: info '(coreutils) cat invocation'
157+
Usage: coat [OPTION]... [FILE]...
158+
cat lines colorfully.
182159

183-
$ taoc --help
184-
Usage: tac [OPTION]... [FILE]...
185-
Write each FILE to standard output, last line first.
186-
187-
With no FILE, or when FILE is -, read standard input.
160+
Support options:
161+
--help display this help and exit
162+
--version output version information and exit
163+
All other options and arguments are delegated to command cat,
164+
more info see the help/man of command cat(e.g. cat --help).
165+
cat executable: /usr/local/opt/coreutils/libexec/gnubin/cat
188166

189-
Mandatory arguments to long options are mandatory for short options too.
190-
-b, --before attach the separator before instead of after
191-
-r, --regex interpret the separator as a regular expression
192-
-s, --separator=STRING use STRING as the separator instead of newline
193-
--help display this help and exit
194-
--version output version information and exit
195-
196-
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
197-
Full documentation <https://www.gnu.org/software/coreutils/tac>
198-
or available locally via: info '(coreutils) tac invocation'
167+
$ taoc --help
168+
Usage: taoc [OPTION]... [FILE]...
169+
tac lines colorfully.
170+
171+
Support options:
172+
--help display this help and exit
173+
--version output version information and exit
174+
All other options and arguments are delegated to command tac,
175+
more info see the help/man of command tac(e.g. tac --help).
176+
tac executable: /usr/local/opt/coreutils/libexec/gnubin/tac
199177
```
200178
201179
注:上面示例中,没有彩色;在控制台上运行可以看出彩色效果,如下:

0 commit comments

Comments
 (0)