-
-
Notifications
You must be signed in to change notification settings - Fork 855
docs(tasks): add bash array pattern for variadic args #7914
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 | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -117,6 +117,24 @@ arg "<files>" var=#true var_max=5 // Maximum 5 files allowed | |||||||
| arg "<files>" var=#true var_min=1 var_max=3 // Between 1 and 3 files | ||||||||
| ``` | ||||||||
|
|
||||||||
| ::: tip Handling Variadic Args with Spaces in Bash | ||||||||
| Variadic arguments are passed as a shell-escaped string. To properly handle arguments containing spaces as a bash array, wrap the variable in parentheses: | ||||||||
|
|
||||||||
| ```bash | ||||||||
| # Convert to bash array: | ||||||||
| eval "files=($usage_files)" | ||||||||
|
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 as array: | ||||||||
| for f in "${files[@]}"; do | ||||||||
| echo "Processing: $f" | ||||||||
| done | ||||||||
|
|
||||||||
| # Or pass to commands: | ||||||||
| touch "${files[@]}" | ||||||||
| ``` | ||||||||
|
|
||||||||
| ::: | ||||||||
|
|
||||||||
| #### Environment Variable Backing | ||||||||
|
|
||||||||
| ```kdl | ||||||||
|
|
@@ -680,6 +698,22 @@ run = 'eslint ${usage_files?}' | |||||||
|
|
||||||||
| </div> | ||||||||
|
|
||||||||
| ::: tip Handling Arguments with Spaces | ||||||||
| If your variadic arguments may contain spaces, convert the variable to a bash array: | ||||||||
|
|
||||||||
| ```mise-toml | ||||||||
| [tasks.process] | ||||||||
| usage = 'arg "<files>" var=#true' | ||||||||
| run = ''' | ||||||||
| eval "files=($usage_files)" | ||||||||
|
||||||||
| eval "files=($usage_files)" | |
| # Safely read the usage_files into a Bash array without using eval | |
| mapfile -t files <<< "$usage_files" |
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.
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-provided input is a security risk. If$usage_filescontains malicious code, it will be executed. Consider documenting safer alternatives likemapfileorreadarray, or at minimum add a security warning about validating input before usingeval.