From 8d25b4bcb817b7d89ee649c66da8fbd8cdcfabcd Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Fri, 18 Oct 2019 21:46:29 +0200 Subject: [PATCH] [scripts][cylc] Refactor version handling to match bash script output --- cylc/flow/scripts/cylc.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/cylc/flow/scripts/cylc.py b/cylc/flow/scripts/cylc.py index 0b47514bed2..2a4cd1c7e04 100644 --- a/cylc/flow/scripts/cylc.py +++ b/cylc/flow/scripts/cylc.py @@ -18,6 +18,8 @@ import click import pkg_resources +from cylc.flow import __version__ + # These will be ported to python as click commands bash_commands = ["cylc-graph-diff", "cylc-jobscript", "cylc-scp-transfer"] @@ -52,14 +54,35 @@ def execute_cmd(cmd, *args): raise SystemExit(exc) -@click.command(context_settings={"ignore_unknown_options": True}) -@click.option("--help", "-h", "help_", is_flag=True) +def print_version(ctx, param, value): + if not value or ctx.resilient_parsing: + return + click.echo(__version__) + ctx.exit() + + +CONTEXT_SETTINGS = dict( + token_normalize_func=lambda x: x.lower(), ignore_unknown_options=True +) + + +@click.command(context_settings=CONTEXT_SETTINGS) @click.argument("command") @click.argument("cmd-args", nargs=-1) -@click.version_option() +@click.option("--help", "-h", "help_", is_flag=True) +@click.option( + "--version", + "-v", + is_flag=True, + callback=print_version, + expose_value=False, + is_eager=True, +) def main(command, cmd_args, help_): if command in ["categories", "commands"] + category_list: execute_cmd("cylc-help", command) + elif command == "version": + click.echo(__version__) elif command: # to match bash script behavior if command == "h": @@ -92,5 +115,6 @@ def main(command, cmd_args, help_): elif help_: execute_cmd("cylc-help") + if __name__ == "__main__": main()