-
I'm not sure whether this is a bug or a feature request. I think the behavior is not documented, so it might be a bug. Related problemIn the docs, it says that when adding only one command to an app, 'Typer is smart enough to create a CLI application with that single function as the main CLI application, not as a command/subcommand' (no click group is created). See this example, which can be run with # welcome.py
import typer
app = typer.Typer()
@app.command()
def main():
typer.echo("Hello World!")
if __name__ == "__main__":
app() When I add this typer app to another app, the behavior changes. I add this file: # parent.py
import typer
import welcome
app = typer.Typer()
app.add_typer(welcome.app, name="welcome")
@app.command()
def main():
typer.echo("Hello parent app!")
if __name__ == "__main__":
app() When I run
The solution you would likeI expect the behavior of a typer app to stay consistent, no matter whether it is the top-level-app or a sub-app of another one. In the example above, I expect the output to be Describe alternatives you've consideredThere is a workaround to get the desired behavior: Replace the line |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
This would indeed be really useful as single commands of a I also agree with you when it comes to the workaround you described and would like to add: With the workaround, the decorator |
Beta Was this translation helpful? Give feedback.
-
+1 ran into the same. Also thanks for the workaround |
Beta Was this translation helpful? Give feedback.
-
Just ran into this issue. I kept searching the documentation and online before I remembered to search here, and found this. It would at least be good to explicitly mention this behavior in the documentation, if not change it. |
Beta Was this translation helpful? Give feedback.
-
(Disclaimer: I'm brand-new to #119 is also relevant to this. The approach by @cataerogong in this comment is similar to what I'm using. The relevant part of that comment roughly being: import child
app = typer.Typer()
if len(child.app.registered_commands) == 1:
app.command('child_name')(child.app.registered_commands[0].callback)
else:
app.add_typer(child.app, name='child_name') (Note: I haven't yet considered how run-time changes might affect this approach.) For now, I monkeypatched typer.Typer.add_typer so that it essentially does something very similar to the above (modulo how the child's name is set). Though I would prefer it if typer had, built-in, at least an option to have this kind of behavior--so that it can still be backward-compatible with how people might have come to expect add_typer() behavior for such single command sub-apps, but optionally mirror the semantics that the docs describe for single command apps in general (i.e. if the (sub-)app has only 1 command, there's no need to include the command name when executing it). (@tiangolo gives another solution here which I had already considered, but my implementation was buggin' out when I would pass e.g. an argument to the sub-app. I haven't spent enough time to see if it's a bug with my code or something else, but his alternative is probably also worth trying.) |
Beta Was this translation helpful? Give feedback.
-
sorry for this +1 reply but just ran into exactly the same issue. |
Beta Was this translation helpful? Give feedback.
-
Struggled with this for a while thank you for this, I would be happy for this to exist in the docs under sub-commands. |
Beta Was this translation helpful? Give feedback.
-
This will be fixed in #1037 |
Beta Was this translation helpful? Give feedback.
-
With Typer 0.15.0 you can now omit # welcome.py
import typer
app = typer.Typer()
@app.command()
def welcome(): # this will be the name for this command
typer.echo("Hello World!")
if __name__ == "__main__":
app() # parent.py
import typer
import welcome
app = typer.Typer()
app.add_typer(welcome.app)
@app.command()
def main():
typer.echo("Hello parent app!")
if __name__ == "__main__":
app() |
Beta Was this translation helpful? Give feedback.
With Typer 0.15.0 you can now omit
name
when usingadd_typer
and get this behavior, see: