Skip to content

Commit a9c35ad

Browse files
committed
add exclude options to control result of CWL against tool/command
1 parent 0ef56e8 commit a9c35ad

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/goldfinch/utils/click2cwl_cli.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,48 @@ def resolve_cli_command(
120120
default=None,
121121
help="If the python script defines multiple commands, this can be used to specify which one to convert.",
122122
)
123+
@click.option(
124+
"--exclude-help",
125+
is_flag=True,
126+
help=(
127+
"Exclude the '-h/--help' option (if any) typically added by '@click.help_option()' "
128+
"from the process/command to avoid it being included as CWL input parameter. "
129+
"Ignored if no such option is found in the referenced Click command."
130+
)
131+
)
132+
@click.option(
133+
"--exclude-version",
134+
is_flag=True,
135+
help=(
136+
"Exclude the '--version' option (if any) typically added by '@click.version_option()' "
137+
"from the process/command to avoid it being included as CWL input parameter. "
138+
"Ignored if no such option is found in the referenced Click command. "
139+
)
140+
)
141+
@click.option(
142+
"--exclude-deprecated",
143+
is_flag=True,
144+
help=(
145+
"Exclude all options (if any) marked as deprecated (i.e.: with 'click.Option(..., deprecated=True)') "
146+
"from the process/command to avoid them being included as CWL input parameters. "
147+
"Ignored if no such option is found in the referenced Click command. "
148+
)
149+
)
150+
@click.option(
151+
"--exclude-option",
152+
help=(
153+
"Exclude a named option from the process/command to avoid it being included as CWL input parameter. "
154+
"The specified option must be *optional* (i.e.: prefixed with '--', no required flag or defines a default). "
155+
"Required parameters will be refused to ensure the CWL definition remains consistent with the tool. "
156+
"The provided option name is case sensitive and should omit the '--' prefix. "
157+
"For convenience, underscores ('_') and dashes ('-') will be handled interchangeably. "
158+
"Ignored if no option matching the name is found in the referenced Click command. "
159+
"Can be repeated multiple times for multiple options to exclude. "
160+
"See also '--exclude-help', '--exclude-version' and '--exclude-deprecated' "
161+
"for more specific handling of these special definitions."
162+
),
163+
multiple=True,
164+
)
123165
@click.option(
124166
"-o", "--output",
125167
type=click.Path(exists=False, dir_okay=False),
@@ -244,6 +286,31 @@ def main(ctx: click.Context, **kwargs: str) -> None:
244286
cli_name, cli_cmd = click_functions[0]
245287
cli_prog_call = resolve_cli_command(**kwargs)
246288

289+
# cleanup extra options as requested
290+
for remove_option in ["exclude_version", "exclude_help", "exclude_deprecated"]:
291+
if kwargs.get(remove_option):
292+
name = remove_option.replace("exclude_", "")
293+
for i, param in reversed(list(enumerate(cli_cmd.params))): # reversed to avoid index issues when deleting
294+
if (
295+
param.param_type_name == "option" and (
296+
(param.name == name and param.is_flag and param.is_bool_flag and not param.expose_value) or
297+
(name == "deprecated" and param.deprecated)
298+
)
299+
):
300+
del cli_cmd.params[i]
301+
if name != "deprecated":
302+
break
303+
for remove_option in kwargs.get("exclude_option") or []:
304+
for i, param in reversed(list(enumerate(cli_cmd.params))): # reversed to avoid index issues when deleting
305+
name = remove_option.replace("-", "_")
306+
if param.param_type_name == "option" and param.name == name:
307+
if param.required:
308+
raise click.UsageError(
309+
f"Parameter '{remove_option}' is required. Cannot exclude it from the command.",
310+
)
311+
del cli_cmd.params[i]
312+
break
313+
247314
# add context arguments to generate the CWL contents
248315
cli_ctx = cli_cmd.context_class(info_name=cli_prog_call, command=cli_cmd)
249316
output_cwl = True # stdout or override file path

0 commit comments

Comments
 (0)