configparam

package
v0.0.0-...-efc3bf3 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: Apache-2.0 Imports: 7 Imported by: 15

Documentation

Overview

Package configparam defines the configuration parameters that a CLI tool can use.

The values of the configuration parameters can be set via CLI flags, environment variables. A CLI flag shall be attached to a cobra.Command using the AttachToCommand method.

The BindConfiguration method binds the configured environment variable name. It shall be invoked only for the command that is executed. The recommendation is to invoke it in the PreRun phase of the cobra.Command.

The configured value of the parameter can be read using the Value and the ValueOrAsk methods.

Value returns the configured value if set. If the parameter value is not set, it returns the default value.

ValueOrAsk also returns the configured value if set. If not set, it asks the user for the value interactively.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoolParam

type BoolParam struct {
	// contains filtered or unexported fields
}

BoolParam type represents a configuration parameter that holds a bool value.

func Bool

func Bool(name, description string) *BoolParam

Bool creates BoolParam value. The mandatory name and description parameters must be set.

Example
package main

import (
	"github.com/SAP/xp-clifford/cli/configparam"

	"github.com/spf13/cobra"
)

func main() {
	boolParam := configparam.Bool("verbose", "Turn verbose messages on").
		WithEnvVarName("VERBOSE").
		WithShortName("v").
		WithDefaultValue(false)
	cmd := &cobra.Command{
		PreRun: func(cmd *cobra.Command, _ []string) {
			boolParam.BindConfiguration(cmd)
		},
	}
	boolParam.AttachToCommand(cmd)
}

func (*BoolParam) AttachToCommand

func (p *BoolParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the persistent bool flag (long form and optional short form) with the supplied cobra.Command.

func (*BoolParam) Value

func (p *BoolParam) Value() bool

Value returns the user configured string slice. If the user has not configured any values, the default value is returned.

type ConfigParam

type ConfigParam interface {
	GetName() string
	AttachToCommand(cmd *cobra.Command)
	BindConfiguration(cmd *cobra.Command)
}

ConfigParam interface defines the methods that a configuration parameter type must implement.

type DurationParam

type DurationParam struct {
	// contains filtered or unexported fields
}

DurationParam represents a configuration parameter that holds a time.Duration value.

func Duration

func Duration(name, description string) *DurationParam

Duration creates a new DurationParam with the specified name and description. The default value is set to 0.

func (*DurationParam) AskValue

func (p *DurationParam) AskValue(ctx context.Context) (time.Duration, error)

AskValue prompts the user for an duration value. This method always prompts the user, regardless of whether a value has already been set. The entered value is persisted in the configuration. Returns the entered value and any error that occurred during input.

func (*DurationParam) AttachToCommand

func (p *DurationParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the duration parameter as a persistent flag on the provided cobra command. This allows the parameter to be set via command-line arguments.

func (*DurationParam) Value

func (p *DurationParam) Value() time.Duration

Value retrieves the current duration value from the configuration.

func (*DurationParam) ValueOrAsk

func (p *DurationParam) ValueOrAsk(ctx context.Context) (time.Duration, error)

ValueOrAsk returns the configured value if it has been set, otherwise it prompts the user for input interactively. Returns the duration value and any error that occurred during input.

type FloatParam

type FloatParam struct {
	// contains filtered or unexported fields
}

FloatParam represents a configuration parameter that holds a float value.

func Float

func Float(name, description string) *FloatParam

Float creates a new FloatParam with the specified name and description. The default value is set to 0.0.

func (*FloatParam) AskValue

func (p *FloatParam) AskValue(ctx context.Context) (float64, error)

AskValue prompts the user for a float value. This method always prompts the user, regardless of whether a value has already been set. The entered value is persisted in the configuration. Returns the entered value and any error that occurred during input.

func (*FloatParam) AttachToCommand

func (p *FloatParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the float parameter as a persistent flag on the provided cobra command. This allows the parameter to be set via command-line arguments.

func (*FloatParam) Value

func (p *FloatParam) Value() float64

Value retrieves the current float value from the configuration.

func (*FloatParam) ValueOrAsk

func (p *FloatParam) ValueOrAsk(ctx context.Context) (float64, error)

ValueOrAsk returns the configured value if it has been set, otherwise it prompts the user for input interactively. Returns the float value and any error that occurred during input.

type IntParam

type IntParam struct {
	// contains filtered or unexported fields
}

IntParam represents a configuration parameter that holds an integer value.

func Int

func Int(name, description string) *IntParam

Int creates a new IntParam with the specified name and description. The default value is set to 0.

func (*IntParam) AskValue

func (p *IntParam) AskValue(ctx context.Context) (int, error)

AskValue prompts the user for an integer value. This method always prompts the user, regardless of whether a value has already been set. The entered value is persisted in the configuration. Returns the entered value and any error that occurred during input.

func (*IntParam) AttachToCommand

func (p *IntParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the integer parameter as a persistent flag on the provided cobra command. This allows the parameter to be set via command-line arguments.

func (*IntParam) Value

func (p *IntParam) Value() int

Value retrieves the current integer value from the configuration.

func (*IntParam) ValueOrAsk

func (p *IntParam) ValueOrAsk(ctx context.Context) (int, error)

ValueOrAsk returns the configured value if it has been set, otherwise it prompts the user for input interactively. Returns the integer value and any error that occurred during input.

type IntSliceParam

type IntSliceParam struct {
	// contains filtered or unexported fields
}

IntSliceParam type represents a configuration parameter that holds an []int value.

func IntSlice

func IntSlice(name, description string) *IntSliceParam

IntSlice creates an IntSliceParam value. The mandatory name and description parameters must be set.

func (*IntSliceParam) AttachToCommand

func (p *IntSliceParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the persistent int-slice flag (long form and optional short form) with the supplied cobra.Command.

func (*IntSliceParam) Value

func (p *IntSliceParam) Value() []int

Value returns the user configured int slice. If the user has not configured any values, the default value is returned.

func (*IntSliceParam) ValueOrAsk

func (p *IntSliceParam) ValueOrAsk(ctx context.Context) ([]int, error)

ValueOrAsk returns the configured slice or prompts the user to choose from the values supplied via [WithPossibleValues] or [WithPossibleValuesFn]. It fails if neither has been set.

After successful selection the chosen slice is stored and the parameter is considered set.

func (*IntSliceParam) WithEnvVarName

func (p *IntSliceParam) WithEnvVarName(_ string) *IntSliceParam

WithEnvVarName for IntSliceParam is not supported. See https://github.com/spf13/viper/issues/1611.

func (*IntSliceParam) WithPossibleValues

func (p *IntSliceParam) WithPossibleValues(values []int) *IntSliceParam

WithPossibleValues restricts the interactive selection to the supplied slice. When the CLI prompts the user, only these strings are offered as choices.

func (*IntSliceParam) WithPossibleValuesFn

func (p *IntSliceParam) WithPossibleValuesFn(fn func() ([]int, error)) *IntSliceParam

WithPossibleValuesFn lazily supplies the valid choices for interactive selection. The given function is called when the CLI prompts the user. The returned strings are presented as options.

type ParamList

type ParamList []ConfigParam

type StringParam

type StringParam struct {
	// contains filtered or unexported fields
}

StringParam type represents a configuration parameter that holds a []string value.

func SensitiveString

func SensitiveString(name, description string) *StringParam

SensitiveString returns a new StringParam whose contents are masked when the parameter is printed to the console. Both name and description are required.

func String

func String(name, description string) *StringParam

String creates a StringParam value. The mandatory name and description parameters must be set.

Example
package main

import (
	"github.com/SAP/xp-clifford/cli/configparam"

	"github.com/spf13/cobra"
)

func main() {
	nameParam := configparam.String("name", "Name of the user").
		WithDefaultValue("anonymous").
		WithEnvVarName("USER").
		WithExample("user1").
		WithShortName("u")
	passwordParam := configparam.SensitiveString("password", "Password of the user").
		WithEnvVarName("PASSWORD").
		WithShortName("p")
	cmd := &cobra.Command{
		PreRun: func(cmd *cobra.Command, _ []string) {
			nameParam.BindConfiguration(cmd)
			passwordParam.BindConfiguration(cmd)
		},
	}
	nameParam.AttachToCommand(cmd)
	passwordParam.AttachToCommand(cmd)
}

func (*StringParam) AskValue

func (p *StringParam) AskValue(ctx context.Context) (string, error)

func (*StringParam) AttachToCommand

func (p *StringParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the persistent string flag (long form and optional short form) with the supplied cobra.Command.

func (*StringParam) Value

func (p *StringParam) Value() string

Value returns the user configured string. If the user has not configured any values, the default value is returned.

func (*StringParam) ValueOrAsk

func (p *StringParam) ValueOrAsk(ctx context.Context) (string, error)

ValueOrAsk returns the configured slice or prompts the user to enter a value value.

After successful selection the entered string is stored and the parameter is considered set.

type StringSliceParam

type StringSliceParam struct {
	// contains filtered or unexported fields
}

StringSliceParam type represents a configuration parameterkxo that holds a []string value.

func SensitiveStringSlice

func SensitiveStringSlice(name, description string) *StringSliceParam

SensitiveStringSlice returns a new StringSliceParam whose contents are masked when the parameter is printed to the console. Both name and description are required.

func StringSlice

func StringSlice(name, description string) *StringSliceParam

StringSlice creates a StringSliceParam value. The mandatory name and description parameters must be set.

Example
package main

import (
	"github.com/SAP/xp-clifford/cli/configparam"

	"github.com/spf13/cobra"
)

func main() {
	groupParam := configparam.StringSlice("group", "Group that the user is member of").
		WithShortName("g")
	cmd := &cobra.Command{
		PreRun: func(cmd *cobra.Command, _ []string) {
			groupParam.BindConfiguration(cmd)
		},
	}
	groupParam.AttachToCommand(cmd)
}

func (*StringSliceParam) AttachToCommand

func (p *StringSliceParam) AttachToCommand(command *cobra.Command)

AttachToCommand registers the persistent string-slice flag (long form and optional short form) with the supplied cobra.Command.

func (*StringSliceParam) Value

func (p *StringSliceParam) Value() []string

Value returns the user configured string slice. If the user has not configured any values, the default value is returned.

func (*StringSliceParam) ValueOrAsk

func (p *StringSliceParam) ValueOrAsk(ctx context.Context) ([]string, error)

ValueOrAsk returns the configured slice or prompts the user to choose from the values supplied via [WithPossibleValues] or [WithPossibleValuesFn]. It fails if neither has been set.

After successful selection the chosen slice is stored and the parameter is considered set.

func (*StringSliceParam) WithPossibleValues

func (p *StringSliceParam) WithPossibleValues(values []string) *StringSliceParam

WithPossibleValues restricts the interactive selection to the supplied slice. When the CLI prompts the user, only these strings are offered as choices.

func (*StringSliceParam) WithPossibleValuesFn

func (p *StringSliceParam) WithPossibleValuesFn(fn func() ([]string, error)) *StringSliceParam

WithPossibleValuesFn lazily supplies the valid choices for interactive selection. The given function is called when the CLI prompts the user. The returned strings are presented as options.

Jump to

Keyboard shortcuts

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