Skip to content

Commit

Permalink
feat(coat/taoc): add --help/--version options 🚩
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Feb 18, 2024
1 parent 488ff1b commit 966525f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 40 deletions.
43 changes: 43 additions & 0 deletions bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,49 @@
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail

# NOTE: DO NOT declare var PROG as readonly in ONE line!
PROG=$(basename -- "$0")
readonly PROG
readonly PROG_VERSION='2.x-dev'

################################################################################
# parse options
################################################################################

progVersion() {
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
printf 'cat executable: %s\n' "$(command -v cat)"
exit
}

usage() {
cat <<EOF
Usage: $PROG [OPTION]... [FILE]...
cat lines colorfully.
Support options:
--help display this help and exit
--version output version information and exit
All other options and arguments are delegated to command cat,
more info see the help/man of command cat(e.g. cat --help).
cat executable: $(command -v cat)
EOF

exit
}

declare -a args=("$@")
# check arguments in reverse, so last option wins.
for ((idx = $# - 1; idx >= 0; --idx)); do
[ "${args[idx]}" = --help ] && usage
[ "${args[idx]}" = --version ] && progVersion
done
unset args idx

################################################################################
# biz logic
################################################################################

# if stdout is not a terminal, use `cat` directly.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
Expand Down
43 changes: 43 additions & 0 deletions bin/taoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,49 @@
# @author Jerry Lee (oldratlee at gmail dot com)
set -eEuo pipefail

# NOTE: DO NOT declare var PROG as readonly in ONE line!
PROG=$(basename -- "$0")
readonly PROG
readonly PROG_VERSION='2.x-dev'

################################################################################
# parse options
################################################################################

progVersion() {
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
printf 'tac executable: %s\n' "$(command -v tac)"
exit
}

usage() {
cat <<EOF
Usage: $PROG [OPTION]... [FILE]...
tac lines colorfully.
Support options:
--help display this help and exit
--version output version information and exit
All other options and arguments are delegated to command tac,
more info see the help/man of command tac(e.g. tac --help).
tac executable: $(command -v tac)
EOF

exit
}

declare -a args=("$@")
# check arguments in reverse, so last option wins.
for ((idx = $# - 1; idx >= 0; --idx)); do
[ "${args[idx]}" = --help ] && usage
[ "${args[idx]}" = --version ] && progVersion
done
unset args idx

################################################################################
# biz logic
################################################################################

# if stdout is not a terminal, use `tac` directly.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
Expand Down
58 changes: 18 additions & 40 deletions docs/shell.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,48 +154,26 @@ line2 of file2
# 帮助信息
# 可以看到本人机器上实现代理的`cat`/`tac`命令是GNU的实现。
$ coat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.

With no FILE, or when FILE is -, read standard input.

-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit

Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/cat>
or available locally via: info '(coreutils) cat invocation'
Usage: coat [OPTION]... [FILE]...
cat lines colorfully.

$ taoc --help
Usage: tac [OPTION]... [FILE]...
Write each FILE to standard output, last line first.

With no FILE, or when FILE is -, read standard input.
Support options:
--help display this help and exit
--version output version information and exit
All other options and arguments are delegated to command cat,
more info see the help/man of command cat(e.g. cat --help).
cat executable: /usr/local/opt/coreutils/libexec/gnubin/cat

Mandatory arguments to long options are mandatory for short options too.
-b, --before attach the separator before instead of after
-r, --regex interpret the separator as a regular expression
-s, --separator=STRING use STRING as the separator instead of newline
--help display this help and exit
--version output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/tac>
or available locally via: info '(coreutils) tac invocation'
$ taoc --help
Usage: taoc [OPTION]... [FILE]...
tac lines colorfully.

Support options:
--help display this help and exit
--version output version information and exit
All other options and arguments are delegated to command tac,
more info see the help/man of command tac(e.g. tac --help).
tac executable: /usr/local/opt/coreutils/libexec/gnubin/tac
```
注:上面示例中,没有彩色;在控制台上运行可以看出彩色效果,如下:
Expand Down

0 comments on commit 966525f

Please sign in to comment.