mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 03:03:32 +01:00
feat: migrate from docopt to cobra for CLI argument parsing
Replace docopt-go with spf13/cobra, giving cheat a built-in `--completion` flag that dynamically generates shell completions for bash, zsh, fish, and powershell. This replaces the manually-maintained static completion scripts and resolves the root cause of multiple completion issues (#768, #705, #633, #632, #476). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
43
internal/completions/cheatsheets.go
Normal file
43
internal/completions/cheatsheets.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// Package completions provides dynamic shell completion functions and
|
||||
// completion script generation for the cheat CLI.
|
||||
package completions
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cheat/cheat/internal/sheets"
|
||||
)
|
||||
|
||||
// Cheatsheets provides completion for cheatsheet names.
|
||||
func Cheatsheets(
|
||||
_ *cobra.Command,
|
||||
args []string,
|
||||
_ string,
|
||||
) ([]string, cobra.ShellCompDirective) {
|
||||
|
||||
if len(args) > 0 {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
conf, err := loadConfig()
|
||||
if err != nil {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||
if err != nil {
|
||||
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
|
||||
consolidated := sheets.Consolidate(cheatsheets)
|
||||
|
||||
names := make([]string, 0, len(consolidated))
|
||||
for name := range consolidated {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
|
||||
return names, cobra.ShellCompDirectiveNoFileComp
|
||||
}
|
||||
Reference in New Issue
Block a user