From a3b47e479697abb768def207fb51df9bb2aeb0dc Mon Sep 17 00:00:00 2001 From: David Gamba Date: Fri, 29 Dec 2023 11:33:02 -0700 Subject: [PATCH] Add GetRequiredArg helper --- helpers.go | 18 ++++++++++++++++++ user_help.go | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 helpers.go diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..15dad72 --- /dev/null +++ b/helpers.go @@ -0,0 +1,18 @@ +package getoptions + +import "fmt" + +// GetRequiredArg - Get the next argument from the args list and error if it doesn't exist. +// By default the error will include the HelpSynopsis section but it can be overriden with the list of sections or getoptions.HelpNone. +func GetRequiredArg(opt *GetOpt, args []string, argName string, sections ...HelpSection) (string, []string, error) { + if len(args) < 1 { + fmt.Fprintf(Writer, "ERROR: missing %s\n", argName) + if sections != nil { + fmt.Fprintf(Writer, "%s", opt.Help(sections...)) + } else { + fmt.Fprintf(Writer, "%s", opt.Help(HelpSynopsis)) + } + return "", args, ErrorHelpCalled + } + return args[0], args[1:], nil +} diff --git a/user_help.go b/user_help.go index 8fa5f63..cdc6cf5 100644 --- a/user_help.go +++ b/user_help.go @@ -13,7 +13,8 @@ type HelpSection int // Help Output Types const ( - helpDefaultName HelpSection = iota + HelpNone HelpSection = iota + helpDefaultName HelpName HelpSynopsis HelpCommandList