aboutsummaryrefslogtreecommitdiff
path: root/flag.go
blob: 9e12188a34f0b73b090c5298e8e3187ef400855f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package envflag

import (
	"flag"
	"fmt"
	"os"
	"regexp"
	"strings"
)

// Option type for configing the parsing.
type Option option
type option func(*config)

// FlagConverterFunc functions are used to update flag name for envvar lookups.
type FlagConverterFunc func(flag string) string

// UsageUpdaterFunc functions are used to update flag usage strings.
type UsageUpdaterFunc func(key, usage string) string

type config struct {
	flagConverter FlagConverterFunc
	usageUpdater  UsageUpdaterFunc
}

// UsageUpdater enables configurable flag usage updates.
func UsageUpdater(f UsageUpdaterFunc) Option {
	return func(cfg *config) {
		cfg.usageUpdater = f
	}
}

// UsagePrefixer prefixes the flag usage with [envvar].
func UsagePrefixer() Option {
	return func(cfg *config) {
		cfg.usageUpdater = usagePrefixer
	}
}

// UsageSuffixer suffixes the flag usage with [envvar].
func UsageSuffixer() Option {
	return func(cfg *config) {
		cfg.usageUpdater = usageSuffixer
	}
}

// FlagConverter converts the flag name to an environment variable key.
func FlagConverter(f FlagConverterFunc) Option {
	return func(cfg *config) {
		cfg.flagConverter = f
	}
}

// ParseWithEnv parses the command-line flags from os.Args[1:]. Must be called
// after all flags are defined and before flags are accessed by the program.
func Parse(opts ...Option) error {
	return parseFlagSetWithEnv(flag.CommandLine, os.Args[1:], opts...)
}

// ParseFlagSetWithEnv parses flag definitions from the argument list, which
// should not include the command name. Must be called after all flags in the
// FlagSet are defined and before flags are accessed by the program. The return
// value will be ErrHelp if -help or -h were set but not defined.
func ParseFlagSet(fs *flag.FlagSet, arguments []string, opts ...Option) error {
	return parseFlagSetWithEnv(fs, arguments, opts...)
}

func parseFlagSetWithEnv(fs *flag.FlagSet, arguments []string, opts ...Option) error {
	if fs.Parsed() {
		return fmt.Errorf("flag has already been parsed")
	}
	cfg := &config{
		flagConverter: flagToEnv(),
	}
	for _, o := range opts {
		o(cfg)
	}
	var nerr error
	fs.VisitAll(func(f *flag.Flag) {
		envKey := cfg.flagConverter(f.Name)
		if cfg.usageUpdater != nil {
			f.Usage = cfg.usageUpdater(envKey, f.Usage)
		}
		if envVal := os.Getenv(envKey); envVal != "" {
			err := f.Value.Set(envVal)
			if err != nil && nerr == nil {
				nerr = err
			}
		}
	})
	if nerr != nil {
		return nerr
	}
	return fs.Parse(arguments)
}

// convert this-format to THIS_FORMAT
func flagToEnv() func(string) string {
	re := regexp.MustCompile("[^a-zA-Z0-9_]+")
	return func(f string) string {
		return strings.ToUpper(re.ReplaceAllLiteralString(f, "_"))
	}
}

func usageSuffixer(key, usage string) string {
	envSuffix := fmt.Sprintf("[%s]", key)
	if strings.HasSuffix(usage, envSuffix) {
		return usage
	}
	return fmt.Sprintf("%s %s", usage, envSuffix)
}

func usagePrefixer(key, usage string) string {
	envPrefix := fmt.Sprintf("[%s]", key)
	if strings.HasPrefix(usage, envPrefix) {
		return usage
	}
	return fmt.Sprintf("%s %s", envPrefix, usage)
}