Skip to content

Commit

Permalink
refactor: use boolean option parse value holder in c, more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed Aug 13, 2023
1 parent e1febba commit 971eccf
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions bin/c
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ progVersion() {
################################################################################

quiet=false
eol=-n
declare -a args=()
keep_eol=false
declare -a target_command=()
while [ $# -gt 0 ]; do
case "$1" in
-k | --keep-eol)
eol=
keep_eol=true
shift
;;
-q | --quiet)
Expand All @@ -102,21 +102,21 @@ while [ $# -gt 0 ]; do
;;
--)
shift
args=(${args[@]:+"${args[@]}"} "$@")
target_command=(${target_command[@]:+"${target_command[@]}"} "$@")
break
;;
-*)
usage 2 "${PROG}: unrecognized option '$1'"
;;
*)
# if not option, treat all follow args as command
args=(${args[@]:+"${args[@]}"} "$@")
target_command=(${target_command[@]:+"${target_command[@]}"} "$@")
break
;;
esac
done

readonly eol quiet args
readonly keep_eol quiet target_command

################################################################################
# biz logic
Expand All @@ -139,8 +139,10 @@ copy() {
catThenCopy() {
local content
content="$(cat)"
# shellcheck disable=SC2086
echo $eol "$content" | copy
{
printf %s "$content"
$keep_eol && echo
} | copy
}

teeAndCopy() {
Expand All @@ -151,8 +153,8 @@ teeAndCopy() {
fi
}

if [ ${#args[@]} -eq 0 ]; then
if [ ${#target_command[@]} -eq 0 ]; then
teeAndCopy
else
"${args[@]}" | teeAndCopy
"${target_command[@]}" | teeAndCopy
fi

0 comments on commit 971eccf

Please sign in to comment.