cobraprompt

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: MIT Imports: 11 Imported by: 0

README

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/tengteng/cobra-prompt

Explore the example

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

Documentation

Index

Constants

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 avaiailable 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/tengteng/go-prompt/blob/master/option.go
	GoPromptOptions []prompt.Option

	// DynamicSuggestionsFunc will be executed if an 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)

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

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)

RunContext same as Run but with context

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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