Skip to content

Commit 35bdd17

Browse files
jdxclaude
andauthored
docs: add bash array pattern for variadic args (#480)
## Summary Documents how to properly convert variadic args (`var=#true`) to bash arrays using the pattern: ```bash eval "arr=($usage_var)" ``` This handles arguments with spaces correctly. ## Example ```bash # Given: usage_files="arg1 'arg with space' arg3" # Convert to bash array: eval "files=($usage_files)" # Now use as array: for f in "${files[@]}"; do echo "Processing: $f" done ``` This addresses the common issue where users struggle to handle variadic args with spaces in bash scripts. Related: #189 🤖 Generated with [Claude Code](https://claude.ai/code) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Documentation-only change; no runtime behavior is modified. Low risk aside from potentially encouraging unsafe `eval` usage if misapplied. > > **Overview** > Adds a new **“Using Variadic Args in Bash”** section to `docs/spec/reference/arg.md` describing how `var=#true` values are exposed via `usage_<name>` and providing a Bash snippet (`eval "files=($usage_files)"`) to safely iterate/pass variadic args that may contain spaces. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit d23303c. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Claude Opus 4.5 <[email protected]>
1 parent 16f3858 commit 35bdd17

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/spec/reference/arg.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,32 @@ arg "<file>" var=#true # multiple args can be passed (e.g. mycli file1 file2 fil
1313
arg "<file>..." # shorthand for var=#true (trailing ellipsis)
1414
arg "<file>" var=#true var_min=3 # at least 3 args must be passed
1515
arg "<file>" var=#true var_max=3 # up to 3 args can be passed
16+
```
17+
18+
## Using Variadic Args in Bash
19+
20+
When using variadic arguments (`var=#true`), the values are passed as a shell-escaped
21+
string via the `usage_<name>` environment variable. To properly handle arguments
22+
containing spaces as a bash array, wrap the variable in parentheses:
23+
24+
```bash
25+
# Given: usage_files="arg1 'arg with space' arg3"
26+
27+
# Convert to bash array:
28+
eval "files=($usage_files)"
29+
30+
# Now use as array:
31+
for f in "${files[@]}"; do
32+
echo "Processing: $f"
33+
done
34+
35+
# Or pass to commands:
36+
touch "${files[@]}"
37+
```
38+
39+
This pattern ensures arguments with spaces are handled correctly as separate elements.
1640

41+
```kdl
1742
arg "<shell>" {
1843
choices "bash" "zsh" "fish" # <shell> must be one of the choices
1944
}

0 commit comments

Comments
 (0)