【问题标题】:Bash - Pass multiple array arguments from scriptBash - 从脚本传递多个数组参数
【发布时间】:2021-11-04 04:45:16
【问题描述】:

我正在使用 XcodeCoverageConverter 按照 README 文件中提供的说明将 XCResult 转换为 Cobertura XML。 当只有一项作为--exclude-packages 的值传递时,它按预期工作。 但是,当我尝试按如下方式传递数组时,

xcc generate coverage.json TargetDirectory cobertura-xml --exclude-packages Tests AnotherPackageName --verbose

我收到以下错误,

Error: The value 'AnotherPackageName' is invalid for '<output-formats>'
Usage: xcc generate <json-file> <output-path> [<output-formats> ...] [--exclude-targets <exclude-targets> ...] [--exclude-packages <exclude-packages> ...] [--verbose]

我正在从我的 Bash 脚本中尝试同样的事情,如果我为 --exclude-packages 传递数组,我会得到同样的错误。

示例 1

jsonPath="coverage.json"
coberturaTargetPath="Cobertura"

xcc generate $jsonPath \
   $coberturaTargetPath cobertura-xml \
   --exclude-packages Tests AnotherPackageName \
   --verbose

示例 2

jsonPath="coverage.json"
coberturaTargetPath="Cobertura"
excludePackages=(Tests AnotherPackageName)

xcc generate $jsonPath \
   $coberturaTargetPath cobertura-xml \
   --exclude-packages ${excludePackages[@]} \
   --verbose

示例 3

jsonPath="coverage.json"
coberturaTargetPath="Cobertura"
excludePackages=(Tests AnotherPackageName)

xcc generate $jsonPath \
   $coberturaTargetPath cobertura-xml \
   --exclude-packages=${excludePackages[@]} \
   --verbose

示例 4

jsonPath="coverage.json"
coberturaTargetPath="Cobertura"
outputFormats=(cobertura-xml)
excludePackages=(Tests AnotherPackageName)

xcc generate $jsonPath \
   $coberturaTargetPath ${outputFormats[@]} \
   --exclude-packages=${excludePackages[@]} \
   --verbose

示例 5

jsonPath="coverage.json"
coberturaTargetPath="Cobertura"
outputFormats=(cobertura-xml)
excludePackages=(Tests AnotherPackageName)

xcc generate $jsonPath \
   $coberturaTargetPath ${outputFormats[@]} \
   --exclude-packages="${excludePackages[@]} " \
   --verbose

在上述所有尝试中,我都遇到了同样的错误。 我为 --exclude-packages 传递的项目(不包括第一项)被考虑用于输出格式。 如何解决这个问题?

更新:

xcc 实用程序确实支持--exclude-packages 的多个参数。 Reference

【问题讨论】:

  • 根据您的建议,我尝试了--exclude-packages="${excludePackages[@]} ",但我得到了同样的错误。当我尝试使用 --exclude-packages "Tests AnotherPackageName" 时,它会将其视为具有 1 个元素“Tests AnotherPackageName”的数组
  • 不使用数组如何运行它?你会--exclude-packages something --exclude-packages somethingelse吗?换句话说,当用户想要将多个参数传递给exclude-packages 并且xcc 实用程序是否支持这样的用例时,xcc 实用程序期望拥有什么?
  • 是的xcc 实用程序支持多个参数,这里是参考link

标签: arrays bash arguments code-coverage xcodebuild


【解决方案1】:

使用 https://shellcheck.net 检查您的脚本。

假设xcc 实用程序支持多个--exclude-packages 参数,则:对于每个数组元素,在每个数组元素之前添加--exclude-packages,然后将其传递给命令。

excludePackages=(Tests AnotherPackageName)

cmd=(
    xcc generate
    "$jsonPath"
    "$coberturaTargetPath"
    "${outputFormats[@]}"
)
for ii in "${excludePackages[@]}"; do
   cmd+=(--exclude-packages "$ii")
done
cmd+=(--verbose)

echo "Running: ${cmd[*]}"
"${cmd[@]}"

【讨论】:

    猜你喜欢
    • 2017-09-26
    • 1970-01-01
    • 2018-03-03
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2013-06-18
    • 1970-01-01
    相关资源
    最近更新 更多