fastapi / typer

Typer, build great CLIs. Easy to code. Based on Python type hints.
https://typer.tiangolo.com/
MIT License
15.78k stars 672 forks source link

How to display commands grouped by category when running using `--help` argument #514

Open brupelo opened 1 year ago

brupelo commented 1 year ago

First Check

Commit to Help

Example Code

import typer

# Troubleshooting and Debugging Commands:
def describe(foo: str):
    print(f"{foo}")

def logs(foo: str):
    print(f"{foo}")

def attach(foo: str):
    print(f"{foo}")

def _exec(foo: str):
    print(f"{foo}")

def port_forward(foo: str):
    print(f"{foo}")

def proxy(foo: str):
    print(f"{foo}")

def cp(foo: str):
    print(f"{foo}")

def auth(foo: str):
    print(f"{foo}")

def debug(foo: str):
    print(f"{foo}")

# Advanced Commands:
def diff(foo: str):
    print(f"{foo}")

def _apply(foo: str):
    print(f"{foo}")

def patch(foo: str):
    print(f"{foo}")

def replace(foo: str):
    print(f"{foo}")

def wait(foo: str):
    print(f"{foo}")

def kustomize(foo: str):
    print(f"{foo}")

# Settings Commands:
def label(foo: str):
    print(f"{foo}")

def annotate(foo: str):
    print(f"{foo}")

def completion(foo: str):
    print(f"{foo}")

def main():
    # console = Console()
    app = typer.Typer()

    # Troubleshooting and Debugging Commands
    app.command(short_help="Show details of a specific resource or group of resources")(
        describe
    )
    app.command(short_help="Print the logs for a container in a pod")(logs)
    app.command(short_help="Attach to a running container")(attach)
    app.command("exec", short_help="Execute a command in a container")(_exec)
    app.command(short_help="Forward one or more local ports to a pod")(port_forward)
    app.command(short_help="Run a proxy to the Kubernetes API server")(proxy)
    app.command(short_help="Copy files and directories to and from containers")(cp)
    app.command(short_help="Inspect authorization")(auth)
    app.command(
        short_help="Create debugging sessions for troubleshooting workloads and nodes"
    )(debug)

    # Advanced
    app.command(short_help="Diff the live version against a would-be applied version")(
        diff
    )
    app.command("apply", short_help="Apply a configuration to a resource by file name or stdin")(
        _apply
    )
    app.command(short_help="Update fields of a resource")(patch)
    app.command(short_help="Replace a resource by file name or stdin")(replace)
    app.command(
        short_help="Experimental: Wait for a specific condition on one or many resources"
    )(wait)
    app.command(short_help="Build a kustomization target from a directory or URL")(
        kustomize
    )

    # Settings
    app.command(short_help="Update the labels on a resource")(label)
    app.command(short_help="Update the annotations on a resource")(annotate)
    app.command(
        short_help="Output shell completion code for the specified shell (bash, zsh, fish, or powershell"
    )(completion)
    app()

if __name__ == "__main__":
    main()

Description

Hi, first of all let me tell you I've only been using typer for 20min and I love it, this is a really modern, powerful and very intuitive python package, so congrats about that :)

I've got a question:

Pasting the kubectl output:

$ kubectl --help
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new
Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and
label selector

Deploy Commands:
  rollout         Manage the rollout of a resource
  scale           Set a new size for a deployment, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:
  certificate     Modify certificate resources.
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe        Show details of a specific resource or group of resources
  logs            Print the logs for a container in a pod
  attach          Attach to a running container
  exec            Execute a command in a container
  port-forward    Forward one or more local ports to a pod
  proxy           Run a proxy to the Kubernetes API server
  cp              Copy files and directories to and from containers
  auth            Inspect authorization
  debug           Create debugging sessions for troubleshooting workloads and nodes

Advanced Commands:
  diff            Diff the live version against a would-be applied version
  apply           Apply a configuration to a resource by file name or stdin
  patch           Update fields of a resource
  replace         Replace a resource by file name or stdin
  wait            Experimental: Wait for a specific condition on one or many resources
  kustomize       Build a kustomization target from a directory or URL.

Settings Commands:
  label           Update the labels on a resource
  annotate        Update the annotations on a resource
  completion      Output shell completion code for the specified shell (bash, zsh, fish, or
powershell)

Other Commands:
  alpha           Commands for features in alpha
  api-resources   Print the supported API resources on the server
  api-versions    Print the supported API versions on the server, in the form of "group/version"
  config          Modify kubeconfig files
  plugin          Provides utilities for interacting with plugins
  version         Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).

Operating System

Windows

Operating System Details

Windows 10 pro

Typer Version

0.7.0

Python Version

Python 3.10.1

Additional Context

No response

oefe commented 1 year ago

If you have enabled rich support in Typer, you can do this by adding a rich rich_help_panel parameter to your app.command calls. See the example in the link

NikosAlexandris commented 1 year ago

I think this "issue" can be closed, the answer is straightforward (as per the link to the manual shared above).