2.6. Parameter References#

In a previous example, we extracted a file using the “tar” program. However, that example was very limited because it assumed that the file we were interested in was called “hello.txt”, and this was written into the .cwl file. This is not the best way to do this, as the “hello.txt” filename may vary or be dependent on the input file(s) used. To avoid this we can specify the name of the file we want in the job parameters file (.yml). In this example, you will see how to reference the value of input parameters dynamically from other fields, which will allow us to then specify the name of the file to extract.

#!/usr/bin/env cwl-runner
cwlVersion: v1.2
class: CommandLineTool
baseCommand: [tar, --extract]
    type: File
      prefix: --file
    type: string
      position: 1
    type: File
      glob: $(inputs.extractfile)
  class: File
  path: hello.tar
extractfile: goodbye.txt

Create your input files and invoke cwltool with the tool description and the input object on the command line:

$ rm hello.tar || true && touch goodbye.txt && tar -cvf hello.tar goodbye.txt
$ cwltool tar-param.cwl tar-param-job.yml
INFO /home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/envs/latest/bin/cwltool 3.1.20240112164112
INFO Resolved 'tar-param.cwl' to 'file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/checkouts/latest/src/_includes/cwl/parameter-references/tar-param.cwl'
INFO [job tar-param.cwl] /tmp/y5r52g5w$ tar \
    --extract \
    --file \
    /tmp/4qjva4wk/stg7c7bdec6-d45b-4724-9236-0493e3c1be1f/hello.tar \
INFO [job tar-param.cwl] completed success
    "extracted_file": {
        "location": "file:///home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/checkouts/latest/src/_includes/cwl/parameter-references/goodbye.txt",
        "basename": "goodbye.txt",
        "class": "File",
        "checksum": "sha1$da39a3ee5e6b4b0d3255bfef95601890afd80709",
        "size": 0,
        "path": "/home/docs/checkouts/readthedocs.org/user_builds/common-workflow-languageuser-guide/checkouts/latest/src/_includes/cwl/parameter-references/goodbye.txt"
}INFO Final process status is success

Certain fields permit parameter references which are enclosed in $(...). These are evaluated and replaced with value being referenced.

    type: File
      glob: $(inputs.extractfile)

References are written using a subset of Javascript syntax. In this example, $(inputs.extractfile), $(inputs["extractfile"]), and $(inputs['extractfile']) are equivalent.

The value of the “inputs” variable is the input object provided when the CWL tool was invoked.

Note that because File parameters are objects, to get the path to an input file you must reference the path field on a file object; to reference the path to the tar file in the above example you would write $(inputs.tarfile.path).

Where are parameter references allowed?

You can only use parameter references in certain fields. These are: