[FEATURE] Set a defaut command in multiple commands
See original GitHub issueI wanna to be able to set a default command when I have multiples commands. For example, with:
import typer
app = typer.Typer()
@app.command()
def hello(name: str):
typer.echo(f"Hello {name}")
@app.command()
def goodbye(name: str, formal: bool = False):
if formal:
typer.echo(f"Goodbye Ms. {name}. Have a good day.")
else:
typer.echo(f"Bye {name}!")
if __name__ == "__main__":
app()
I would like to have some way of being able to call it without specifying the command, calling a default command (maybe with something like @app.command(default=True) like this:
$ python main.py Hiro
Hello Hiro
$ python main.py helo Hiro # This also should work
Hello Hiro
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Lesson 1.2: Multiple, Default, and Hidden Commands
With Spectre.Console.Cli, we can define multiple commands, each with their own arguments and options, and each getting called when the user ...
Read more >One or Multiple Commands - Typer - tiangolo
Typer, build great CLIs. Easy to code. Based on Python type hints.
Read more >Modular Commands — cmd2 2.4 documentation
CommandSets represent a logical grouping of commands within an cmd2 application. By default, all CommandSets will be discovered and loaded automatically ...
Read more >Commands and Groups — Click Documentation (7.x)
The default implementation for such a merging system is the CommandCollection class. It accepts a list of other multi commands and makes the...
Read more >combine multiple commands into one r function - Stack Overflow
I need to create a function that will return several things, I started with a real data example and was able to get...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

UPDATE 2: I can accomplish what I need using the context from within the callback:
This yields the CLI I wanted. 😃
UPDATE: I found the
@APP.callback(invoke_without_command=True)stuff, which lets me run the callback like a command, I think that is probably enough for me to build the CLI I would like.@tiangolo - I love the docs for this project, but when searching for specific options, I do miss an API reference page, would something like that be possible to add at some point? I’d be up for helping to build it, if that would help?
Apologies for opening an old thread, I bumped into this scenario just recently…
Is there a way to use a callback with a set of arguments, without specifying a subcommand?
What I would like to do, is provide a CLI along the lines of:
tool release --option a, alongsidetool release all. I would like these two actions to be exclusive, if I provide options to the default, but name no other command, I’d like to run that default command. If I provide a command to typer, I would like to run that command without (or simply skipping), the default command…For example, here’s a minimal reproduction of what I currently see (in reality, this CLI is nested into a larger project, hence the APP):
Outputs:
Given the above desired output, I feel that perhaps some kind of default subcommand, rather than a callback would be more appropriate? An example of a CLI like this would be
git remote, where it has a set of functionality and options for justgit remote, but also includes subcommands likeaddetc.Is there something I’ve missed that would let me build something like this?
Thanks in advance, Typer is a wonderful library!
@ABitMoreDepth I am trying to do something similar to your example here, but where I have a
strargument to my callback like this:Unfortunately, if I try to call
python test.py subcommand, typer interprets"subcommand"as the string argument for"example_arg"and thereforectx.invoked_subcommand is Noneso it runs the callbackmain, notsubcommandas I would expect.When
"example_arg"is anint, like in your example, I simply get an errorI am wondering if and how you got around this?