flag

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind(cmd *cobra.Command, v *viper.Viper) error

Bind connects cobra command flags with viper configuration values. It synchronizes flag values with their corresponding configuration values in viper, allowing flags to be set via environment variables or config files.

For each flag that hasn't been explicitly set on the command line:

  • Checks if a corresponding value exists in viper
  • If found, sets the flag's value to match the viper configuration

The function takes:

  • cmd: The cobra.Command containing the flags to bind
  • v: A configured viper instance with loaded configuration values

This enables a priority order where command-line flags take precedence over configuration values from viper (environment variables or config files).

Returns an error if either cmd or v is nil.

func Load

func Load(cmd *cobra.Command, flags []interface{}) error

Load registers flags with a cobra.Command based on the provided flag definitions. For each flag, it:

  • Registers it with the command using the appropriate type (string or int)
  • Adds allowed values to the usage text if specified
  • Handles both long (--flag) and short (-f) flag formats

The function takes:

  • cmd: The cobra.Command to register flags with
  • flags: A slice of flag definitions (String or Int types)

For flags with allowed values, the usage text is automatically extended with the list of allowed values in parentheses.

Example usage text for a flag with allowed values:

--format string   Output format (allowed: "json", "yaml", "text")

Returns an error if an unsupported flag type is provided.

func Validate

func Validate(cmd *cobra.Command, flags []interface{}) error

Validate checks if all flag values in the cobra.Command are valid according to their constraints. It performs the following validations:

  • Ensures all flags are of supported types (String or Int)
  • For flags with AllowedValues set, verifies the current value is in that list
  • Skips validation for flags that don't have AllowedValues defined

The function takes:

  • cmd: The cobra.Command containing the flags to validate
  • flags: A slice of flag definitions (String or Int types)

Returns an error if:

  • An unsupported flag type is found
  • A flag's value is not in its AllowedValues list

Types

type Int

type Int struct {
	// Pointer points to the variable that will store the flag value
	Pointer *int

	// FlagName is the name of the flag (used with -- prefix)
	FlagName string

	// FlagShortHand is an optional single-character shorthand (used with - prefix)
	FlagShortHand string

	// FlagUsage is the help text describing the flag's purpose
	FlagUsage string

	// AllowedValues is an optional list of valid values for this flag
	AllowedValues []int

	// FlagDefaultValue is used when the flag is not explicitly set
	FlagDefaultValue int
}

Int holds a *cobra.Command flag of type int. It provides a type-safe way to define integer flags with validation support.

type String

type String struct {
	// Pointer points to the variable that will store the flag value
	Pointer *string

	// FlagName is the name of the flag (used with -- prefix)
	FlagName string

	// FlagShortHand is an optional single-character shorthand (used with - prefix)
	FlagShortHand string

	// FlagDefaultValue is used when the flag is not explicitly set
	FlagDefaultValue string

	// FlagUsage is the help text describing the flag's purpose
	FlagUsage string

	// AllowedValues is an optional list of valid values for this flag
	AllowedValues []string
}

String holds a *cobra.Command flag of type string. It provides a type-safe way to define string flags with validation support.

Jump to

Keyboard shortcuts

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