-
-
Notifications
You must be signed in to change notification settings - Fork 49
docs: add bash array pattern for variadic args #480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,7 +13,32 @@ arg "<file>" var=#true # multiple args can be passed (e.g. mycli file1 file2 fil | |||||||||
| arg "<file>..." # shorthand for var=#true (trailing ellipsis) | ||||||||||
| arg "<file>" var=#true var_min=3 # at least 3 args must be passed | ||||||||||
| arg "<file>" var=#true var_max=3 # up to 3 args can be passed | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Using Variadic Args in Bash | ||||||||||
|
|
||||||||||
| When using variadic arguments (`var=#true`), the values are passed as a shell-escaped | ||||||||||
| string via the `usage_<name>` environment variable. To properly handle arguments | ||||||||||
| containing spaces as a bash array, wrap the variable in parentheses: | ||||||||||
|
|
||||||||||
| ```bash | ||||||||||
| # Given: usage_files="arg1 'arg with space' arg3" | ||||||||||
|
|
||||||||||
| # Convert to bash array: | ||||||||||
| eval "files=($usage_files)" | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of
Suggested change
References
|
||||||||||
|
|
||||||||||
| # Now use as array: | ||||||||||
| for f in "${files[@]}"; do | ||||||||||
| echo "Processing: $f" | ||||||||||
| done | ||||||||||
|
|
||||||||||
| # Or pass to commands: | ||||||||||
| touch "${files[@]}" | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| This pattern ensures arguments with spaces are handled correctly as separate elements. | ||||||||||
|
|
||||||||||
| ```kdl | ||||||||||
| arg "<shell>" { | ||||||||||
| choices "bash" "zsh" "fish" # <shell> must be one of the choices | ||||||||||
| } | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
evalwith user-controlled input can introduce command injection vulnerabilities. Ifusage_filescontains malicious shell commands, they will be executed. Consider documenting safer alternatives such as usingreadarraywith process substitution, or add prominent security warnings about input validation ifevalis necessary.