comptplus

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2023 License: MIT Imports: 7 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, facilitating easier and more intuitive command usage.
    • Default cache duration for responses is set to 500ms, reducing latency 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, improving consistency and predictability of the shell environment.
    • 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: Added HookBefore and HookAfter for performing actions before and after command execution.
Enhanced Flag Persistence
  • Persist Flag Values: Refined behavior of PersistFlagValues to offer more seamless flag value persistence across shell sessions.

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.
	DynamicSuggestionsFunc func(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 ran after each command, if PersistFlagValues is false
	CustomFlagResetBehaviour func(pflag *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(input string)

	// HookBefore is a hook that will be executed every time  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