Skip to content

Commit 2a5d446

Browse files
committed
Update wdlgen task to generate without command options
1 parent 73572ad commit 2a5d446

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

tests/test_task_generation.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,28 @@ def test_commandarg_nospace(self):
208208
)
209209
self.assertEqual("arg=argVal", t.get_string())
210210

211+
def test_commandarg_flag(self):
212+
t = Task.Command.CommandInput.from_fields(
213+
name="my_value",
214+
true="--arg"
215+
)
216+
self.assertEqual("~{if (my_value) then \"--arg\" else \"\"}", t.get_string())
217+
218+
def test_commandarg_flag_false(self):
219+
t = Task.Command.CommandInput.from_fields(
220+
name="my_value",
221+
false="--arg"
222+
)
223+
self.assertEqual("~{if (my_value) then \"\" else \"--arg\"}", t.get_string())
224+
225+
def test_commandinp_array_inp(self):
226+
t = Task.Command.CommandInput.from_fields(
227+
name="my_array",
228+
separator=" ",
229+
default=[]
230+
)
231+
self.assertEqual("~{sep(" ", if defined(my_array) then my_array else [])}", t.get_string())
232+
211233

212234
class TestWorkflowGeneration(unittest.TestCase):
213235
def test_hello_workflow(self):

wdlgen/task.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,43 +123,43 @@ def from_fields(name: str,
123123
# Ugly optional workaround: https://github.com/openwdl/wdl/issues/25#issuecomment-315424063
124124
# Additional workaround for 'length(select_first({name}, [])' as length requires a non-optional array
125125
internal_pref = f'if defined({name}) && length(select_first([{name}, []])) > 0 then "{bc}" else ""'
126-
return Task.Command.CommandInput(f'~{{{internal_pref}}}~{{sep=" {bc}" {name}}}', position=position)
127-
return Task.Command.CommandInput(f'~{{sep=" " prefix("{bc}", {name})}}', position=position)
126+
return Task.Command.CommandInput(f'~{{{internal_pref}}}~{{sep(" {bc}", {name})}}', position=position)
127+
return Task.Command.CommandInput(f'~{{sep(" ", prefix("{bc}", {name}))}}', position=position)
128128

129-
if array_sep and optional:
129+
elif array_sep and optional:
130130
# optional array with separator
131131
# ifdefname = f'(if defined({name}) then {name} else [])'
132-
return Task.Command.CommandInput(f'~{{true="{bc}" false="" defined({name})}}~{{sep="{array_sep}" {name}}}', position=position)
132+
return Task.Command.CommandInput(f'~{{true="{bc}" false="" defined({name})}}~{{sep("{array_sep}", {name})}}', position=position)
133133

134-
options = []
135-
if default:
134+
# build up new value from previous options
135+
value = name
136+
137+
if default is not None:
136138
val = default
137139
if isinstance(default, str):
138140
val = f'"{default}"'
139141
if isinstance(default, bool):
140-
141142
val = "true" if default else "false"
142-
options.append(f"default={val}")
143+
144+
value = f"if defined({value}) then {value} else {val}"
145+
143146
if array_sep:
144-
options.append(f'sep="{array_sep}"')
147+
value = f'sep("{array_sep}", {value})'
145148
is_flag = true or false
146149
if is_flag:
147-
options.append(f'true="{true if true else ""}"')
148-
options.append(f'false="{false if false else ""}"')
149-
150-
stroptions = "".join(o + " " for o in options)
150+
value = f'if ({value}) then "{true if true else ""}" else "{false if false else ""}"'
151151

152152
prewithquotes = f'"{bc}" + ' if bc.strip() else ""
153153
if optional and not default and not is_flag and prewithquotes:
154154
# Option 1: We apply quotes are value, Option 2: We quote whole "prefix + name" combo
155155
full_token = (
156-
f"{prewithquotes} '\"' + {name} + '\"'"
156+
f"{prewithquotes} '\"' + {value} + '\"'"
157157
if (separate_value_from_prefix and prefix and prewithquotes)
158-
else f"'\"' + {prewithquotes}{name} + '\"'"
158+
else f"'\"' + {prewithquotes}{value} + '\"'"
159159
)
160-
return Task.Command.CommandInput(f'~{{{stroptions}if defined({name}) then ({full_token}) else ""}}', position=position)
160+
return Task.Command.CommandInput(f'~{{if defined({value}) then ({full_token}) else ""}}', position=position)
161161
else:
162-
return Task.Command.CommandInput(bc + f"~{{{stroptions}{name}}}", position=position)
162+
return Task.Command.CommandInput(bc + f"~{{{value}}}", position=position)
163163

164164
def __init__(
165165
self,

0 commit comments

Comments
 (0)