Skip to content

Commit

Permalink
runner CLI usability tweaks
Browse files Browse the repository at this point in the history
allow space between "input=" and filename, to allow shell filename completion

let --empty supply empty string as well as empty array

add --none to null out optional inputs with defaults
  • Loading branch information
mlin committed Sep 27, 2020
1 parent baf9312 commit 76ed3cc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
55 changes: 48 additions & 7 deletions WDL/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,13 @@ def fill_run_subparser(subparsers):
"--empty",
metavar="input_key",
action="append",
help="explicitly set an array input to the empty array (to override a default)",
help="explicitly set a string input to the empty string OR an array input to the empty array",
)
group.add_argument(
"--none",
metavar="input_key",
action="append",
help="explicitly set an optional input to None (to override a default)",
)
group.add_argument(
"--task",
Expand Down Expand Up @@ -495,6 +501,7 @@ def runner(
inputs=[],
input_file=None,
empty=[],
none=[],
json_only=False,
run_dir=None,
path=None,
Expand Down Expand Up @@ -622,6 +629,7 @@ def file_found(fn):
inputs,
input_file,
empty,
none,
task=task,
file_found=file_found,
root=eff_root, # if copy_input_files is set, then input files need not reside under the configured root
Expand Down Expand Up @@ -771,7 +779,7 @@ def runner_input_completer(prefix, parsed_args, **kwargs):


def runner_input(
doc, inputs, input_file, empty, task=None, check_required=True, file_found=None, root="/"
doc, inputs, input_file, empty, none, task=None, check_required=True, file_found=None, root="/"
):
"""
- Determine the target workflow/task
Expand Down Expand Up @@ -804,17 +812,50 @@ def runner_input(
available_inputs, (target.name if isinstance(target, Workflow) else ""), input_file, root
)

# set explicitly empty arrays
# set explicitly empty arrays or strings
for empty_name in empty or []:
try:
decl = available_inputs[empty_name]
except KeyError:
runner_input_help(target)
raise Error.InputError(f"No such input to {target.name}: {empty_name}")
if not isinstance(decl.type, Type.Array) or decl.type.nonempty:
if isinstance(decl.type, Type.Array):
if decl.type.nonempty:
raise Error.InputError(
f"Cannot set input {str(decl.type)} {decl.name} to empty array"
)
input_env = input_env.bind(empty_name, Value.Array(decl.type.item_type, []), decl)
elif isinstance(decl.type, Type.String):
input_env = input_env.bind(empty_name, Value.String(""), decl)
else:
msg = f"Cannot set {str(decl.type)} {decl.name} to empty array or string"
if decl.type.optional:
msg += "; perhaps you want --none " + decl.name
raise Error.InputError(msg)

# set explicitly None values
for none_name in none or []:
try:
decl = available_inputs[none_name]
except KeyError:
runner_input_help(target)
raise Error.InputError(f"Cannot set input {str(decl.type)} {decl.name} to empty array")
input_env = input_env.bind(empty_name, Value.Array(decl.type.item_type, []), decl)
raise Error.InputError(f"No such input to {target.name}: {none_name}")
if not decl.type.optional:
raise Error.InputError(
f"Cannot set non-optional input {str(decl.type)} {decl.name} to None"
)
input_env = input_env.bind(none_name, Value.Null(), decl)

# preprocess command-line inputs: merge adjacent elements ("x=", "y") into ("x=y"), allowing
# shell filename completion on y
inputs = list(inputs)
i = 0
while i < len(inputs):
len_i = len(inputs[i])
if len_i > 1 and inputs[i].find("=") == len_i - 1 and i + 1 < len(inputs):
inputs[i] = inputs[i] + inputs[i + 1]
del inputs[i + 1]
i += 1

# add in command-line inputs
for one_input in inputs:
Expand Down Expand Up @@ -1259,7 +1300,7 @@ def file_found(fn):

try:
target, input_env, input_json = runner_input(
doc, [], infile, [], task=task, check_required=False, file_found=file_found
doc, [], infile, [], [], task=task, check_required=False, file_found=file_found
)
except Error.InputError as exn:
die(exn.args[0])
Expand Down
6 changes: 3 additions & 3 deletions tests/runner.t
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ task echo {
Array[String]+ a_s
Array[File] a_f
File? o_f
Array[String]? o_a_s
Array[String]? o_a_s = ["zzz"]
}
command {
Expand All @@ -52,14 +52,14 @@ task echo {
output {
Int out_i = i
Array[String]+ out_s = flatten([[s],a_s])
Array[String]+ out_s = flatten([[s],a_s,select_all([o_a_s])])
Array[File]+ out_f = flatten([[f],a_f,select_all([o_f]),["fox"]])
}
}
EOF
touch quick brown fox

$miniwdl run --dir taskrun/. echo_task.wdl s=foo i=42 f=quick a_s=bar a_f=brown | tee stdout
$miniwdl run --dir taskrun/. echo_task.wdl s=foo i=42 f= quick a_s=bar a_f=brown --none o_a_s | tee stdout
is "$?" "0" "task run"
is "$(jq '.outputs["echo.out_i"]' stdout)" "42" "task stdout out_i"
is "$(jq '.["echo.out_i"]' taskrun/outputs.json)" "42" "task outputs.json out_i"
Expand Down

0 comments on commit 76ed3cc

Please sign in to comment.