flagvalue

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package flagvalue provides custom pflag.Value implementations for common flag types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SimpleValue

type SimpleValue interface {
	constraints.Float | constraints.Integer | ~string | ~bool |
		*string | *bool
}

SimpleValue is a constraint that includes all the types supported by the Simple flag value.

type Value

type Value = pflag.Value

Value is the interface to the dynamic value stored in a flag.

func Counter

func Counter(val int, p *int) Value

Counter returns a pflags.Value that sets the value at p with the default val or the value provided via a flag.

func Duration

func Duration(val time.Duration, p *time.Duration) Value

Duration returns a pflag.Value that sets the duration at p with the default val or the value provided via a flag.

Example
package main

import (
	"time"

	"github.com/spf13/pflag"

	"github.com/hashicorp/tfctl-cli/internal/pkg/flagvalue"
)

func main() {
	var sleep time.Duration
	f := pflag.NewFlagSet("example", pflag.ContinueOnError)
	f.AddFlag(&pflag.Flag{
		Name:     "wait",
		Usage:    "wait specifies the time to sleep before taking an action",
		DefValue: "5s",
		Value:    flagvalue.Duration(5*time.Second, &sleep),
	})

	time.Sleep(sleep)

	// ... Take an action
}

func Enum

func Enum[T comparable](allowed []T, val T, p *T) Value

Enum returns a pflags.Value that sets the value at p with the default val or the value provided via a flag. The provided value must be in the allowed list or an error is returned.

Example
package main

import (
	"github.com/spf13/pflag"

	"github.com/hashicorp/tfctl-cli/internal/pkg/flagvalue"
)

func main() {
	var logLevel string
	f := pflag.NewFlagSet("example", pflag.ContinueOnError)
	f.AddFlag(&pflag.Flag{
		Name:     "log-level",
		Usage:    "log-level specifies the verbosity to log with.",
		DefValue: "warn",
		Value:    flagvalue.Enum([]string{"trace", "debug", "info", "warn", "error"}, "warn", &logLevel),
	})

	// Setup logger
	// logger := hclog.Default().SetLevel(hclog.LevelFromString(logLevel))
	// logger.Warn("we are using flags!")
}

func Simple

func Simple[T SimpleValue](val T, p *T) Value

Simple returns a pflags.Value that sets the value at p with the default val or the value provided via a flag.

If the type of the value is a boolean, set the NoOptDefVal to "true", on the Flag. Otherwise the flag will have to have a value set to be parsed. As an example if the boolean flag had the name "force" and NoOptDefVal is not set, the flag will have to be set as --force=true.

Example
package main

import (
	"github.com/spf13/pflag"

	"github.com/hashicorp/tfctl-cli/internal/pkg/flagvalue"
)

func main() {
	var projectID string
	f := pflag.NewFlagSet("example", pflag.ContinueOnError)
	f.AddFlag(&pflag.Flag{
		Name:  "project",
		Usage: "project specifies the HCP Project ID to use.",
		Value: flagvalue.Simple[string]("", &projectID),
	})
}
Example (Boolean)
package main

import (
	"github.com/spf13/pflag"

	"github.com/hashicorp/tfctl-cli/internal/pkg/flagvalue"
)

func main() {
	var force bool
	f := pflag.NewFlagSet("example", pflag.ContinueOnError)
	f.AddFlag(&pflag.Flag{
		Name:      "force",
		Shorthand: "f",
		Usage:     "force force deletes without confirmation.",
		Value:     flagvalue.Simple[bool](false, &force),

		// Critical to set for boolean values. Otherwise -f, --force will not
		// set force to true. Instead the flag parsing will error expecting a
		// value to be set for the flag.
		NoOptDefVal: "true",
	})
}

func SimpleMap

func SimpleMap[K, V SimpleValue](val map[K]V, p *map[K]V) Value

SimpleMap returns a pflags.Value that sets the map at p with the default val or the value(s) provided via a flag.

Example
package main

import (
	"github.com/spf13/pflag"

	"github.com/hashicorp/tfctl-cli/internal/pkg/flagvalue"
)

func main() {
	var headers map[string]string
	f := pflag.NewFlagSet("example", pflag.ContinueOnError)
	f.AddFlag(&pflag.Flag{
		Name:  "headers",
		Usage: "headers is a set of headers to send with the request. May be specified multiple times in the form of KEY=VALUE.",
		Value: flagvalue.SimpleMap(nil, &headers),
	})

	// Make the request
}

func SimpleSlice

func SimpleSlice[T SimpleValue](val []T, p *[]T) Value

SimpleSlice returns a pflags.Value that sets the slice at p with the default val or the value(s) provided via a flag.

Example
package main

import (
	"github.com/spf13/pflag"

	"github.com/hashicorp/tfctl-cli/internal/pkg/flagvalue"
)

func main() {
	var secrets []string
	f := pflag.NewFlagSet("example", pflag.ContinueOnError)
	f.AddFlag(&pflag.Flag{
		Name:  "secret",
		Usage: "secret is a secret to read. Multiple values may be specified.",
		Value: flagvalue.SimpleSlice[string]([]string{}, &secrets),
	})

	// Fetch the secrets
}

Jump to

Keyboard shortcuts

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