-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathargv.go
40 lines (35 loc) · 991 Bytes
/
argv.go
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
package gonfig
import (
"flag"
"log"
"os"
"strings"
)
// the Argv configurable.
type ArgvConfig struct {
Configurable
Prefix string
}
// Creates a new ArgvConfig and returns it as a ReadableConfig
func NewArgvConfig(prefix string) ReadableConfig {
cfg := &ArgvConfig{NewMemoryConfig(), prefix}
return cfg
}
// Loads all the variables from argv to the underlaying Configurable.
// If a Prefix is provided for ArgvConfig then keys are imported with the Prefix removed
// so --test.asd=1 with Prefix 'test.' imports "asd" with value of 1
func (self *ArgvConfig) Load() (err error) {
flagset := flag.NewFlagSet("arguments", flag.ContinueOnError)
flagset.Parse(os.Args)
flagset.VisitAll(func(f *flag.Flag) {
name := f.Name
log.Println(f.Name)
if self.Prefix != "" && strings.HasPrefix(f.Name, self.Prefix) {
name = strings.Replace(name, self.Prefix, "", 1)
}
if getter, ok := f.Value.(flag.Getter); ok {
self.Set(name, getter.Get().(string))
}
})
return nil
}