Skip to content

Commit

Permalink
Add GetRequiredArg helper
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Dec 29, 2023
1 parent c83ca56 commit a3b47e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
18 changes: 18 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion user_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type HelpSection int

// Help Output Types
const (
helpDefaultName HelpSection = iota
HelpNone HelpSection = iota
helpDefaultName
HelpName
HelpSynopsis
HelpCommandList
Expand Down

0 comments on commit a3b47e4

Please sign in to comment.