altsrc

package module
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 12 Imported by: 30

README

Welcome to urfave/cli-altsrc/v3

Run Tests Go Reference Go Report Card

urfave/cli-altsrc/v3 is an extension for urfave/cli/v3 to read flag values from JSON, YAML, and TOML. The extension keeps third-party libraries for these features away from urfave/cli/v3.

Example
	configFiles := []string{
		filepath.Join(testdataDir, "config.yaml"),
		filepath.Join(testdataDir, "alt-config.yaml"),
	}

	app := &cli.Command{
		Name: "greet",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "name",
				Aliases: []string{"n"},
				Sources: yaml.YAML("greet.name", configFiles...),
			},
			&cli.IntFlag{
				Name:    "enthusiasm",
				Aliases: []string{"!"},
				Sources: yaml.YAML("greet.enthusiasm", configFiles...),
			},
		},
		Action: func(ctx context.Context, cmd *cli.Command) error {
			punct := ""
			if cmd.Int("enthusiasm") > 9000 {
				punct = "!"
			}

			fmt.Fprintf(os.Stdout, "Hello, %[1]v%[2]v\n", cmd.String("name"), punct)

			return nil
		},
	}

	// Simulating os.Args
	os.Args = []string{"greet"}

	if err := app.Run(context.Background(), os.Args); err != nil {
		fmt.Fprintf(os.Stdout, "OH NO: %[1]v\n", err)
	}

	// Output:
	// Hello, Berry!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Err = errors.New("urfave/cli-altsrc error")
)

Functions

func MustTestdataDir

func MustTestdataDir(ctx context.Context) string

func NestedVal

func NestedVal(name string, tree map[any]any) (any, bool)

NestedVal returns a value from the given map. The lookup name may be a dot-separated path into the map. If that is the case, it will recursively traverse the map based on the '.' delimited sections to find a nested value for the key.

func TestdataDir

func TestdataDir(ctx context.Context) (string, error)

Types

type MapAnyAnyURISourceCache

type MapAnyAnyURISourceCache = URISourceCache[map[any]any]

func NewMapAnyAnyURISourceCache

func NewMapAnyAnyURISourceCache(file string, f func([]byte, any) error) *MapAnyAnyURISourceCache

type Sourcer

type Sourcer interface {
	SourceURI() string
}

type StringPtrSourcer

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

func NewStringPtrSourcer

func NewStringPtrSourcer(p *string) StringPtrSourcer

func (StringPtrSourcer) SourceURI

func (s StringPtrSourcer) SourceURI() string

type StringSourcer

type StringSourcer string

func (StringSourcer) SourceURI

func (s StringSourcer) SourceURI() string

type URISourceCache

type URISourceCache[T any] struct {
	// contains filtered or unexported fields
}

func NewURISourceCache

func NewURISourceCache[T any](uri string, f func([]byte, any) error) *URISourceCache[T]

func (*URISourceCache[T]) Get

func (fsc *URISourceCache[T]) Get() T

type ValueSource

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

func NewValueSource

func NewValueSource(f func([]byte, any) error, desc string, key string, uriSrc Sourcer) *ValueSource

func (*ValueSource) GoString

func (vs *ValueSource) GoString() string

func (*ValueSource) Lookup

func (vs *ValueSource) Lookup() (string, bool)

func (*ValueSource) String

func (vs *ValueSource) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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