typer
typer copied to clipboard
Is there a way to show option related to a sub-commands using --help
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
# main file
import typer
import commands
app = typer.Typer()
app.add_typer(typer_instance=commands.make_structure.app, name="create", help="Create the project structure")
app.add_typer(typer_instance=commands.lang_choise.app, name="lang", help="Pick C or Cpp language")
# TODO: add a callback to the CLI indicate what's going on!!!
if __name__== "__main__":
app()
# command 1
import typer
from utils.utils import create_main
from enums.prog_lang import ProgLang
app = typer.Typer()
@app.command("lang")
def language_choise(lang: ProgLang = ProgLang.c.value):
# set the directory to work with
if lang.value == "C":
# TODO: create the main file in /src directory and write to it
lang_code = 0
if create_main(lang_code) != 0:
print("exit code 2")
return 2
# TODO: Write in the makefile
pass
else:
# TODO: create the main file in /src directory and write to it
lang_code = 1
if (create_main(lang_code) != 0):
print("exit code 2")
return 2
# TODO: write in the makefile
pass
# command 2
from typing import Optional
import typer
import os
from utils.utils import build_hiarchy
app = typer.Typer()
# TODO: write the docs for every componenent in the app
@app.command("create")
def make_struct(name: str = typer.Option(...,"--name", "-n"), verbose: bool=typer.Option(False, "--verbose", "-v")) -> int:
dir = os.getcwd()
subdirs_to_create = ["src", "bin", "obj"]
file_to_create = "Makefile"
project_directory = os.path.join(dir,name)
# build hiarchy
if build_hiarchy(project_directory, subdirs_to_create, file_to_create, verbose) == 0:
return 0
typer.echo("exit code 1")
return 1
Description
I created a small CLI scripts for building development environment for C projects.
I divided my projects to main file situated in the main workspace directory and a package called command where I development most of the sub-commands.
Using the API typer.add_typer(), I included the sub-commands in the main file.
The issue is when I run the program : python3 main.py [sub-command] --help it doesn't show the option available for the that sub-command
Operating System
Linux
Operating System Details
No response
Typer Version
0.3.2
Python Version
3.9.8
Additional Context
No response
In the subcommand files use app.callback()
instead app.command()
and you will see what you expect.
cli.py
:
import typer
import commands
app = typer.Typer(add_completion=False)
app.add_typer(typer_instance=commands.make_structure.app, name="create", help="Create the project structure")
app.add_typer(typer_instance=commands.lang_choise.app, name="lang", help="Pick C or Cpp language")
if __name__== "__main__":
app()
❯ python issue.py --help
Usage: issue.py [OPTIONS] COMMAND [ARGS]...
Options:
--help Show this message and exit.
Commands:
create Create the project structure
lang Pick C or Cpp language
commands/lang_choise.py
:
import typer
app = typer.Typer(add_completion=False)
@app.callback("lang")
def language_choise(choise: str = typer.Argument(...)):
# set the directory to work with
print(choise)
❯ python cli.py lang --help
Usage: cli.py lang [OPTIONS] CHOISE COMMAND [ARGS]...
Pick C or Cpp language
Arguments:
CHOISE [required]
Options:
--help Show this message and exit.
commands/make_structure.py
:
import typer
app = typer.Typer(add_completion=False)
@app.callback("create")
def make_struct(name: str = typer.Option(..., "--name", "-n"), verbose: bool = typer.Option(False, "--verbose", "-v")):
print(name, verbose)
❯ python cli.py create --help
Usage: cli.py create [OPTIONS] COMMAND [ARGS]...
Create the project structure
Options:
-n, --name TEXT [required]
-v, --verbose
--help Show this message and exit.