comptplus

package module
v1.0.0-rc.8 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: MIT Imports: 7 Imported by: 0

README

Cobra-Prompt Plus (Comptplus)

Comptplus is a fork of Cobra Prompt with added features:

  • Automatic Flag Value Completion: Extracts possible flag values and recommends them to the user.
    • Works with descriptions registered via RegisterFlagCompletionFunc
    • This is only possible with Cobra 1.8.0 (specifically PRs spf13/cobra#1943 and spf13/cobra#2063)
    • Flag descriptions are also added correctly (in Cobra \t is used to split the flag values vs. flag descriptions).
  • Refactor to use some stateless funcs

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.

View Source
const PersistFlagValuesFlag = "persist-flag-values"

PersistFlagValuesFlag the flag that will be available when PersistFlagValues is true

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.
	DynamicSuggestionsFunc func(annotationValue string, document *prompt.Document) []prompt.Suggest

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

	// 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 after a command has been executed
	HookAfter func(input string)

	// HookBefore is a hook that will be executed before a command has been executed
	HookBefore func(input string)

	// 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
	// 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