2.7. 输出#

2.7.1. 返回输出文件#

某个工具的 outputs(输出)项是运行该工具后应返回的输出参数列表。 每个参数都有一个代表其名称的 id字段,以及描述其有效值类型的 type 字段。

工具在 CWL 下运行时,起始工作目录即指定输出目录。 基础工具或脚本要记录运行结果,必须以输出目录下文件的形式实现。 CWL 工具返回的输出参数要么是输出文件本身,要么是分析这些文件的内容而得出的信息。

下例演示怎样返回从 tar 文件中提取的文件。

小技巧

将必要参数传递给 baseCommand

在此前的例子里,baseCommand 命令只是一个字符串,命令的参数都由 CWL 输入传递而来。不过,我们也可以用“字符串数组”作为 baseCommand 的值。数组的第一个元素是将要运行的命令,其后的元素均为命令行的必要参数

tar.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: [tar, --extract]
inputs:
  tarfile:
    type: File
    inputBinding:
      prefix: --file
outputs:
  example_out:
    type: File
    outputBinding:
      glob: hello.txt
tar-job.yml#
tarfile:
  class: File
  path: hello.tar

接下来,新建一个 tar 文件供本示例使用。

$ touch hello.txt && tar -cvf hello.tar hello.txt
hello.txt

现在,在命令行上结合工具描述和输入对象运行 cwltool:

$ cwltool tar.cwl tar-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/envs/latest/bin/cwltool 3.2.20260411152607
INFO Resolved 'tar.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/tar.cwl'
INFO [job tar.cwl] /tmp/ydr8hzm4$ tar \
    --extract \
    --file \
    /tmp/p4tcw9ip/stgab2b811f-7319-4fc8-beca-aba3e38b090c/hello.tar
INFO [job tar.cwl] completed success
{
    "example_out": {
        "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/hello.txt",
        "basename": "hello.txt",
        "class": "File",
        "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
        "size": 0,
        "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/hello.txt"
    }
}INFO Final process status is success

outputBinding 字段描述如何设置每个输出参数的值。

outputs:
  example_out:
    type: File
    outputBinding:
      glob: hello.txt

glob 字段包含可匹配输出目录中文件名的模式。这可以是确切的文件名,不过如果事先不知道文件名,还可以使用通配符,如 glob: '*.txt'.

2.7.2. 捕获标准输出#

要捕获工具的标准输出流,请添加 stdout 字段,配上输出流应定向到的文件名。 然后,给相应的输出参数添加 type: stdout 字段。

stdout.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: echo
stdout: output.txt
inputs:
  message:
    type: string
    inputBinding:
      position: 1
outputs:
  example_out:
    type: stdout
echo-job.yml#
message: Hello world!

现在,调用命令 cwltool 并为它提供工具描述和输入对象:

$ cwltool stdout.cwl echo-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/envs/latest/bin/cwltool 3.2.20260411152607
INFO Resolved 'stdout.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/stdout.cwl'
INFO [job stdout.cwl] /tmp/raf_nr5b$ echo \
    'Hello world!' > /tmp/raf_nr5b/output.txt
INFO [job stdout.cwl] completed success
{
    "example_out": {
        "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/output.txt",
        "basename": "output.txt",
        "class": "File",
        "checksum": "sha1$47a013e660d408619d894b20806b1d5086aab03b",
        "size": 13,
        "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/output.txt"
    }
}INFO Final process status is success

2.7.3. 数组输出#

您也可以用 glob 将多个输出文件捕获至一个文件数组。

array-outputs.cwl#
#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: touch
inputs:
  touchfiles:
    type:
      type: array
      items: string
    inputBinding:
      position: 1
outputs:
  output:
    type:
      type: array
      items: File
    outputBinding:
      glob: "*.txt"
array-outputs-job.yml#
touchfiles:
  - foo.txt
  - bar.dat
  - baz.txt

现在,调用命令 cwltool 并为它提供工具描述和输入对象:

$ cwltool array-outputs.cwl array-outputs-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/envs/latest/bin/cwltool 3.2.20260411152607
INFO Resolved 'array-outputs.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/array-outputs.cwl'
INFO [job array-outputs.cwl] /tmp/18juuk81$ touch \
    foo.txt \
    bar.dat \
    baz.txt
INFO [job array-outputs.cwl] completed success
{
    "output": [
        {
            "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/baz.txt",
            "basename": "baz.txt",
            "class": "File",
            "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "size": 0,
            "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/baz.txt"
        },
        {
            "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/foo.txt",
            "basename": "foo.txt",
            "class": "File",
            "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
            "size": 0,
            "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide-zh-hans/checkouts/latest/src/_includes/cwl/outputs/foo.txt"
        }
    ]
}INFO Final process status is success

《YAML 指南》中所述,array-outputs-job.yml 文件中行首为 - 的成员代表数组元素,用以指明我们期待的各个输出。这种格式还可以用在 CWL 描述中表示数组成员。接下来若干章节中会演示这一用法。