yaml

package
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: 2 Imported by: 8

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func YAML

func YAML(key string, source altsrc.Sourcer) *altsrc.ValueSource

YAML is a helper function to encapsulate a number of yamlValueSource together as a cli.ValueSourceChain

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

app := &cli.Command{
	Name: "greet",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "name",
			Aliases: []string{"n"},
			Sources: cli.NewValueSourceChain(YAML("greet.name", configFiles[0]), YAML("greet.name", configFiles[1])),
		},
		&cli.IntFlag{
			Name:    "enthusiasm",
			Aliases: []string{"!"},
			Sources: cli.NewValueSourceChain(YAML("greet.enthusiasm", configFiles[0]), YAML("greet.enthusiasm", configFiles[1])),
		},
	},
	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!

Types

This section is empty.

Jump to

Keyboard shortcuts

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