flags

package module
v0.0.0-...-c6a0258 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NameDefault = strs.TrimExe(filepath.Base(os.Args[0]))

Functions

func Execute

func Execute()

func RootSet

func RootSet(options ...Option)

Types

type Command

type Command = cobra.Command

func AddCommand

func AddCommand(use string, options ...Option) *Command

type FlagOption

type FlagOption func(*FlagSet) // FlagOption flag option

func Struct

func Struct(structObj any, options ...StructOption) FlagOption

func Val

func Val[T VarT](val *T, name, short string, usage, env string) FlagOption

Val is a helper function to add a variable to the flag set.

func Var

func Var[T VarT](name, short string, val T, usage, env string) FlagOption

Var is a helper function to add a variable to the flag set.

func (FlagOption) Apply

func (fo FlagOption) Apply(fs *FlagSet)

type FlagSet

type FlagSet = pflag.FlagSet

type Option

type Option func(*Command) // Option option

func Aliases

func Aliases(aliases ...string) Option

Aliases is an array of aliases that can be used instead of the first word in Use.

func Deprecated

func Deprecated(deprecated string) Option

Deprecated defines, if this command is deprecated and should print this string when used.

func Description

func Description(short string, long ...string) Option

Description

Short is the short description shown in the 'help' output.
Long is the long message shown in the 'help <this-command>' output.

func ExactArgs

func ExactArgs(n int) Option

ExactArgs returns an error if there are not exactly n args.

func Example

func Example(example string) Option

Example is examples of how to use the command.

func FlagStruct

func FlagStruct(structObj any, options ...StructOption) Option

func Flags

func Flags(sets ...FlagOption) Option

Flags returns the complete FlagSet that applies to this command (local and persistent declared here and by all parents).

func GroupID

func GroupID(groupID string) Option

The group id under which this subcommand is grouped in the 'help' output of its parent.

func Hidden

func Hidden(hide bool) Option

func MarkHidden

func MarkHidden(names ...string) Option

func MaximumNArgs

func MaximumNArgs(n int) Option

MaximumNArgs returns an error if there are more than N args.

func MinimumNArgs

func MinimumNArgs(n int) Option

MinimumNArgs returns an error if there is not at least N args.

func Options

func Options(options ...Option) Option

Options options

func PersistentFlags

func PersistentFlags(sets ...FlagOption) Option

PersistentFlags returns the persistent FlagSet specifically set in the current command.

func PersistentPostRun

func PersistentPostRun[T RunFunc](fn T) Option

PersistentPostRun persistent post run the command

func PersistentPreRun

func PersistentPreRun[T RunFunc](fn T) Option

PersistentPreRun persistent pre run the command

func PositionalArgs

func PositionalArgs(fn func(cmd *Command, args []string) error) Option

Expected arguments

func PostRun

func PostRun[T RunFunc](fn T) Option

PostRun post run the command

func PreRun

func PreRun[T RunFunc](fn T) Option

PreRun pre run the command

func RangeArgs

func RangeArgs(min, max int) Option

RangeArgs returns an error if the number of args is not within the expected range.

func Run

func Run[T RunFunc](fn T) Option

Run run the command

func SortFlags

func SortFlags(enabled bool) Option

func SortPersistentFlags

func SortPersistentFlags(enabled bool) Option

func SuggestFor

func SuggestFor(suggestFor ...string) Option

SuggestFor is an array of command names for which this command will be suggested - similar to aliases but only suggests.

func Use

func Use(use string, aliases ...string) Option

Use is the one-line usage message. Recommended syntax is as follows:

[ ] identifies an optional argument. Arguments that are not enclosed in brackets are required.
... indicates that you can specify multiple values for the previous argument.
|   indicates mutually exclusive information. You can use the argument to the left of the separator or the
    argument to the right of the separator. You cannot use both arguments in a single use of the command.
{ } delimits a set of mutually exclusive arguments when one of the arguments is required. If the arguments are
    optional, they are enclosed in brackets ([ ]).

Example: add [-F file | -D dir]... [-f format] profile

Aliases is an array of aliases that can be used instead of the first word in Use.

type RunFunc

type RunFunc interface {
	func() | func([]string) | func(*Command, []string) | func(*Command) |
		func() error | func([]string) error | func(*Command, []string) error | func(*Command) error |
		func(context.Context) | func(context.Context) error | func(context.Context, []string) error
}

RunFunc cobra run

type StructOption

type StructOption func(*StructOptions)

func EnvPrefix

func EnvPrefix(prefix string) StructOption

func NamePrefix

func NamePrefix(prefix string) StructOption

type StructOptions

type StructOptions struct {
	EnvPrefix  string
	NamePrefix string
}

type VarT

type VarT interface {
	strs.Ints |
		~bool | ~string | time.Duration | net.IP |
		~[]bool | ~[]string |
		~[]int | ~[]int32 | ~[]int64 |
		~[]uint |
		~[]float32 | ~[]float64 |
		~[]time.Duration |
		~[]net.IP
}

VarT is the type of the variable to be added to the flag set.

Jump to

Keyboard shortcuts

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