-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (103 loc) · 2.5 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cmd
import (
"fmt"
"os"
"strings"
"github.com/GreenRaccoon23/gosloc/futil"
"github.com/GreenRaccoon23/gosloc/logger"
"github.com/spf13/pflag"
)
const (
newLine = '\n'
)
var (
// inclusionsBunch description under parsArgs
inclusionsBunch string
// exclusionsBunch description under parsArgs
exclusionsBunch string
// Concurrency description under parsArgs
Concurrency int
// Total description under parsArgs
Total bool
// Rpaths description under parsArgs
Rpaths []string
// Inclusions is inclusionsBunch split by ','
Inclusions []string
// Exclusions is inclusionsBunch split by ','
Exclusions []string
)
// Parse parses command line arguments
func Parse() {
parsArgs()
parseStdin()
// debug()
// os.Exit(0)
validate()
setInclusions()
setExclusions()
}
func parsArgs() {
pflag.StringVarP(&inclusionsBunch, "include", "i", "", "File patterns to include, separated by commas")
pflag.StringVarP(&exclusionsBunch, "exclude", "x", "", "File patterns to exclude, separated by commas")
pflag.IntVarP(&Concurrency, "concurrency", "c", 1, "Max number of files to read simultaneously")
pflag.BoolVarP(&Total, "total", "t", false, "Show a grand total, not the total for each file")
pflag.Usage = usage
pflag.CommandLine.SortFlags = false
pflag.Parse()
Rpaths = pflag.Args()
}
func parseStdin() {
stdin := os.Stdin
if !futil.IsPipe(stdin) {
return
}
rpaths, err := futil.ReadLines(stdin)
if err != nil {
logger.Err(err)
}
Rpaths = append(Rpaths, rpaths...)
}
// func debug() {
//
// fmt.Printf("inclusionsBunch: %v\n", inclusionsBunch)
// fmt.Printf("exclusionsBunch: %v\n", exclusionsBunch)
// fmt.Printf("Concurrency: %v\n", Concurrency)
// fmt.Printf("Total: %v\n", Total)
// fmt.Printf("Rpaths: %v\n", Rpaths)
// }
// usage overrides pflag.Usage
func usage() {
fmt.Fprintf(os.Stderr, "gosloc <options> <path>...\n")
pflag.PrintDefaults()
fmt.Fprintf(os.Stderr,
`
WARNING: Setting concurrency too high will cause the program to crash.
`,
)
}
func validate() {
if len(Rpaths) == 0 {
complain("No paths specified")
}
if Concurrency <= 0 {
complain("-c (concurrency) must be above 0")
}
}
func complain(complaint string) {
fmt.Fprintf(os.Stderr, "%v\n\n", complaint)
usage()
fmt.Fprintf(os.Stderr, "\n%v\n", complaint)
os.Exit(2)
}
func setInclusions() {
if inclusionsBunch == "" {
return
}
Inclusions = strings.Split(inclusionsBunch, ",")
}
func setExclusions() {
if exclusionsBunch == "" {
return
}
Exclusions = strings.Split(exclusionsBunch, ",")
}