-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathargument_parser.go
114 lines (92 loc) · 2.88 KB
/
argument_parser.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"bytes"
"errors"
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/jessevdk/go-flags"
)
const usage = "[options] <from> <to> [<path>]"
type arguments struct {
Bare bool `short:"b" long:"bare" description:"Use patterns as they are"`
Regexp bool `short:"r" long:"regexp" description:"Use patterns as regular expressions"`
RawCaseNames string `short:"c" long:"cases" description:"Comma-separated names of enabled case styles (options: camel, upper-camel, kebab, upper-kebab, snake, upper-snake, space, upper-space)"`
RawInclude string `short:"i" long:"include" description:"Include paths matched with the given regular expression"`
RawExclude string `short:"e" long:"exclude" description:"Exclude paths matched with the given regular expression"`
NoGit bool `long:"no-git" description:"Ignore Git repository information"`
Verbose bool `short:"v" long:"verbose" description:"Be verbose"`
Help bool `short:"h" long:"help" description:"Show this help"`
Version bool `long:"version" description:"Show version"`
From string
To string
Path string
CaseNames map[caseName]struct{}
Include *regexp.Regexp
Exclude *regexp.Regexp
}
type argumentParser struct {
workingDirectory string
}
func newArgumentParser(d string) *argumentParser {
return &argumentParser{d}
}
func (p *argumentParser) Parse(ss []string) (*arguments, error) {
args := arguments{}
parser := flags.NewParser(&args, flags.PassDoubleDash)
parser.Usage = usage
ss, err := parser.ParseArgs(ss)
if err != nil {
return nil, err
} else if args.Help || args.Version {
return &args, nil
} else if len(ss) < 2 || len(ss) > 3 {
return nil, errors.New("invalid number of arguments")
}
args.From, args.To = ss[0], ss[1]
if len(ss) == 3 {
args.Path = p.resolvePath(ss[2])
} else {
args.Path = p.workingDirectory
}
if args.RawCaseNames != "" {
args.CaseNames = map[caseName]struct{}{}
for _, n := range strings.Split(args.RawCaseNames, ",") {
n := caseName(n)
if _, ok := allCaseNames[n]; !ok {
return nil, fmt.Errorf("invalid case name: %v", n)
}
args.CaseNames[n] = struct{}{}
}
}
if args.RawInclude != "" {
args.Include, err = regexp.Compile(args.RawInclude)
if err != nil {
return nil, err
}
}
if args.RawExclude != "" {
args.Exclude, err = regexp.Compile(args.RawExclude)
if err != nil {
return nil, err
}
}
return &args, nil
}
func (*argumentParser) Help() string {
p := flags.NewParser(&arguments{}, flags.PassDoubleDash)
p.Usage = usage
// Parse() is run here to show default values in help.
// This seems to be a bug in go-flags.
p.Parse() // nolint:errcheck
b := &bytes.Buffer{}
p.WriteHelp(b)
return b.String()
}
func (p *argumentParser) resolvePath(s string) string {
if filepath.IsAbs(s) {
return s
}
return filepath.Join(p.workingDirectory, s)
}