commands

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package commands handles staghorn command parsing, registry, and rendering.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToClaude

func ConvertToClaude(cmd *Command) string

ConvertToClaude converts a staghorn command to Claude Code command format. It adds a usage hint so Claude (the LLM) understands the {{varname}} syntax.

func NewCommandTemplate

func NewCommandTemplate(name, description string) string

NewCommandTemplate returns a template for creating a new command.

func ParseArgs

func ParseArgs(rawArgs []string) (map[string]string, error)

ParseArgs parses command-line style arguments into a map. Supports formats:

  • --name value
  • --name=value
  • name=value

Types

type Arg

type Arg struct {
	Name        string   `yaml:"name"`
	Description string   `yaml:"description"`
	Default     string   `yaml:"default"`
	Options     []string `yaml:"options,omitempty"` // Valid options if constrained
	Required    bool     `yaml:"required"`
}

Arg defines a command argument.

type ClaudeCommand

type ClaudeCommand struct {
	Name        string `yaml:"name"`
	Description string `yaml:"description,omitempty"`
}

ClaudeCommand represents a Claude Code custom command.

type Command

type Command struct {
	Frontmatter
	Body     string // Markdown content after frontmatter
	Source   Source // Where this command came from
	FilePath string // Path to the command file
}

Command represents a staghorn command.

func LoadFromDirectory

func LoadFromDirectory(dir string, source Source) ([]*Command, error)

LoadFromDirectory loads all commands from a directory.

func Parse

func Parse(content string, source Source, filePath string) (*Command, error)

Parse parses a command from markdown content.

func ParseFile

func ParseFile(path string, source Source) (*Command, error)

ParseFile parses a command from a file.

func (*Command) ExtractVariables

func (c *Command) ExtractVariables() []string

ExtractVariables returns all variable names used in the command body.

func (*Command) FormatArgsHelp

func (c *Command) FormatArgsHelp() string

FormatArgsHelp formats the arguments help text for a command.

func (*Command) GetArg

func (c *Command) GetArg(name string) *Arg

GetArg returns an argument by name.

func (*Command) GetArgWithDefault

func (c *Command) GetArgWithDefault(args map[string]string, name string) string

GetArgWithDefault returns the argument value or its default.

func (*Command) HasArg

func (c *Command) HasArg(name string) bool

HasArg checks if the command has a specific argument.

func (*Command) Render

func (c *Command) Render(args map[string]string) (string, error)

Render renders a command's body with the provided arguments. Uses a simple {{varname}} syntax for user-friendliness.

func (*Command) RenderWithValidation

func (c *Command) RenderWithValidation(args map[string]string) (string, []string, error)

RenderWithValidation renders and also returns warnings for undefined variables.

func (*Command) ValidateArgs

func (c *Command) ValidateArgs(args map[string]string) error

ValidateArgs validates provided arguments against the command's definition.

type Frontmatter

type Frontmatter struct {
	Name        string   `yaml:"name"`
	Description string   `yaml:"description"`
	Tags        []string `yaml:"tags,omitempty"`
	Args        []Arg    `yaml:"args,omitempty"`
}

Frontmatter contains the YAML frontmatter of a command.

func ReadFrontmatterOnly

func ReadFrontmatterOnly(path string) (*Frontmatter, error)

ReadFrontmatterOnly reads just the frontmatter without loading the full body. Useful for listing commands without loading all content into memory.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages commands from multiple sources with precedence handling. Precedence (highest to lowest): project > personal > team

func LoadRegistry

func LoadRegistry(teamDir, personalDir, projectDir string) (*Registry, error)

LoadRegistry creates a registry by loading commands from all sources.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty command registry.

func (*Registry) Add

func (r *Registry) Add(cmd *Command)

Add adds a command to the registry. If a command with the same name exists from a lower precedence source, it's overridden.

func (*Registry) AddAll

func (r *Registry) AddAll(cmds []*Command)

AddAll adds multiple commands to the registry.

func (*Registry) All

func (r *Registry) All() []*Command

All returns all unique commands (highest precedence version of each).

func (*Registry) BySource

func (r *Registry) BySource(source Source) []*Command

BySource returns all commands from a specific source.

func (*Registry) ByTag

func (r *Registry) ByTag(tag string) []*Command

ByTag returns all commands that have a specific tag.

func (*Registry) Count

func (r *Registry) Count() int

Count returns the total number of unique commands.

func (*Registry) CountBySource

func (r *Registry) CountBySource() map[Source]int

CountBySource returns counts per source.

func (*Registry) Get

func (r *Registry) Get(name string) *Command

Get returns a command by name (highest precedence version).

func (*Registry) GetAllVersions

func (r *Registry) GetAllVersions(name string) []*Command

GetAllVersions returns all versions of a command across sources.

func (*Registry) IsOverridden

func (r *Registry) IsOverridden(name string, source Source) bool

IsOverridden checks if a command from a lower source is overridden.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns all command names.

type Source

type Source string

Source indicates where a command came from.

const (
	SourceTeam     Source = "team"
	SourcePersonal Source = "personal"
	SourceProject  Source = "project"
	SourceStarter  Source = "starter"
)

func (Source) Label

func (s Source) Label() string

SourceLabel returns a human-readable label for the source.

Jump to

Keyboard shortcuts

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