diff options
author | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-04-13 16:04:05 +0200 |
---|---|---|
committer | Rasmus Dahlberg <rasmus@mullvad.net> | 2022-04-13 16:04:05 +0200 |
commit | 8a4f764a7304ecbff07d451a21926bdc1a7fb3d4 (patch) | |
tree | b3f403ba48437693a061aa331110c972730ebc5c | |
parent | 6d9aea74c19c350802cb52787f1450bd751557f5 (diff) |
add common option settings
-rw-r--r-- | internal/options/options.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/options/options.go b/internal/options/options.go new file mode 100644 index 0000000..bfe16a2 --- /dev/null +++ b/internal/options/options.go @@ -0,0 +1,31 @@ +package options + +import ( + "flag" + "os" +) + +func Parse(usage func(), set func(*flag.FlagSet)) *flag.FlagSet { + args := os.Args + if len(args) < 2 { + args = append(args, "") + } + + fs := flag.NewFlagSet(args[1], flag.ExitOnError) + fs.Usage = func() { + usage() + } + set(fs) + fs.Parse(args[2:]) + return fs +} + +func AddString(fs *flag.FlagSet, opt *string, short, long, value string) { + fs.StringVar(opt, short, value, "") + fs.StringVar(opt, long, value, "") +} + +func AddUint64(fs *flag.FlagSet, opt *uint64, short, long string, value uint64) { + fs.Uint64Var(opt, short, value, "") + fs.Uint64Var(opt, long, value, "") +} |