cli

package
v0.43.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CWD relative_to = iota
	CONFIG
)

Variables

This section is empty.

Functions

func CompleteExecutableFirstArg

func CompleteExecutableFirstArg(completions *Completions, word string, arg_num int)

func CompleteExecutablesInPath

func CompleteExecutablesInPath(prefix string, paths ...string) []string

func CompleteFiles

func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error

func CompletionForWrapper

func CompletionForWrapper(wrapped_cmd string) func(completions *Completions, word string, arg_num int)

func GenerateCompletions

func GenerateCompletions(args []string) error

func GetOptionValue

func GetOptionValue[T any](self *Command, name string) (ans T, err error)

func NormalizeOptionName

func NormalizeOptionName(name string) string

func RegisterExeForCompletion

func RegisterExeForCompletion(x func(root *Command))

func ShowError

func ShowError(err error)

func ShowHelpInPager

func ShowHelpInPager(text string)

func WcswidthKittenEntryPoint

func WcswidthKittenEntryPoint(root *Command)

Types

type Alias

type Alias struct {
	NameWithoutHyphens string
	IsShort            bool
	IsUnset            bool
}

func (*Alias) String

func (self *Alias) String() string

type Command

type Command struct {
	Name, Group                       string
	Usage, ShortDescription, HelpText string
	Hidden                            bool

	// Number of non-option arguments after which to stop parsing options. 0 means no options after the first non-option arg.
	AllowOptionsAfterArgs int
	// If true does not fail if the first non-option arg is not a sub-command
	SubCommandIsOptional bool
	// If true subcommands are ignored unless they are the first non-option argument
	SubCommandMustBeFirst bool
	// The entry point for this command
	Run RunFunc
	// The completer for args
	ArgCompleter CompletionFunc
	// Stop completion processing at this arg num
	StopCompletingAtArg int
	// Consider all args as non-options args when parsing for completion
	OnlyArgsAllowed bool
	// Pass through all args, useful for wrapper commands
	IgnoreAllArgs bool
	// Specialised arg parsing
	ParseArgsForCompletion func(cmd *Command, args []string, completions *Completions)
	// Callback that is called on error
	CallbackOnError func(cmd *Command, err error, during_parsing bool, exit_code int) (final_exit_code int)

	SubCommandGroups []*CommandGroup
	OptionGroups     []*OptionGroup
	Parent           *Command

	Args []string

	IndexOfFirstArg int
	// contains filtered or unexported fields
}

func NewRootCommand

func NewRootCommand() *Command

func (*Command) Add

func (self *Command) Add(s OptionSpec) *Option

func (*Command) AddClone

func (self *Command) AddClone(group string, src *Command) *Command

func (*Command) AddOptionFromString

func (self *Command) AddOptionFromString(items ...string) *Option

func (*Command) AddOptionGroup

func (self *Command) AddOptionGroup(title string) *OptionGroup

func (*Command) AddOptionToGroupFromString

func (self *Command) AddOptionToGroupFromString(group string, items ...string) *Option

func (*Command) AddSubCommand

func (self *Command) AddSubCommand(ans *Command) *Command

func (*Command) AddSubCommandGroup

func (self *Command) AddSubCommandGroup(title string) *CommandGroup

func (*Command) AddToGroup

func (self *Command) AddToGroup(group string, s OptionSpec) *Option

func (*Command) AllOptions

func (self *Command) AllOptions() []*Option

func (*Command) Clone

func (self *Command) Clone(parent *Command) *Command

func (*Command) CommandStringForUsage

func (self *Command) CommandStringForUsage() string

func (*Command) ExecArgs

func (self *Command) ExecArgs(args []string) (exit_code int)

func (*Command) FindOption

func (self *Command) FindOption(name_with_hyphens string) *Option

func (*Command) FindOptions

func (self *Command) FindOptions(name_with_hyphens string) []*Option

func (*Command) FindSubCommand

func (self *Command) FindSubCommand(name string) *Command

func (*Command) FindSubCommands

func (self *Command) FindSubCommands(prefix string) []*Command

func (*Command) FormatSubCommands

func (self *Command) FormatSubCommands(output io.Writer, formatter *markup.Context, screen_width int)

func (*Command) GenerateManPages

func (self *Command) GenerateManPages(level int, recurse bool) (err error)

func (*Command) GetCompletions

func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions

func (*Command) GetOptionValues

func (self *Command) GetOptionValues(pointer_to_options_struct any) error

func (*Command) GetVisibleOptions

func (self *Command) GetVisibleOptions() ([]string, map[string][]*Option)

func (*Command) HasSubCommands

func (self *Command) HasSubCommands() bool

func (*Command) HasVisibleSubCommands

func (self *Command) HasVisibleSubCommands() bool

func (*Command) OptionsSeenOnCommandLine added in v0.42.2

func (self *Command) OptionsSeenOnCommandLine() map[string]bool

func (*Command) ParseArgs

func (self *Command) ParseArgs(args []string) (*Command, error)

func (*Command) ResetAfterParseArgs

func (self *Command) ResetAfterParseArgs()

func (*Command) Root

func (self *Command) Root() *Command

func (*Command) ShowHelp

func (self *Command) ShowHelp()

func (*Command) ShowHelpWithCommandString

func (self *Command) ShowHelpWithCommandString(cs string)

func (*Command) ShowVersion

func (self *Command) ShowVersion()

func (*Command) SuggestionsForCommand

func (self *Command) SuggestionsForCommand(name string, max_distance int) []string

func (*Command) SuggestionsForOption

func (self *Command) SuggestionsForOption(name_with_hyphens string, max_distance int) []string

func (*Command) Validate

func (self *Command) Validate() error

func (*Command) VisitAllOptions

func (self *Command) VisitAllOptions(callback func(*Option) error) error

type CommandGroup

type CommandGroup struct {
	SubCommands []*Command
	Title       string
}

func (*CommandGroup) AddSubCommand

func (self *CommandGroup) AddSubCommand(parent *Command, name string) *Command

func (*CommandGroup) Clone

func (self *CommandGroup) Clone(parent *Command) *CommandGroup

func (*CommandGroup) FindSubCommand

func (self *CommandGroup) FindSubCommand(name string) *Command

func (*CommandGroup) FindSubCommands

func (self *CommandGroup) FindSubCommands(prefix string, matches []*Command) []*Command

func (*CommandGroup) HasVisibleSubCommands

func (self *CommandGroup) HasVisibleSubCommands() bool

type CompletionFunc

type CompletionFunc = func(completions *Completions, word string, arg_num int)

func ChainCompleters

func ChainCompleters(completers ...CompletionFunc) CompletionFunc

func DirectoryCompleter

func DirectoryCompleter(title string, relative_to relative_to) CompletionFunc

func FnmatchCompleter

func FnmatchCompleter(title string, relative_to relative_to, patterns ...string) CompletionFunc

func MimepatCompleter

func MimepatCompleter(title string, relative_to relative_to, patterns ...string) CompletionFunc

func NamesCompleter

func NamesCompleter(title string, names ...string) CompletionFunc

type Completions

type Completions struct {
	Groups   []*MatchGroup `json:"groups,omitempty"`
	Delegate Delegate      `json:"delegate,omitempty"`

	CurrentCmd             *Command `json:"-"`
	AllWords               []string `json:"-"` // all words passed to parse_args()
	CurrentWordIdx         int      `json:"-"` // index of current word in all_words
	CurrentWordIdxInParent int      `json:"-"` // index of current word in parents command line 1 for first word after parent
	// contains filtered or unexported fields
}

func NewCompletions

func NewCompletions() *Completions

func (*Completions) AddMatchGroup

func (self *Completions) AddMatchGroup(title string) *MatchGroup

func (*Completions) AddPrefixToAllMatches

func (self *Completions) AddPrefixToAllMatches(prefix string)

func (*Completions) MergeMatchGroup

func (self *Completions) MergeMatchGroup(mg *MatchGroup)

type Context

type Context struct {
	SeenCommands []*Command
}

type Delegate

type Delegate struct {
	NumToRemove int    `json:"num_to_remove,omitempty"`
	Command     string `json:"command,omitempty"`
}

type FileEntry

type FileEntry struct {
	Name, CompletionCandidate, Abspath string
	Mode                               os.FileMode
	IsDir, IsSymlink, IsEmptyDir       bool
}

type Match

type Match struct {
	Word        string `json:"word,omitempty"`
	Description string `json:"description,omitempty"`
}

func (*Match) FormatForCompletionList

func (self *Match) FormatForCompletionList(max_word_len int, f *markup.Context, screen_width int) string

type MatchGroup

type MatchGroup struct {
	Title           string   `json:"title,omitempty"`
	NoTrailingSpace bool     `json:"no_trailing_space,omitempty"`
	IsFiles         bool     `json:"is_files,omitempty"`
	Matches         []*Match `json:"matches,omitempty"`
}

func (*MatchGroup) AddMatch

func (self *MatchGroup) AddMatch(word string, description ...string) *Match

func (*MatchGroup) AddPrefixToAllMatches

func (self *MatchGroup) AddPrefixToAllMatches(prefix string)

type Option

type Option struct {
	Name       string
	Aliases    []Alias
	Choices    []string
	Default    string
	OptionType OptionType
	Hidden     bool
	Depth      int
	Help       string
	IsList     bool
	Parent     *Command
	Completer  CompletionFunc
	// contains filtered or unexported fields
}

func OptionFromString

func OptionFromString(entries ...string) (*Option, error)

Create an Option from a string. Syntax is:

--option-name, --option-alias, -s
type: string
dest: destination
choices: choice1, choice2, choice 3
depth: 0
default: something
Help text on multiple lines. Indented lines are preserved as indented blocks. Blank lines
are preserved as blank lines. #placeholder_for_formatting# is replaced by the empty string.
.. code:: blocks are handled specially. Lines in them starting with "$ " have the $ colored
to indicate a prompt.

Available types are: string, str, list, int, float, count, bool-set, bool-reset, choices The default dest is the first --option-name which must be a long option. The destination is automatically CamelCased from snake_case. If choices are specified type is set to choices automatically. If depth is negative option is added to all subcommands. If depth is positive option is added to sub-commands upto the specified depth. Set the help text to "!" to have an option hidden.

func OptionsFromStruct

func OptionsFromStruct(pointer_to_options_struct any) ([]*Option, error)

func (*Option) FormatOption

func (self *Option) FormatOption(output io.Writer, formatter *markup.Context, screen_width int)

func (*Option) FormatOptionForMan

func (self *Option) FormatOptionForMan(output io.Writer)

func (*Option) HasAlias

func (self *Option) HasAlias(name_without_hyphens string, is_short bool) bool

func (*Option) MatchingAlias

func (self *Option) MatchingAlias(prefix_without_hyphens string, is_short bool) string

type OptionGroup

type OptionGroup struct {
	Options []*Option
	Title   string
}

func (*OptionGroup) AddOption

func (self *OptionGroup) AddOption(parent *Command, spec OptionSpec) (*Option, error)

func (*OptionGroup) AddOptionFromString

func (self *OptionGroup) AddOptionFromString(parent *Command, items ...string) (*Option, error)

func (*OptionGroup) Clone

func (self *OptionGroup) Clone(parent *Command) *OptionGroup

func (*OptionGroup) FindOption

func (self *OptionGroup) FindOption(name_with_hyphens string) *Option

func (*OptionGroup) FindOptions

func (self *OptionGroup) FindOptions(prefix_with_hyphens string) []*Option

type OptionSpec

type OptionSpec struct {
	Name      string
	Type      string
	Dest      string
	Choices   string
	Depth     int
	Default   string
	Help      string
	Completer CompletionFunc
}

type OptionType

type OptionType int
const (
	StringOption OptionType = iota
	IntegerOption
	FloatOption
	BoolOption
	CountOption
)

type ParseError

type ParseError struct {
	Option  *Option
	Message string
}

func (*ParseError) Error

func (self *ParseError) Error() string

type RunFunc

type RunFunc = func(cmd *Command, args []string) (int, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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