cmdtree

package
v0.0.2-testing Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cmdtree describes the command tree as data. The same description backs `search`, the command surface snapshot and the description lint, so generated and hand-written commands are treated alike.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitDefaults

func InitDefaults(root *cobra.Command)

InitDefaults adds what cobra otherwise adds only when a command runs: the help and completion commands and the --help and --version flags of the root. Call it before Walk, so that a tree built in a test and the tree of a running command are walked alike.

func Lint

func Lint(cmds []Command) []error

Lint checks the descriptions that `search`, `--help` and the agent docs are built from. A command whose summary only repeats its name ("dns", "Operations for records", "Manage accounts") cannot be found by describing a task.

cobra's completion commands and the flags cobra adds are skipped: cobra writes their text, which changes with cobra and is not ours to fix.

Types

type Command

type Command struct {
	Path  string `json:"path"`
	Usage string `json:"usage"`
	Short string `json:"short"`
	// Long is searched but left out of the surface snapshot: it is prose and
	// may embed machine-specific values such as the config path.
	Long    string   `json:"-"`
	Aliases []string `json:"aliases,omitempty"`
	// Runnable is false for groups that only hold subcommands, and for topics.
	Runnable bool `json:"runnable"`
	// Topic marks a help topic: text read with `<app> help <topic>`.
	Topic bool   `json:"topic,omitempty"`
	Flags []Flag `json:"flags,omitempty"`
	// contains filtered or unexported fields
}

Command is one visible command.

func Walk

func Walk(root *cobra.Command) []Command

Walk lists the visible commands under root in tree order, root included. Hidden commands, their subcommands and cobra's help command are skipped. The flags listed on the root include the global flags.

type Flag

type Flag struct {
	Name      string `json:"name"`
	Shorthand string `json:"shorthand,omitempty"`
	Type      string `json:"type"`
	Default   string `json:"default,omitempty"`
	Usage     string `json:"usage"`
	// contains filtered or unexported fields
}

Flag is a local flag of a command. Global flags are listed on the root only.

type Match

type Match struct {
	Command string `json:"command"`
	Summary string `json:"summary"`
}

Match is one search result.

func Search(cmds []Command, query string, limit int) []Match

Search ranks the runnable commands and help topics against a task description and returns at most limit matches, best first. It runs offline over the command tree. A topic is returned as the command that reads it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL