Repository: common-workflow-language/cwltool

Issue:
Creating input yaml - issue with ruamel comments.py
Hi,
I am using cwltool version 3.0.20201203173111 that came with `pip install cwl-runner`.  This might be issue with ruamel or with my workflow but here is run command and trace:

`cwltool --make-template --debug tumor_only_exome.cwl > hiv_input.yml`

traceback:
```
Traceback (most recent call last):
  File "/home/coyote/anaconda3/envs/cwl/lib/python3.6/site-packages/cwltool/main.py", line 991, in main
    make_template(tool)
  File "/home/coyote/anaconda3/envs/cwl/lib/python3.6/site-packages/cwltool/main.py", line 744, in make_template
    generate_input_template(tool),
  File "/home/coyote/anaconda3/envs/cwl/lib/python3.6/site-packages/cwltool/main.py", line 301, in generate_input_template
    template.insert(0, name, value, comment)
  File "/home/coyote/anaconda3/envs/cwl/lib/python3.6/site-packages/ruamel/yaml/comments.py", line 722, in insert
    self.yaml_add_eol_comment(comment, key=key)
  File "/home/coyote/anaconda3/envs/cwl/lib/python3.6/site-packages/ruamel/yaml/comments.py", line 295, in yaml_add_eol_comment
    if comment[0] != '#':
IndexError: string index out of range
```


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language) Report
> Merging [#1760](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language) (1dbf916) into [main](https://codecov.io/gh/common-workflow-language/cwltool/commit/14f402f643f86f72fa13a1d2e5fde3dcc7a90914?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language) (14f402f) will **decrease** coverage by `31.61%`.
> The diff coverage is `0.00%`.

```diff
@@             Coverage Diff             @@
##             main    #1760       +/-   ##
===========================================
- Coverage   80.89%   49.28%   -31.62%     
===========================================
  Files          47       47               
  Lines        8177     8175        -2     
  Branches     2230     2075      -155     
===========================================
- Hits         6615     4029     -2586     
- Misses       1047     3555     +2508     
- Partials      515      591       +76     
```


| [Impacted Files](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language) | Coverage Δ | |
|---|---|---|
| [cwltool/main.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9tYWluLnB5) | `30.07% <0.00%> (-43.99%)` | :arrow_down: |
| [cwltool/\_\_main\_\_.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9fX21haW5fXy5weQ==) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [cwltool/factory.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9mYWN0b3J5LnB5) | `0.00% <0.00%> (-90.91%)` | :arrow_down: |
| [cwltool/pack.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9wYWNrLnB5) | `8.50% <0.00%> (-79.50%)` | :arrow_down: |
| [cwltool/cwlviewer.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9jd2x2aWV3ZXIucHk=) | `22.82% <0.00%> (-76.09%)` | :arrow_down: |
| [cwltool/subgraph.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9zdWJncmFwaC5weQ==) | `11.60% <0.00%> (-68.51%)` | :arrow_down: |
| [cwltool/provenance.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9wcm92ZW5hbmNlLnB5) | `14.07% <0.00%> (-67.00%)` | :arrow_down: |
| [cwltool/provenance\_profile.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9wcm92ZW5hbmNlX3Byb2ZpbGUucHk=) | `11.01% <0.00%> (-62.79%)` | :arrow_down: |
| [cwltool/cuda.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9jdWRhLnB5) | `21.87% <0.00%> (-53.13%)` | :arrow_down: |
| [cwltool/software\_requirements.py](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language#diff-Y3dsdG9vbC9zb2Z0d2FyZV9yZXF1aXJlbWVudHMucHk=) | `25.64% <0.00%> (-47.44%)` | :arrow_down: |
| ... and [29 more](https://codecov.io/gh/common-workflow-language/cwltool/pull/1760/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language) | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=common-workflow-language)

tom-tan: Thank you for the suggestion.
I run it but it does not change `main.py`.

```console
$ make cleanup
...
reformatted mypy-stubs/prov/model.pyi

All done! ✨ 🍰 ✨
1 file reformatted, 413 files left unchanged.
...
diff-quality --compare-branch=main --violations=pydocstyle --fail-under=100 pydocstyle_report.txt
-------------
Diff Quality
Quality Report: pydocstyle
Diff: main...HEAD, staged and unstaged changes
-------------
No lines with quality information in this diff.
-------------
```

Patch:
diff --git a/cwltool/main.py b/cwltool/main.py
index e024d22a..23c12d59 100755
--- a/cwltool/main.py
+++ b/cwltool/main.py
@@ -195,13 +195,15 @@ def generate_example_input(
                 else:
                     comment = "optional"
         else:
-            example = CommentedSeq()
-            for index, entry in enumerate(inptype):
+            example, comment = generate_example_input(inptype[0], default)
+            type_names = []
+            for entry in inptype:
                 value, e_comment = generate_example_input(entry, default)
-                example.append(value)
-                example.yaml_add_eol_comment(e_comment, index)
+                if e_comment:
+                    type_names.append(e_comment)
+            comment = "one of " + ", ".join(type_names)
             if optional:
-                comment = "optional"
+                comment = f"{comment} (optional)"
     elif isinstance(inptype, Mapping) and "type" in inptype:
         if inptype["type"] == "array":
             first_item = cast(MutableSequence[CWLObjectType], inptype["items"])[0]


Test patch:
diff --git a/tests/test_make_template.py b/tests/test_make_template.py
index 519f8603..f2bd2867 100644
--- a/tests/test_make_template.py
+++ b/tests/test_make_template.py
@@ -8,3 +8,21 @@ from cwltool import main
 def test_anonymous_record() -> None:
     inputs = cmap([{"type": "record", "fields": []}])
     assert main.generate_example_input(inputs, None) == ({}, "Anonymous record type.")
+
+
+def test_union() -> None:
+    """Test for --make-template for a union type."""
+    inputs = cmap(["string", "string[]"])
+    assert main.generate_example_input(inputs, None) == (
+        "a_string",
+        'one of type "string", type "string[]"',
+    )
+
+
+def test_optional_union() -> None:
+    """Test for --make-template for an optional union type."""
+    inputs = cmap(["null", "string", "string[]"])
+    assert main.generate_example_input(inputs, None) == (
+        "a_string",
+        'one of type "string", type "string[]" (optional)',
+    )