-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtagflag.go
62 lines (54 loc) · 1.44 KB
/
tagflag.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
package tagflag
import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"golang.org/x/xerrors"
)
// Struct fields after this one are considered positional arguments.
type StartPos struct{}
// Default help flag was provided, and should be handled.
var ErrDefaultHelp = errors.New("help flag")
// Parses given arguments, returning any error.
func ParseErr(cmd interface{}, args []string, opts ...parseOpt) (err error) {
p, err := newParser(cmd, opts...)
if err != nil {
return
}
return p.parse(args)
}
// Parses the command-line arguments, exiting the process appropriately on
// errors or if usage is printed.
func Parse(cmd interface{}, opts ...parseOpt) *Parser {
opts = append([]parseOpt{Program(filepath.Base(os.Args[0]))}, opts...)
return ParseArgs(cmd, os.Args[1:], opts...)
}
// Like Parse, but operates on the given args instead.
func ParseArgs(cmd interface{}, args []string, opts ...parseOpt) *Parser {
p, err := newParser(cmd, opts...)
if err == nil {
err = p.parse(args)
}
if xerrors.Is(err, ErrDefaultHelp) {
p.printUsage(os.Stdout)
os.Exit(0)
}
if err != nil {
fmt.Fprintf(os.Stderr, "tagflag: error parsing args: %v\n", err)
if _, ok := err.(userError); ok {
os.Exit(2)
}
os.Exit(1)
}
return p
}
func Unmarshal(arg string, v interface{}) error {
_v := reflect.ValueOf(v).Elem()
m := valueMarshaler(_v.Type())
if m == nil {
return fmt.Errorf("can't unmarshal to type %s", _v.Type())
}
return m.Marshal(_v, arg)
}