typer icon indicating copy to clipboard operation
typer copied to clipboard

How to add extra options (a la "allow_extra_args", etc.) to a command's help?

Open nealian opened this issue 1 year ago • 2 comments

First Check

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

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

import typer

app = typer.Typer

@app.command(
    context_settings = {"allow_extra_args": True, "ignore_unknown_options": True}
)
def main(
    ctx: typer.Context,
    some_common_option: str = typer.Option(default='', help='Some Help'),
  ):
  """This is the command's overall description
  """
  handle_extra(ctx.args)

# I do this to work around the lack of support of multiple, multi-valued options (that is, repeated multi-valued options, like `--set-program-opt <program> <opt> <value>`)

Description

The above example should already output some nice help for the existing options.

I know that proper support for this sort of thing has been requested (or at least asked about; #387 for instance), but it's also possible to do things even crazier than that by handling your own extra options/arguments (variable multi-valued options? IDK people do some crazy things... And I'm one of them.)

The question is, then, is there a simple method of adding additional help bits on this command to properly document the extra options I am adding?

Operating System

Linux

Operating System Details

No response

Typer Version

0.6.1

Python Version

3.9.7

Additional Context

No response

nealian avatar Sep 14 '22 03:09 nealian

I guess this could also extend to completion, but one thing at a time 😅

nealian avatar Sep 14 '22 03:09 nealian

This is a big hack, but I did something similar by using Python's exec to invoke dynamically generated command line content. Something similar might work for you If you can get past the ugliness (and this is really ugly).

https://github.com/davidbrownell/v4-Common_Foundation/blob/c37f42ae79a00fd368cc3b3d41da0f47e2bd4f9c/Libraries/Python/Common_FoundationEx/src/Common_FoundationEx/BuildImpl/init.py#L246

davidbrownell avatar Sep 22 '22 22:09 davidbrownell

Nop, there's no way to do dynamic things like this. But I think that for your use case, the first thing would really be request for support for multiple multi-valued options, right?

tiangolo avatar Nov 14 '22 09:11 tiangolo

Right; the easier? Better? option would be to implement support for multiple multi-valued options, as in #459 (or #362). Would you be willing to give those a look? Thanks!

nealian avatar Nov 18 '22 01:11 nealian

Thanks! I'll check that out. 🤓

tiangolo avatar Nov 20 '22 11:11 tiangolo