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

✨ Add ability to show sub-commands #1115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

rickwporter
Copy link

Rationale

When creating complex CLI programs, it is useful to see all the options available from/below a command.

Changes

Add Typer(command_tree=False) parameter to the constructor. When command_tree=True, there is a --show-sub-commands option available to see all the commands below the current one.

Example

See tests/assets/subcommand_tree.py for code. Here's an example of the output:

(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py --help
                                                                                                
 Usage: subcommand_tree.py [OPTIONS] COMMAND [ARGS]...                                          
                                                                                                
 Random help                                                                                    
                                                                                                
╭─ Options ────────────────────────────────────────────────────────────────────────────────────╮
│ --show-sub-commands           Show sub-command tree                                          │
│ --install-completion          Install completion for the current shell.                      │
│ --show-completion             Show completion for the current shell, to copy it or customize │
│                               the installation.                                              │
│ --help                        Show this message and exit.                                    │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────────────────╮
│ version   Print CLI version and exit                                                         │
│ users     Manage users                                                                       │
│ pets                                                                                         │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯

(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py --show-sub-commands
╭─ Sub-Commands ───────────────────────────────────────────────────────────────────────────────╮
│ version*                       Print CLI version and exit                                    │
│ users                          Manage users                                                  │
│   add*                         Short help                                                    │
│   delete*                                                                                    │
│   update                       Update user info                                              │
│     name*                      change name                                                   │
│     address*                   change address                                                │
│ pets                                                                                         │
│   add*                         add pet                                                       │
│   list*                                                                                      │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯
* denotes commands without subcommands
(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py version
My CLI Version 1.0
(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py version --help
                                                                                                
 Usage: subcommand_tree.py version [OPTIONS]                                                    
                                                                                                
 Print CLI version and exit                                                                     
                                                                                                
╭─ Options ────────────────────────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                                                  │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯

(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py users --help
                                                                                                
 Usage: subcommand_tree.py users [OPTIONS] COMMAND [ARGS]...                                    
                                                                                                
 Manage users                                                                                   
                                                                                                
╭─ Options ────────────────────────────────────────────────────────────────────────────────────╮
│ --show-sub-commands          Show sub-command tree                                           │
│ --help                       Show this message and exit.                                     │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────────────────╮
│ add      Short help                                                                          │
│ delete                                                                                       │
│ update   Update user info                                                                    │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯

(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py users --show-sub-commands
╭─ Sub-Commands ───────────────────────────────────────────────────────────────────────────────╮
│ add*                                  Short help                                             │
│ delete*                                                                                      │
│ update                                Update user info                                       │
│   name*                               change name                                            │
│   address*                            change address                                         │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯
* denotes commands without subcommands
(.venv) ~/typer-rickwporter/tests/assets> ./subcommand_tree.py users update --show-sub-commands
╭─ Sub-Commands ───────────────────────────────────────────────────────────────────────────────╮
│ name*                               change name                                              │
│ address*                            change address                                           │
╰──────────────────────────────────────────────────────────────────────────────────────────────╯
* denotes commands without subcommands
(.venv) ~/typer-rickwporter/tests/assets> 

@rickwporter rickwporter marked this pull request as ready for review January 8, 2025 02:10
@svlandeg svlandeg added the feature New feature, enhancement or request label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature, enhancement or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants