Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove [default: None] for arguments and required options #465

Open
7 tasks done
taranlu-houzz opened this issue Oct 7, 2022 · 5 comments · May be fixed by #1120
Open
7 tasks done

Remove [default: None] for arguments and required options #465

taranlu-houzz opened this issue Oct 7, 2022 · 5 comments · May be fixed by #1120
Labels
feature New feature, enhancement or request investigate

Comments

@taranlu-houzz
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

import typer


def main(
    name: str,
    option_1: str = typer.Option(
        "option_1_default",
    ),
    option_2: str = typer.Option(
        ...,
    ),
):
    print(f"Hello {name}")
    print(option_1)
    print(option_2)


if __name__ == "__main__":
    typer.run(main)

Description

In order to make the help output less cluttered, it makes sense to hide the [default: None] for arguments and required options since a value must always be passed (meaning that the default doesn't really have any use).

As an aside, I supposed that the default is sometimes used as an "example," so maybe it could make sense to add an example param that could be used for this purpose?

Wanted Solution

Current help output:

❯ pdm run python typer_issue.py --help

 Usage: typer_issue.py [OPTIONS] NAME

╭─ Arguments ──────────────────────────────────────────────────╮
│ *    name      TEXT  [default: None] [required]              │
╰──────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────╮
│    --option-1                  TEXT  [default:               │
│                                      option_1_default]       │
│ *  --option-2                  TEXT  [default: None]         │
│                                      [required]              │
│    --install-completion              Install completion for  │
│                                      the current shell.      │
│    --show-completion                 Show completion for the │
│                                      current shell, to copy  │
│                                      it or customize the     │
│                                      installation.           │
│    --help                            Show this message and   │
│                                      exit.                   │
╰──────────────────────────────────────────────────────────────╯

Desired help output:

❯ pdm run python typer_issue.py --help

 Usage: typer_issue.py [OPTIONS] NAME

╭─ Arguments ──────────────────────────────────────────────────╮
│ *    name      TEXT                  [required]              │
╰──────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────╮
│    --option-1                  TEXT  [default:               │
│                                      option_1_default]       │
│ *  --option-2                  TEXT  [required]              │
│    --install-completion              Install completion for  │
│                                      the current shell.      │
│    --show-completion                 Show completion for the │
│                                      current shell, to copy  │
│                                      it or customize the     │
│                                      installation.           │
│    --help                            Show this message and   │
│                                      exit.                   │
╰──────────────────────────────────────────────────────────────╯

Wanted Code

# No changes to the way `typer` is used

Alternatives

No response

Operating System

Linux, macOS

Operating System Details

No response

Typer Version

0.6.1

Python Version

Python 3.8.14

Additional Context

Obviously, this is a personal preference. I just feel that it helps to clean up the --help output a little bit.

@taranlu-houzz taranlu-houzz added the feature New feature, enhancement or request label Oct 7, 2022
@taranlu-houzz
Copy link
Author

I didn't think about it before, but it seems like an obvious solution to this is to just use show_default=False. Still, might be worth changing the default behavior.

@varioustoxins
Copy link

when using show_default = False, it would be nice if a default added by the programmer [which is sometimes the reason for this] it would be nice if there is something that looks like a default e.g [default: ....] it would also be coloured grey like the normal default text [or is that too magical] I tried with rich styling but it didn't seem to work but my typer maybe too old 0.7.0...

@peterjc
Copy link

peterjc commented Oct 11, 2024

Confirming using typer.Argument(..., show_default=False) or typer.Option(..., show_default=False) has the desired behaviour on required arguments and options with typer 0.12.5, but this is a verbose workaround.

@matdmul
Copy link

matdmul commented Nov 8, 2024

This seems to have regressed in 0.13.0 (works in 0.12.5)?

@mattmess1221
Copy link

I would suggest the following code change to rich_utils.py

typer/typer/rich_utils.py

Lines 288 to 302 in f110845

if param.show_default:
show_default_is_str = isinstance(param.show_default, str)
default_value = param._extract_default_help_str(ctx=ctx)
default_str = param._get_default_string(
ctx=ctx,
show_default_is_str=show_default_is_str,
default_value=default_value,
)
if default_str:
items.append(
Text(
DEFAULT_STRING.format(default_str),
style=STYLE_OPTION_DEFAULT,
)
)

diff --git a/typer/rich_utils.py b/typer/rich_utils.py
index 7d603da..46e2474 100644
--- a/typer/rich_utils.py
+++ b/typer/rich_utils.py
@@ -285,9 +285,9 @@ def _get_parameter_help(
     # Default value
     # This uses Typer's specific param._get_default_string
     if isinstance(param, (TyperOption, TyperArgument)):
-        if param.show_default:
-            show_default_is_str = isinstance(param.show_default, str)
-            default_value = param._extract_default_help_str(ctx=ctx)
+        default_value = param._extract_default_help_str(ctx=ctx)
+        show_default_is_str = isinstance(param.show_default, str)
+        if show_default_is_str or (default_value is not None and (param.show_default or ctx.show_default)):
             default_str = param._get_default_string(
                 ctx=ctx,
                 show_default_is_str=show_default_is_str,

This would make it align better with other implementations in core.py

typer/typer/core.py

Lines 351 to 368 in f110845

default_value = self._extract_default_help_str(ctx=ctx)
# Typer override end
show_default_is_str = isinstance(self.show_default, str)
if show_default_is_str or (
default_value is not None and (self.show_default or ctx.show_default)
):
# Typer override:
# Extracted to _get_default_string() to allow re-using it in rich_utils
default_string = self._get_default_string(
ctx=ctx,
show_default_is_str=show_default_is_str,
default_value=default_value,
)
# Typer override end
if default_string:
extra.append(_("default: {default}").format(default=default_string))

mattmess1221 added a commit to mattmess1221/typer that referenced this issue Jan 14, 2025
…s installed

Fixes fastapi#465

Improves consitency with typer/core.py TyperArgument.get_help_record, which does not print the default value if it is None.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request investigate
Projects
None yet
6 participants