Skip to content

Commit 1285be5

Browse files
committed
cli: cleaner argument handling
1 parent ac5e1db commit 1285be5

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

codapi-cli

+11-13
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ main() {
1313
exit 1
1414
fi
1515

16-
local resource="$1"
17-
local command="$2"
18-
local args=("${@:3}")
16+
local resource="$1"; shift
17+
local command="$1"; shift
1918

2019
case "$resource" in
2120
"sandbox")
22-
do_sandbox "$command" ${args[@]+"${args[@]}"}
21+
do_sandbox "$command" "$@"
2322
;;
2423
*)
2524
echo "Unknown resource: $resource"
@@ -29,18 +28,17 @@ main() {
2928
}
3029

3130
do_sandbox() {
32-
local command="$1"
33-
local args=("${@:2}")
31+
local command="$1"; shift
3432
mkdir -p "$sandboxes_dir"
3533
case "$command" in
3634
"add")
37-
do_sandbox_add ${args[@]+"${args[@]}"}
35+
do_sandbox_add "$@"
3836
;;
3937
"rm")
40-
do_sandbox_rm ${args[@]+"${args[@]}"}
38+
do_sandbox_rm "$@"
4139
;;
4240
"ls")
43-
do_sandbox_ls ${args[@]+"${args[@]}"}
41+
do_sandbox_ls "$@"
4442
;;
4543
*)
4644
echo "Unknown command: $command"
@@ -52,11 +50,11 @@ do_sandbox() {
5250
do_sandbox_add() {
5351
# Command: codapi-cli sandbox add <path>
5452

55-
if [[ "$#" -ne 1 ]]; then
53+
local path="${1:-}"
54+
if [[ -z "$path" ]]; then
5655
echo "Usage: $0 sandbox add <path>"
5756
exit 1
5857
fi
59-
local path="$1"
6058

6159
# 1. Set the name of the sandbox.
6260
local filename
@@ -149,11 +147,11 @@ do_sandbox_add() {
149147
do_sandbox_rm() {
150148
# Command: codapi-cli sandbox rm <name>
151149

152-
if [[ "$#" -ne 1 ]]; then
150+
local name="${1:-}"
151+
if [[ -z "$name" ]]; then
153152
echo "Usage: $0 sandbox rm <name>"
154153
exit 1
155154
fi
156-
local name="$1"
157155

158156
local target_dir="$sandboxes_dir/$name"
159157
if [[ ! -d "$target_dir" ]]; then

0 commit comments

Comments
 (0)