From 8a4f764a7304ecbff07d451a21926bdc1a7fb3d4 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Wed, 13 Apr 2022 16:04:05 +0200 Subject: add common option settings --- internal/options/options.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 internal/options/options.go (limited to 'internal') 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, "") +} -- cgit v1.2.3