-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
helpers.go
66 lines (60 loc) · 2.24 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// This file is part of go-getoptions.
//
// Copyright (C) 2015-2024 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package getoptions
import (
"fmt"
"strconv"
"github.com/DavidGamba/go-getoptions/text"
)
// 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.
//
// If the arguments have been named with `opt.HelpSynopsisArg` then the error will include the argument name.
func (gopt *GetOpt) GetRequiredArg(args []string, sections ...HelpSection) (string, []string, error) {
if len(args) < 1 {
if len(gopt.programTree.SynopsisArgs) > gopt.programTree.SynopsisArgsIdx {
argName := gopt.programTree.SynopsisArgs[gopt.programTree.SynopsisArgsIdx].Arg
fmt.Fprintf(Writer, text.ErrorMissingRequiredNamedArgument+"\n", argName)
} else {
fmt.Fprintf(Writer, "%s\n", text.ErrorMissingRequiredArgument)
}
if sections != nil {
fmt.Fprintf(Writer, "%s", gopt.Help(sections...))
} else {
fmt.Fprintf(Writer, "%s", gopt.Help(HelpSynopsis))
}
gopt.programTree.SynopsisArgsIdx++
return "", args, ErrorHelpCalled
}
gopt.programTree.SynopsisArgsIdx++
return args[0], args[1:], nil
}
// Same as GetRequiredArg but converts the argument to an int.
func (gopt *GetOpt) GetRequiredArgInt(args []string, sections ...HelpSection) (int, []string, error) {
arg, args, err := gopt.GetRequiredArg(args, sections...)
if err != nil {
return 0, args, err
}
i, err := strconv.Atoi(arg)
if err != nil {
return 0, args, fmt.Errorf(text.ErrorConvertArgumentToInt, arg)
}
return i, args, nil
}
// Same as GetRequiredArg but converts the argument to a float64.
func (gopt *GetOpt) GetRequiredArgFloat64(args []string, sections ...HelpSection) (float64, []string, error) {
arg, args, err := gopt.GetRequiredArg(args, sections...)
if err != nil {
return 0, args, err
}
f, err := strconv.ParseFloat(arg, 64)
if err != nil {
return 0, args, fmt.Errorf(text.ErrorConvertArgumentToFloat64, arg)
}
return f, args, nil
}