Documentation ¶
Overview ¶
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Copyright © 2022 Roberto Hidalgo <chinampa@un.rob.mx> SPDX-License-Identifier: Apache-2.0
Index ¶
- Variables
- func RegisterValueSource(key string, completion CompletionFunc)
- type Action
- type Argument
- func (arg *Argument) EnvName() string
- func (arg *Argument) IsKnown() bool
- func (arg *Argument) Resolve(current string) (values []string, flag cobra.ShellCompDirective, err error)
- func (arg *Argument) SetValue(value []string)
- func (arg *Argument) ToDesc() string
- func (arg *Argument) ToString() string
- func (arg *Argument) ToValue() any
- func (arg *Argument) Validate() error
- func (arg *Argument) Validates() bool
- type Arguments
- func (args *Arguments) AllKnown() map[string]any
- func (args *Arguments) AllKnownStr() map[string]string
- func (args *Arguments) AreValid() error
- func (args *Arguments) CompletionFunction(cc *cobra.Command, provided []string, toComplete string) ([]string, cobra.ShellCompDirective)
- func (args *Arguments) Parse(supplied []string) error
- type AutocompleteTemplate
- type Command
- func (cmd *Command) AdditionalHelp(printLinks bool) *string
- func (cmd *Command) FlagSet() *pflag.FlagSet
- func (cmd *Command) FullName() string
- func (cmd *Command) HasAdditionalHelp() bool
- func (cmd *Command) HelpRenderer(globalOptions Options) func(cc *cobra.Command, args []string)
- func (cmd *Command) IsRoot() bool
- func (cmd *Command) Name() string
- func (cmd *Command) ParseInput(cc *cobra.Command, args []string) error
- func (cmd *Command) ResolveTemplate(templateString string, currentValue string) (string, error)
- func (cmd *Command) Run(cc *cobra.Command, args []string) error
- func (cmd *Command) SetBindings() *Command
- func (cmd *Command) SetCobra(cc *cobra.Command)
- func (cmd *Command) ShowHelp(globalOptions Options, _ []string) ([]byte, error)
- func (cmd *Command) Validate() (report map[string]int)
- type CompletionFunc
- type HelpFunc
- type Option
- func (opt *Option) CompletionFunction(cmd *cobra.Command, args []string, toComplete string) (values []string, flag cobra.ShellCompDirective)
- func (opt *Option) IsKnown() bool
- func (opt *Option) Resolve(currentValue string) (values []string, flag cobra.ShellCompDirective, err error)
- func (opt *Option) ToString() string
- func (opt *Option) ToValue() any
- func (opt *Option) Validate(name string) error
- func (opt *Option) Validates() bool
- type Options
- type SourceCommand
- type ValueSource
- type ValueType
Constants ¶
This section is empty.
Variables ¶
var Root = &Command{ Summary: "Replace me with chinampa.Configure", Description: "", Path: []string{runtime.Executable}, Options: Options{ _c.HelpCommandName: &Option{ ShortName: "h", Type: "bool", Description: "Display help for any command", }, "color": &Option{ Type: "bool", Description: "Always print colors to stderr", Default: runtime.ColorEnabled(), }, "no-color": &Option{ Type: "bool", Description: "Disable printing of colors to stderr", Default: !runtime.ColorEnabled(), }, "verbose": &Option{ ShortName: "v", Type: "bool", Default: runtime.VerboseEnabled(), Description: "Log verbose output to stderr", }, "silent": &Option{ Type: "bool", Description: "Silence non-error logging", }, "skip-validation": &Option{ Type: "bool", Description: "Do not validate any arguments or options", }, "version": &Option{ Type: "bool", Default: false, Description: "Display program version and exit", }, }, }
Functions ¶
func RegisterValueSource ¶
func RegisterValueSource(key string, completion CompletionFunc)
Registers a completion function for the given command.ValueType key name.
Types ¶
type Argument ¶
type Argument struct { // Name is how this variable will be exposed to the underlying command. Name string `json:"name" yaml:"name" validate:"required,excludesall=!$\\/%^@#?:'\""` // Description is what this argument is for. Description string `json:"description" yaml:"description" validate:"required"` // Default is the default value for this argument if none is provided. Default any `json:"default,omitempty" yaml:"default,omitempty" validate:"excluded_with=Required"` // Variadic makes an argument a list of all values from this one on. Variadic bool `json:"variadic" yaml:"variadic"` // Required raises an error if an argument is not provided. Required bool `json:"required" yaml:"required" validate:"excluded_with=Default"` // Values describes autocompletion and validation for an argument Values *ValueSource `json:"values,omitempty" yaml:"values" validate:"omitempty"` Command *Command `json:"-" yaml:"-" validate:"-"` // contains filtered or unexported fields }
Argument represents a single command-line argument.
func (*Argument) Resolve ¶
func (arg *Argument) Resolve(current string) (values []string, flag cobra.ShellCompDirective, err error)
Resolve returns autocomplete values for an argument.
type Arguments ¶
type Arguments []*Argument
Arguments is an ordered list of Argument.
func (*Arguments) AllKnownStr ¶
func (*Arguments) CompletionFunction ¶
func (args *Arguments) CompletionFunction(cc *cobra.Command, provided []string, toComplete string) ([]string, cobra.ShellCompDirective)
CompletionFunction is called by cobra when asked to complete arguments.
type AutocompleteTemplate ¶
func (*AutocompleteTemplate) Arg ¶
func (tpl *AutocompleteTemplate) Arg(name string) string
func (*AutocompleteTemplate) Opt ¶
func (tpl *AutocompleteTemplate) Opt(name string) string
type Command ¶
type Command struct { Path []string `json:"path" yaml:"path"` // Summary is a short description of a command, on supported shells this is part of the autocomplete prompt Summary string `json:"summary" yaml:"summary" validate:"required"` // Description is a long form explanation of how a command works its magic. Markdown is supported Description string `json:"description" yaml:"description" validate:"required"` // A list of arguments for a command Arguments Arguments `json:"arguments" yaml:"arguments" validate:"dive"` // A map of option names to option definitions Options Options `json:"options" yaml:"options" validate:"dive"` HelpFunc HelpFunc `json:"-" yaml:"-"` // The action to take upon running Action Action `json:"-" yaml:"-"` Cobra *cobra.Command `json:"-" yaml:"-"` // Meta stores application specific stuff Meta any `json:"meta" yaml:"meta"` Hidden bool `json:"-" yaml:"-"` // contains filtered or unexported fields }
func (*Command) AdditionalHelp ¶
func (*Command) HasAdditionalHelp ¶
func (*Command) HelpRenderer ¶
func (*Command) ResolveTemplate ¶
func (*Command) SetBindings ¶
type CompletionFunc ¶
type Option ¶
type Option struct { // Type represents the type of value expected to be provided for this option. Type ValueType `json:"type" yaml:"type" validate:"omitempty,oneof=string bool int"` // Description is a required field that show up during completions and help. Description string `json:"description" yaml:"description" validate:"required"` // Default value for this option, if none provided. Default any `json:"default,omitempty" yaml:"default,omitempty"` // ShortName When set, enables representing this Option as a short flag (-x). ShortName string `json:"short-name,omitempty" yaml:"short-name,omitempty"` // nolint:tagliatelle // Values denote the source for completion/validation values of this option. Values *ValueSource `json:"values,omitempty" yaml:"values,omitempty" validate:"omitempty"` // Repeated options may be specified more than once. Repeated bool `json:"repeated" yaml:"repeated" validate:"omitempty"` // Command references the Command this Option is defined for. Command *Command `json:"-" yaml:"-" validate:"-"` // contains filtered or unexported fields }
Option represents a command line flag.
func (*Option) CompletionFunction ¶
func (opt *Option) CompletionFunction(cmd *cobra.Command, args []string, toComplete string) (values []string, flag cobra.ShellCompDirective)
CompletionFunction is called by cobra when asked to complete an option.
func (*Option) Resolve ¶
func (opt *Option) Resolve(currentValue string) (values []string, flag cobra.ShellCompDirective, err error)
Resolve returns autocomplete values for an option.
type Options ¶
Options is a map of name to Option.
func (*Options) AllKnownStr ¶
AllKnownStr returns a map of option names to their stringified values.
type SourceCommand ¶
type ValueSource ¶
type ValueSource struct { // Directories prompts for directories with the given prefix. Directories *string `json:"dirs,omitempty" yaml:"dirs,omitempty" validate:"omitempty,excluded_with=Command Files Func Script Static"` // Files prompts for files with the given extensions Files *[]string `json:"files,omitempty" yaml:"files,omitempty" validate:"omitempty,excluded_with=Command Func Directories Script Static"` // Script runs the provided command with `bash -c "$script"` and returns an option for every line of stdout. Script string `json:"script,omitempty" yaml:"script,omitempty" validate:"omitempty,excluded_with=Command Directories Files Func Static"` // Static returns the given list. Static *[]string `json:"static,omitempty" yaml:"static,omitempty" validate:"omitempty,excluded_with=Command Directories Files Func Script"` // Command runs a subcommand and returns an option for every line of stdout. Command *SourceCommand `json:"command,omitempty" yaml:"command,omitempty" validate:"omitempty,excluded_with=Directories Files Func Script Static"` // Func runs a function Func CompletionFunc `json:"-" yaml:"-" validate:"omitempty,excluded_with=Command Directories Files Script Static"` // Timeout is the maximum amount of time we will wait for a Script, Command, or Func before giving up on completions/validations. Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty" validate:"omitempty,excluded_with=Directories Files Static"` // Suggestion if provided will only suggest autocomplete values but will not perform validation of a given value Suggestion bool `json:"suggest-only" yaml:"suggest-only" validate:"omitempty"` // nolint:tagliatelle // SuggestRaw if provided the shell will not add a space after autocompleting SuggestRaw bool `json:"suggest-raw" yaml:"suggest-raw" validate:"omitempty"` // nolint:tagliatelle // contains filtered or unexported fields }
ValueSource represents the source for an auto-completed and/or validated option/argument.
func (*ValueSource) Resolve ¶
func (vs *ValueSource) Resolve(currentValue string) (values []string, flag cobra.ShellCompDirective, err error)
Resolve returns the values for autocomplete and validation.
func (*ValueSource) UnmarshalYAML ¶
func (vs *ValueSource) UnmarshalYAML(node *yaml.Node) error
func (*ValueSource) Validates ¶
func (vs *ValueSource) Validates() bool
Validates tells if a value needs to be validated.
type ValueType ¶
type ValueType string
ValueType represent the kinds of or option.
const ( // ValueTypeDefault is the empty string, maps to ValueTypeString. ValueTypeDefault ValueType = "" // ValueTypeString a value treated like a string. ValueTypeString ValueType = "string" // ValueTypeBoolean is a value treated like a boolean. ValueTypeBoolean ValueType = "bool" // ValueTypeBoolean is a value treated like an integer. ValueTypeInt ValueType = "int" )