Skip to content

Commit b652d6e

Browse files
committed
fix(common-utils): 🐛 update argument prefix for params in dispatch utility
1 parent 7820242 commit b652d6e

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/common-utils/_zz_args.sh

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ if test $# -lt 1; then
2121
<argname> <datatype> <varname> <help>
2222
...
2323
help" >&2
24+
25+
echo "With <argname>:
26+
x for flags x with value (e.g., -f value, -h being reserved for help)
27+
- for sequential arguments in the order defined, without flags
28+
+ to capture all remaining arguments as a single variable with spaces as separators
29+
& to capture all remaining arguments as a single variable with newlines as separators" >&2
2430
return 1
2531
fi
2632

@@ -48,7 +54,7 @@ while read argname datatype varname help; do
4854
line="<$datatype...>"
4955
helpinfo="$helpinfo\n\t$(printf '%-12s : %s' "$name" "$help")"
5056
elif [ "$argname" = "&" ]; then
51-
line="<$datatype...>"
57+
line="<$datatype...>>"
5258
helpinfo="$helpinfo\n\t$(printf '%-12s : %s' "$name" "$help")"
5359
else
5460
if [ "$datatype" = "-" ]; then
@@ -113,15 +119,23 @@ else
113119

114120
# Process remaining '&' parameters
115121
for arg in $(echo $varnames | grep -E "^&" | cut -f2); do
116-
if [ "$#" -gt "0" ]; then
117-
echo "$arg='$(echo $@ | sed "s/ /\n/g")'" && shift $#
118-
fi
122+
lines=""
123+
while [ "$#" -gt "0" ]; do
124+
# spaces that are not escaped should be preserved as part of the argument
125+
if [ -z "$lines" ]; then
126+
lines="$1"
127+
else
128+
lines="$lines\\\\n$1"
129+
fi
130+
shift 1
131+
done
132+
echo "$arg='$lines'"
119133
done
120134

121135
# Process remaining '+' parameters
122136
for arg in $(echo $varnames | grep -E "^\+" | cut -f2); do
123137
if [ "$#" -gt "0" ]; then
124-
echo "$arg='$(echo $@ | sed "s/ /\\\\ /g")'" && shift $#
138+
echo "$arg='$@'" && shift $#
125139
fi
126140
done
127141

src/common-utils/_zz_dispatch.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ set -e
88

99
# Parse arguments and display help if needed
1010
eval $(
11-
zz_args "Dispatch Utility" $0 "$@" <<- help
11+
./src/common-utils/_zz_args.sh "Dispatch Utility" $0 "$@" <<- help
1212
- caller caller Caller script path
1313
- subcmd subcmd Target script to execute
14-
+ params params Remaining arguments passed to the target script
14+
d debug debug Enable debug mode
15+
& params params Remaining arguments passed to the target script
1516
help
1617
)
1718

@@ -24,6 +25,12 @@ usage() {
2425
fi
2526
}
2627

28+
preserve() {
29+
echo $@ | while read -r line; do
30+
echo \"$line\"
31+
done
32+
}
33+
2734
# Determine caller directory and base name
2835
caller_basename=$(basename "${caller}")
2936
caller_dir=$(dirname "${caller}")
@@ -38,8 +45,6 @@ if [ -z "${subcmd}" ]; then
3845
exit 1
3946
fi
4047

41-
42-
4348
# If caller_dir is just the basename (no slash) and file exists in PATH, try to locate
4449
if [ "${caller_dir}" = "${caller_basename}" ] || [ -z "${caller_dir}" ]; then
4550
caller_dir="."
@@ -52,12 +57,14 @@ else
5257
target="${caller_dir}/${name}"
5358
fi
5459

60+
echo $(preserve $params)
61+
5562
if [ -x "${target}" ]; then
5663
zz_log i "Dispatching to executable target: ${target}" >&2
57-
exec "${target}" $params
64+
exec "${target}" $(preserve $params)
5865
elif [ -f "${target}" ]; then
5966
zz_log i "Dispatching to subshell target: ${target}" >&2
60-
exec sh "${target}" $params
67+
exec sh "${target}" $(preserve $params)
6168
else
6269
# Nothing found: show help and available utilities in the same directory
6370
zz_log w "No dispatch target found" && usage

0 commit comments

Comments
 (0)