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

Add support for TyperArgument arguments #59

Closed
taranlu-houzz opened this issue Apr 20, 2022 · 17 comments
Closed

Add support for TyperArgument arguments #59

taranlu-houzz opened this issue Apr 20, 2022 · 17 comments
Labels
bug Something isn't working high priority

Comments

@taranlu-houzz
Copy link
Contributor

It seems like typer.Argument params are being grouped in with the other options. This happens whether or not SHOW_ARGUMENTS is enabled.

image

@ewels ewels added bug Something isn't working high priority labels Apr 21, 2022
@ewels ewels closed this as completed in 0956afe May 14, 2022
@ewels
Copy link
Owner

ewels commented May 14, 2022

Thanks for reporting this! I was checking for click.core.Argument but Typer has it's own type, typer.core.Argument as you said.. So now the code should detect either of these types and skip unless SHOW_ARGUMENTS is specified.

Let me know how you get on with it!

Cheers,

Phil

@taranlu-houzz
Copy link
Contributor Author

Hey @ewels, I just updated to the latest release of rich-click, but I am now getting this error (the first part of the help was printed before hitting this error):

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/---/cli.py", line 267, in run_app
    APP()
  File "/---/lib/python3.10/site-packages/typer/main.py", line 214, in __call__
    return get_command(self)(*args, **kwargs)
  File "/---/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/---/lib/python3.10/site-packages/rich_click/rich_group.py", line 21, in main
    return super().main(*args, standalone_mode=False, **kwargs)
  File "/---/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/---/lib/python3.10/site-packages/click/core.py", line 1655, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "/---/lib/python3.10/site-packages/click/core.py", line 920, in make_context
    self.parse_args(ctx, args)
  File "/---/lib/python3.10/site-packages/click/core.py", line 1378, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/---/lib/python3.10/site-packages/click/core.py", line 2360, in handle_parse_result
    value = self.process_value(ctx, value)
  File "/---/lib/python3.10/site-packages/click/core.py", line 2322, in process_value
    value = self.callback(ctx, self, value)
  File "/---/lib/python3.10/site-packages/click/core.py", line 1273, in show_help
    echo(ctx.get_help(), color=ctx.color)
  File "/---/lib/python3.10/site-packages/click/core.py", line 699, in get_help
    return self.command.get_help(self)
  File "/---/lib/python3.10/site-packages/click/core.py", line 1298, in get_help
    self.format_help(ctx, formatter)
  File "/---/lib/python3.10/site-packages/rich_click/rich_command.py", line 32, in format_help
    rich_format_help(self, ctx, formatter)
  File "/---/lib/python3.10/site-packages/rich_click/rich_click.py", line 459, in rich_format_help
    _get_parameter_help(param, ctx),
  File "/---/lib/python3.10/site-packages/rich_click/rich_click.py", line 245, in _get_parameter_help
    if param.allow_from_autoenv and ctx.auto_envvar_prefix is not None and param.name is not None:
AttributeError: 'TyperArgument' object has no attribute 'allow_from_autoenv'

@ewels
Copy link
Owner

ewels commented May 17, 2022

Ah that's no good, thanks for letting me know. I've moved your comment to a new issue (#70) to track. I'll fix + release ASAP.

@taranlu-houzz
Copy link
Contributor Author

@ewels Hey, thanks for the quick fix. The error is gone now, but I still am having the initial issue (using the latest version of rich-click that you just released). Even though I have SHOW_ARGUMENTS set, it still groups them under options as shown in the initial screenshot I posted.

I have also tried explicitly setting GROUP_ARGUMENTS_OPTIONS = False, but the did not help.

@ewels
Copy link
Owner

ewels commented May 17, 2022

Ah really? It should now hide the arguments by default.. 🤔 (If you set SHOW_ARGUMENTS to True it'll go back to the same behaviour you saw above).

It works for me with examples/typer/02_subcommands.py (note NAME is a positional argument and doesn't show in the table):

$ python examples/typer/02_subcommands.py download --help
Debug mode is off

 Usage: 02_subcommands.py download [OPTIONS] NAME

 Pretend to download some files from somewhere. Multi-line help strings are unwrapped until you use a double newline.
 Only the first paragraph is used in group help texts. Don't forget you can opt-in to rich and markdown formatting!
 Click escape markers should still work.
   * So you
   * Can keep
   * Your newlines

 And this is a paragraph that will be rewrapped again.

╭─ Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --all/--no-all      Get everything [default: no-all]                                                                                       │
│ --help              Show this message and exit.                                                                                            │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Can you add an example script to this issue and what you expect it to show?

It's entirely possible that GROUP_ARGUMENTS_OPTIONS doesn't work with Typer arguments. They're a different type to Click and I don't remember messing with that code so I probably missed that 🤔

@ewels ewels reopened this May 17, 2022
@ewels
Copy link
Owner

ewels commented May 17, 2022

Yup, there are a few instances of click.core.Argument which need TyperArgument support adding to them..

@ewels ewels changed the title typer.Argument shows up in the Options section Add support for TyperArgument arguments May 17, 2022
@taranlu-houzz
Copy link
Contributor Author

Hey, sorry! Unfortunately, I don't have time to cleanup an example right now (will try to later if still an issue). It sounds like you have managed to identify where the problem is though, right?

@ewels
Copy link
Owner

ewels commented May 18, 2022

It sounds like you have managed to identify where the problem is though, right?

Potentially, but I'm not sure it's the same - I was able to replicate your original issue to begin with but not since 0956afe

I'll try to find some time to play with the TyperArgument stuff a bit more if I can, it will hopefully solve your issues. It's just a little risky without a test case as if I can't replicate your issue I don't know if I've fixed it or not 😉

@taranlu-houzz
Copy link
Contributor Author

Makes sense. I will try to get a cut down example to you when I have a chance.

@AlirezaTheH
Copy link
Contributor

Any update on this?

@ewels
Copy link
Owner

ewels commented Jul 6, 2022

Nope, still need a minimal example to replicate the problem.

@zmoon
Copy link
Contributor

zmoon commented Jul 7, 2022

How about this?

from rich_click import typer

typer.rich_click.SHOW_ARGUMENTS = True


def cli(
    name: str = typer.Argument("World"),
    say_hi: bool = typer.Option(True),
):
    if say_hi:
        print(f"Hello, {name}!")


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

The argument shows with SHOW_ARGUMENTS = True set, but in the Options section. Same result regardless of the setting of GROUP_ARGUMENTS_OPTIONS.

image

@AlirezaTheH
Copy link
Contributor

@ewels If you need any help on this just say the word.

@ewels
Copy link
Owner

ewels commented Jul 10, 2022

I've just started my summer holiday so I'll be slow to tackle this now. So yeah if you fancy taking a crack at it that would be fab 👍🏻

@AlirezaTheH
Copy link
Contributor

OK, I'll give it a try.

@taranlu-houzz
Copy link
Contributor Author

@AlirezaTheH @ewels @zmoon Hey, sorry I never got back on this. Thanks for taking things forward!

ewels added a commit that referenced this issue Aug 1, 2022
@ewels
Copy link
Owner

ewels commented Aug 1, 2022

Fixed in #82

@ewels ewels closed this as completed Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

No branches or pull requests

4 participants