comptplus

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 10 Imported by: 3 modules, 1 package

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.

README

Cobra-Prompt Plus (Comptplus)

Comptplus Banner

Comptplus is a fork of Cobra Prompt with added features, intended to enhance the original implementation by making it more versatile.

Projects using this fork

Features unique to this fork

Flag Value Completions and Persistence
  • Flag Completions: Added completions for flag values, for easier and more intuitive command usage.
    • Default cache duration for refreshing flag value completions is set to 500ms, to avoid lag in user interaction.
    • Support for flag descriptions by splitting on \t.
Customizable Flag Reset Behaviors
  • Custom Flag Reset Behaviors: Ability to set custom behaviors for resetting flag values.
    • The default flag reset behaviour has also been changed to reset flags to their default values after each command execution (rather than on each typed character).
    • A bug in the original repo caused slice/array flags to be reset incorrectly, by appending the default values to the previous execution's values. This has been fixed in this fork.
Pre and Post Execution Hooks
  • Execution Hooks: Ability to use HookBefore and HookAfter for performing actions before and after command execution.
elk-language/go-prompt
  • Bump go-prompt: Switched to an actively maintained fork of go-prompt with more features and bug fixes: https://github.com/elk-language/go-prompt. Sadly this involves some breaking changes.
    • This fixes a bug which would disallow usage of CTRL + C in after usage of go-prompt.
Proper Argument Passing via SetArgs
  • SetArgs instead of os.Args: Command execution now uses cobra.Command.SetArgs() instead of manipulating the global os.Args. This avoids side effects with global state and works correctly when comptplus is used as a sub-command of a larger CLI (e.g. apps that call root.SetArgs as part of their bootstrap).
Shell-Aware Argument Parsing
  • Quoted argument support: Default argument parser now uses go-shellquote instead of strings.Fields. This means arguments with spaces work correctly when quoted, e.g. get -n "John Oliver" food apple is parsed as ["get", "-n", "John Oliver", "food", "apple"] rather than splitting on every space. Falls back to strings.Fields on parse errors (e.g. unclosed quotes). Custom parsers via InArgsParser are still supported.
DynamicSuggestionsFunc with Command Context
  • Command-aware dynamic suggestions: DynamicSuggestionsFunc now receives the resolved *cobra.Command as its first parameter, allowing consumers to use cmd.ValidArgsFunction() for generic auto-completion logic.
Graceful Error Handling
  • No more os.Exit on error: handleUserError no longer calls os.Exit(1) when no OnErrorFunc is set. Instead it prints the error and returns control to the prompt loop, so a single bad command doesn't kill the entire shell session.

Original README below


Cobra-Prompt

Cobra-prompt makes every Cobra command and flag available for go-prompt.

Features

  • Traverse cobra command tree. Every command and flag will be available.
  • Persist flag values.
  • Add custom functions for dynamic suggestions.

Getting started

Get the module:

go get github.com/stromland/cobra-prompt

Explore the example

cd _example
go build -o cobra-prompt
./cobra-prompt

Documentation

Index

Constants

View Source
const CacheIntervalFlag = "cache-interval"
View Source
const DynamicSuggestionsAnnotation = "cobra-prompt-dynamic-suggestions"

DynamicSuggestionsAnnotation for dynamic suggestions.

Variables

This section is empty.

Functions

This section is empty.

Types

type CobraPrompt

type CobraPrompt struct {
	// RootCmd is the start point, all its sub commands and flags will be available as suggestions
	RootCmd *cobra.Command

	// GoPromptOptions is for customize go-prompt
	// see https://github.com/c-bata/go-prompt/blob/master/option.go
	GoPromptOptions []prompt.Option

	// DynamicSuggestionsFunc will be executed if a command has CallbackAnnotation as an annotation. If it's included
	// the value will be provided to the DynamicSuggestionsFunc function.
	// The *cobra.Command parameter is the resolved command, allowing use of ValidArgsFunction for completions.
	DynamicSuggestionsFunc func(cmd *cobra.Command, annotationValue string, document *prompt.Document) []prompt.Suggest

	// PersistFlagValues will persist flags. For example have verbose turned on every command.
	PersistFlagValues bool

	// CustomFlagResetBehaviour allows you to specify custom behaviour which will be run after each command, if PersistFlagValues is false
	CustomFlagResetBehaviour func(flag *pflag.Flag)

	// ShowHelpCommandAndFlags will make help command and flag for every command available.
	ShowHelpCommandAndFlags bool

	// DisableCompletionCommand will disable the default completion command for cobra
	DisableCompletionCommand bool

	// ShowHiddenCommands makes hidden commands available
	ShowHiddenCommands bool

	// ShowHiddenFlags makes hidden flags available
	ShowHiddenFlags bool

	// AddDefaultExitCommand adds a command for exiting prompt loop
	AddDefaultExitCommand bool

	// OnErrorFunc handle error for command.Execute, if not set print error and exit
	OnErrorFunc func(err error)

	// HookAfter is a hook that will be executed every time after a command has been executed
	HookAfter func(cmd *cobra.Command, input string) error

	// HookBefore is a hook that will be executed every time before a command is executed
	HookBefore func(cmd *cobra.Command, input string) error

	// InArgsParser adds a custom parser for the command line arguments (default: strings.Fields)
	InArgsParser func(args string) []string

	// SuggestionFilter will be uses when filtering suggestions as typing
	SuggestionFilter func(suggestions []prompt.Suggest, document *prompt.Document) []prompt.Suggest

	// AsyncFlagValueSuggestions enables non-blocking flag value completions.
	// When enabled, flag value suggestions are fetched in a background goroutine
	// instead of blocking the prompt. The first completion for a flag may return
	// empty results; subsequent keystrokes will return the fetched data.
	AsyncFlagValueSuggestions bool
	// contains filtered or unexported fields
}

CobraPrompt given a Cobra command it will make every flag and sub commands available as suggestions. Command.Short will be used as description for the suggestion.

func (*CobraPrompt) Run

func (co *CobraPrompt) Run()

Run will automatically generate suggestions for all cobra commands and flags defined by RootCmd and execute the selected commands. Run will also reset all given flags by default, see PersistFlagValues

func (*CobraPrompt) RunContext

func (co *CobraPrompt) RunContext(ctx context.Context)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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