Skip to content

Commit 55a4811

Browse files
committed
Each flag "over" and "top" has default value
1 parent 6923ae4 commit 55a4811

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

cmd/gocognit/main.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package main
1818

1919
import (
20-
"errors"
2120
"flag"
2221
"fmt"
2322
"go/parser"
@@ -47,16 +46,15 @@ The output fields for each line are:
4746
<complexity> <package> <function> <file:row:column>
4847
`
4948

50-
func usage() {
51-
_, _ = fmt.Fprint(os.Stderr, usageDoc)
52-
os.Exit(2)
53-
}
49+
const (
50+
defaultOverFlagVal = -1
51+
defaultTopFlagVal = 0
52+
)
5453

5554
const (
56-
defaultValueIndicator = -1
57-
textFormat = "text"
58-
jsonFormat = "json"
59-
jsonPrettyFormat = "json-pretty"
55+
textFormat = "text"
56+
jsonFormat = "json"
57+
jsonPrettyFormat = "json-pretty"
6058
)
6159

6260
var errFormatNotDefined = fmt.Errorf("format is not valid, use a supported format %v", supportedFormats)
@@ -66,8 +64,8 @@ var (
6664
textFormat, jsonFormat, jsonPrettyFormat,
6765
}
6866

69-
over = flag.Int("over", defaultValueIndicator, "show functions with complexity > N only")
70-
top = flag.Int("top", defaultValueIndicator, "show the top N most complex functions only")
67+
over = flag.Int("over", defaultOverFlagVal, "show functions with complexity > N only")
68+
top = flag.Int("top", defaultTopFlagVal, "show the top N most complex functions only")
7169
avg = flag.Bool("avg", false, "show the average complexity")
7270
format = flag.String("format", textFormat, fmt.Sprintf("which format to use, supported formats: %v", supportedFormats))
7371
)
@@ -174,16 +172,14 @@ func analyzeDir(dirname string, stats []gocognit.Stat) ([]gocognit.Stat, error)
174172
return stats, nil
175173
}
176174

177-
var errFormatNotDefined = errors.New(fmt.Sprintf("Format is not valid, use a supported format %v", supportedFormats))
178-
179175
func writeStats(w io.Writer, sortedStats []gocognit.Stat) (int, error) {
180176
filter := gocognit.Filter{}
181-
if *top != defaultValueIndicator {
177+
if *top != 0 {
182178
// top filter
183179
filter.AddFilter(gocognit.NewTopFilter(*top))
184180
}
185181

186-
if *over != defaultValueIndicator {
182+
if *over != -1 {
187183
// over filter
188184
filter.AddFilter(gocognit.NewComplexityFilter(*over))
189185
}
@@ -193,13 +189,10 @@ func writeStats(w io.Writer, sortedStats []gocognit.Stat) (int, error) {
193189
switch *format {
194190
case textFormat:
195191
formatter = gocognit.NewTextFormatter(w)
196-
break
197192
case jsonFormat:
198193
formatter = gocognit.NewJsonFormatter(w, false)
199-
break
200194
case jsonPrettyFormat:
201195
formatter = gocognit.NewJsonFormatter(w, true)
202-
break
203196
default:
204197
return 0, errFormatNotDefined
205198
}

0 commit comments

Comments
 (0)