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