typer icon indicating copy to clipboard operation
typer copied to clipboard

Chaining of commands does not work when add_typer is used

Open dhoomakethu opened this issue 1 year ago • 1 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

# This works

import typer

app = typer.Typer(chain=True)

@app.command()
def cmd1(ctx: typer.Context, name: str = "123"):
    print(f"In command 1  {name}")

@app.command()
def cmd2(ctx: typer.Context, name: str = "345"):
    print(f"In command 2  {name}")

if __name__ == "__main__":
    app()

# Output
In command 1  34345
In command 2  4546

## This does not work
import typer

app = typer.Typer(chain=True)
app2 = typer.Typer(chain=True)


@app.command()
def cmd1(ctx: typer.Context, name: str = "123"):
    print(f"In command 1  {name}")

@app2.command()
def cmd2(ctx: typer.Context, name: str = "345"):
    print(f"In command 2  {name}")


app.add_typer(app2)

if __name__ == "__main__"
     app()

## Output
Error: No such command 'cmd2'

Description

Add Typer objects to an app with add_typer method and try to chain commands across apps does not work. Chaining works for commands under the same app.

Operating System

macOS

Operating System Details

No response

Typer Version

0.6.1

Python Version

Python 3.8.13

Additional Context

No response

dhoomakethu avatar Sep 23 '22 13:09 dhoomakethu

Enabling chain for the subcommand makes it hard to interpret whether the next command is part of the subgroup or main group. What worked from me is to only have chain enabled for the main app but not for the subcommand apps.

app = typer.Typer(chain=True)
app2 = typer.Typer(chain=False)

PHAC-LewisLiu avatar Oct 26 '23 15:10 PHAC-LewisLiu