fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.44k stars 656 forks source link

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

Open taranlu-houzz opened 1 year ago

taranlu-houzz commented 1 year ago

First Check

Commit to Help

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 commented 1 year ago

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 commented 1 month ago

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...