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

Chaining of commands does not work when add_typer is used #460

Open
7 tasks done
dhoomakethu opened this issue Sep 23, 2022 · 2 comments
Open
7 tasks done

Chaining of commands does not work when add_typer is used #460

dhoomakethu opened this issue Sep 23, 2022 · 2 comments
Labels
investigate question Question or problem

Comments

@dhoomakethu
Copy link

dhoomakethu commented Sep 23, 2022

First Check

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

Commit to Help

  • 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 dhoomakethu added the question Question or problem label Sep 23, 2022
@PHAC-LewisLiu
Copy link

PHAC-LewisLiu commented Oct 26, 2023

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)

@mattytrentini
Copy link

What worked from me is to only have chain enabled for the main app but not for the subcommand apps.

It seems that configuring the subcommand to chain=False also behaves incorrectly if the subcommands accept parameters:

Given :

import typer

app = typer.Typer(chain=True)
sub_app = typer.Typer(chain=False)
app.add_typer(sub_app, name="sub")

@app.command()
def foo():
    print("foo")

@sub_app.command()
def bar(param: str):
    print(f"sub bar {param}")

if __name__ == "__main__":
    app()

Output:

>  python chain_sub_test.py foo
foo
> #

> python chain_sub_test.py sub bar "test"
Usage: chain_sub_test.py [OPTIONS] COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...
Try 'chain_sub_test.py --help' for help.
Error:
No such command 'test'.
> #

I haven't figured out a workaround that allows chained commands, including subcommands, that require parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate question Question or problem
Projects
None yet
Development

No branches or pull requests

4 participants