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

[QUESTION] is there any way to add fallback function if no command name has been entered? #339

Closed
7 tasks done
smahm006 opened this issue Nov 11, 2021 · 1 comment
Closed
7 tasks done
Labels
question Question or problem

Comments

@smahm006
Copy link

smahm006 commented Nov 11, 2021

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

import typer
from enum import Enum

app = typer.Typer()

class Product(str, Enum):
    toy = "toy"
    phone ="phone"
     
@app.command("toy")
# called with "python3 main.py test toy"
def toy_test(product: str = Product.toy, sn: str = typer.Option(..., prompt="Please input the Serial Number")):
    print("Testing toy product")
    print("SN is: ", sn)

@app.command("phone")
# called with "python3 main.py test phone"
def phone_test(product: str = Product.phone, sn: str = typer.Option(..., prompt="Please input the Serial Number")):
    print("Testing phone product")
    print("SN is: ", sn)

@app.command(None)
# called with just "python3 main.py test"
def choose_product(product: Product = typer.Option(..., prompt="Please choose the Product type -")
   if product == "toy":
      typer.run(toy_test)
   if product == "phone":
      typer.run(phone_test)

Description

Forgive me if this question seems simple or has been answered before,

I would like to be able to run my cli with a command name which would correspond to testing the product of choice, but would also like the freedom of inputting no command and letting me choose the product.

I have a nested cli with "main.add_typer(test_cli, name='test')"

  • main.py
    • test_cli.py

For example, if my code above was called test_cli.py, I would like to run
"python3 main.py test toy" -- this would test the toy product
but if I run
"python3 main.py test" -- this would ask me to choose which product to test and would then run the corresponding test function as if it was run from the command line (so all the Options/Arguments will be asked)

I have tried using name == "test_cli" but that runs with or without a command.

Is this possible?

Operating System

Linux

Operating System Details

No response

Typer Version

0.4.0

Python Version

3.8

Additional Context

No response

@smahm006 smahm006 added the question Question or problem label Nov 11, 2021
@smahm006
Copy link
Author

My bad, didn't look far back enough. Found a solution #18

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

No branches or pull requests

1 participant