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

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

Open
7 tasks done
brupelo opened this issue Dec 10, 2022 · 2 comments
Open
7 tasks done
Labels
question Question or problem

Comments

@brupelo
Copy link

brupelo commented Dec 10, 2022

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the Typer documentation, with the integrated search.
  • I already searched in Google "How to X in Typer" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to Typer but to Click.

Commit to Help

  • I commit to help with one of those options 👆

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:

  • I'm trying to figure out how to achieve a similar behaviour to kubectl --help cli in a way that if run python my_typer_cli.py --help I'd be able to see all my commands grouped by categories. How can I achieve a similar behaviour using typer?

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

@brupelo brupelo added the question Question or problem label Dec 10, 2022
@oefe
Copy link

oefe commented Dec 18, 2022

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
Projects
None yet
Development

No branches or pull requests

3 participants