How to get input name from a tool parsed using `cwl_utils.parser.load_document_by_uri`

Hello everyone, I am trying out the cwl_utils package to parse CWL documents.

Consider the following tool.cwl:

cwlVersion: v1.0
class: CommandLineTool
baseCommand: echo
inputs:
  message:
    type: string
    inputBinding:
      position: 1
outputs: []

I am parsing this using

from cwl_utils.parser import load_document_by_uri
tool = load_document_by_uri('tool.cwl')

I can then get the inputs using tool.inputs. What I am after is the name of the input parameter. In this example there is just one, i.e., message, but I cannot find where to get it from. The tool.inputs property returns a list of CommandInputParameters instances, but as far as I can tell, there is no attribute/property that returns the name:

In [1]: tool = load_document_by_uri('tool.cwl')

In [2]: tool.inputs
Out[2]: [<cwl_utils.parser.cwl_v1_0.CommandInputParameter at 0x7f3e7bb735d0>]

In [3]: tool.inputs[0].id
Out[3]: 'file:///tool.cwl#message'

The id returns a URI that contains it at the end after the #, so I guess I could technically get it through tool.inputs[0].id.split('#')[-1] but this feels really hacky and fragile. Surely there must be a better way of doing this?

1 Like

I also stumbled on the same issue, and found the load_inputfile function from cwl_utils.parser.cwl_v1_2_utils, which returns the file contents as a nested dictionary:

from cwl_utils.parser.cwl_v1_2_utils import load_inputfile
input_file = load_inputfile('tool.cwl')

resulting in input_file as:

{'cwlVersion': 'v1.0',
 'class': 'CommandLineTool',
 'baseCommand': 'echo',
 'inputs': {'message': {'type': 'string', 'inputBinding': {'position': 1}}},
 'outputs': []}

However, there’s also the CommandLineTool.fromDoc classmethod, that should return a CommandLineTool instance, which should be a bit more specific than the nested dictionary. Still grappling a bit with the API to make it work, though. Maybe some of the devs can provide a bit more info or pointers about the cwl-utils package, the RTD page seems quite minimal. Cheers!

That’s what I would do, yep. We could add a utility function to cwl-utils that does this; could you open an issue at https://github.com/common-workflow-language/cwl-utils/issues/new with a description of your use-case? Thanks!