diff --git a/src/eessi/cli/check.py b/src/eessi/cli/check.py index 3eef1e3..6f37bd6 100644 --- a/src/eessi/cli/check.py +++ b/src/eessi/cli/check.py @@ -4,11 +4,22 @@ import typer +from eessi.cli.help import help_callback + app = typer.Typer() @app.command() -def check(): +def check( + help: bool = typer.Option( + None, # default value + "-h", + "--help", + help="Show this message and exit.", + callback=help_callback, + is_eager=True, + ), +): """ Check CernVM-FS setup for accessing EESSI """ diff --git a/src/eessi/cli/help.py b/src/eessi/cli/help.py new file mode 100644 index 0000000..38b6f3d --- /dev/null +++ b/src/eessi/cli/help.py @@ -0,0 +1,34 @@ +# license (SPDX): GPL-2.0-only +# +# authors: +# - Kenneth Hoste (Ghent University) +# - Alex Domingo (Vrije Universiteit Brussel) + +import click +import typer +from rich import print as rich_print +from typer import rich_utils + +import eessi + + +def help_callback(ctx: click.Context, param: click.Parameter, value: bool): + """ + Show default help with rich and exit early. + """ + # ensures this doesn't run during completion or other early parsing phases + if not value or ctx.resilient_parsing: + return + + # print default help with rich + rich_utils.rich_format_help(obj=ctx.command, ctx=ctx, markup_mode="rich") + ctx.exit() + +def version_callback(value: bool): + """ + Show version and exit early. + """ + if value: + rich_print(f"[bold]eessi[/bold] version {eessi.__version__}") + raise typer.Exit() + diff --git a/src/eessi/cli/init.py b/src/eessi/cli/init.py index 8f63dd0..ab5c279 100644 --- a/src/eessi/cli/init.py +++ b/src/eessi/cli/init.py @@ -4,13 +4,25 @@ import os import sys + import typer +from eessi.cli.help import help_callback + app = typer.Typer() @app.command() -def init(): +def init( + help: bool = typer.Option( + None, # default value + "-h", + "--help", + help="Show this message and exit.", + callback=help_callback, + is_eager=True, + ), +): """ Initialize shell environment for using EESSI """ diff --git a/src/eessi/cli/main.py b/src/eessi/cli/main.py index c794be7..801ba01 100644 --- a/src/eessi/cli/main.py +++ b/src/eessi/cli/main.py @@ -1,40 +1,51 @@ # license (SPDX): GPL-2.0-only # -# authors: Kenneth Hoste (Ghent University) +# authors: +# - Kenneth Hoste (Ghent University) +# - Alex Domingo (Vrije Universiteit Brussel) import typer -import eessi from eessi.cli.check import app as check_app +from eessi.cli.help import help_callback, version_callback from eessi.cli.init import app as init_app from eessi.cli.shell import app as shell_app app = typer.Typer( help="User-friendly command line interface to EESSI - https://eessi.io", - context_settings={"help_option_names": ["-h", "--help"]}, + # display help if no arguments given no_args_is_help=True, + # we use custom help option to control its placement + add_help_option=False, ) app.add_typer(check_app) app.add_typer(init_app) app.add_typer(shell_app) -def version_callback(value: bool): - if value: - print(f"eessi version {eessi.__version__}") - raise typer.Exit() - @app.callback() def main( + help: bool = typer.Option( + None, # default value + "-h", + "--help", + help="Show this message and exit.", + callback=help_callback, + is_eager=True, + ), version: bool = typer.Option( None, # default value - "-v", # short option - "--version", # long option - help="Show version of eessi CLI", + "-v", + "--version", + help="Show version of eessi CLI.", callback=version_callback, + is_eager=True, ), ): + """ + Top level eessi command + """ pass diff --git a/src/eessi/cli/shell.py b/src/eessi/cli/shell.py index c8447b2..82be213 100644 --- a/src/eessi/cli/shell.py +++ b/src/eessi/cli/shell.py @@ -5,9 +5,12 @@ import subprocess import sys import tempfile +from typing import Annotated + import typer from rich import print as rich_print -from typing import Annotated + +from eessi.cli.help import help_callback app = typer.Typer() @@ -24,7 +27,17 @@ def report_error(msg, exit_code: int = 1): @app.command() def shell( - eessi_version: Annotated[str, typer.Option(help="EESSI version")] = '', + help: bool = typer.Option( + None, # default value + "-h", + "--help", + help="Show this message and exit.", + callback=help_callback, + is_eager=True, + ), + eessi_version: Annotated[str, typer.Option( + help="EESSI version" + )] = '', ): """ Create subshell in which EESSI is available and initialised