Skip to content

urfave/cli-altsrc

Folders and files

NameName
Last commit message
Last commit date
Jan 23, 2025
Jun 23, 2023
Feb 27, 2025
Feb 27, 2025
Feb 27, 2025
Jun 23, 2023
Jun 23, 2023
Jun 23, 2023
Jun 23, 2023
Jan 23, 2025
Feb 27, 2025
Jan 29, 2025
Feb 27, 2025
Feb 27, 2025
Nov 17, 2024
Feb 9, 2025
Jan 31, 2025

Repository files navigation

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!