Is there a way to show option related to a sub-commands using --help #1325
-
First Check
Commit to Help
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 1DescriptionI created a small CLI scripts for building development environment for C projects. Operating SystemLinux Operating System DetailsNo response Typer Version0.3.2 Python Version3.9.8 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
In the subcommand files use
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
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.
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. |
Beta Was this translation helpful? Give feedback.
-
|
You just need to remove 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")You specified command name twice - in Related section of docs: https://typer.tiangolo.com/tutorial/one-file-per-command/ |
Beta Was this translation helpful? Give feedback.

You just need to remove
name="create"andname="lang"in these lines:You specified command name twice - in
command()decorator and inadd_typer(). In this case you would need to typepython main.py create createto access your command:Related section of docs: https://typer.tiangolo.com/tutorial/one-file-per-command/