From 0086b7b316dd48eb3e3de24d67d5319334a8a38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 5 Jan 2020 21:17:03 +0100 Subject: [PATCH] :bug: Fix help extraction implementation and priority --- typer/main.py | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/typer/main.py b/typer/main.py index 48e5045..ecc4419 100644 --- a/typer/main.py +++ b/typer/main.py @@ -261,33 +261,44 @@ def get_group_name(typer_info: TyperInfo) -> Optional[str]: def solve_typer_info_help(typer_info: TyperInfo) -> str: - # Priority 1a: Value was set in app.add_typer() + # Priority 1: Explicit value was set in app.add_typer() if not isinstance(typer_info.help, DefaultPlaceholder): return inspect.cleandoc(typer_info.help or "") - # Priority 1b: Value was set in app.add_typer(), in callback docstring + # Priority 2: Explicit value was set in sub_app.callback() + try: + callback_help = typer_info.typer_instance.registered_callback.help + if not isinstance(callback_help, DefaultPlaceholder): + return inspect.cleandoc(callback_help or "") + except AttributeError: + pass + # Priority 3: Explicit value was set in sub_app = typer.Typer() + try: + instance_help = typer_info.typer_instance.info.help + if not isinstance(instance_help, DefaultPlaceholder): + return inspect.cleandoc(instance_help or "") + except AttributeError: + pass + # Priority 4: Implicit inference from callback docstring in app.add_typer() if typer_info.callback: doc = inspect.getdoc(typer_info.callback) if doc: return doc + # Priority 5: Implicit inference from callback docstring in @app.callback() try: - # Priority 2a: Value was set in @subapp.callback() - doc = typer_info.typer_instance.registered_callback.help - if not isinstance(doc, DefaultPlaceholder): - return inspect.cleandoc(doc or "") - # Priority 2b: Value was set in @subapp.callback(), in callback docstring - doc = inspect.getdoc(typer_info.typer_instance.registered_callback.callback) - if doc: - return doc + callback = typer_info.typer_instance.registered_callback.callback + if not isinstance(callback, DefaultPlaceholder): + doc = inspect.getdoc(callback or "") + if doc: + return doc except AttributeError: pass + # Priority 6: Implicit inference from callback docstring in typer.Typer() try: - # Priority 3a: Value set in subapp = typer.Typer() - instance_value = typer_info.typer_instance.info.help - if not isinstance(instance_value, DefaultPlaceholder): - return inspect.cleandoc(instance_value or "") - doc = inspect.getdoc(typer_info.typer_instance.callback) - if doc: - return doc + instance_callback = typer_info.typer_instance.info.callback + if not isinstance(instance_callback, DefaultPlaceholder): + doc = inspect.getdoc(instance_callback) + if doc: + return doc except AttributeError: pass # Value not set, use the default