Skip to content

Commit 17eb18b

Browse files
committed
Handle errors that occur when parsing command-line options
1 parent 8ea4006 commit 17eb18b

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

git-sizer.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,54 +65,59 @@ func mainImplementation() error {
6565
var progress bool
6666
var version bool
6767

68-
pflag.BoolVar(&processBranches, "branches", false, "process all branches")
69-
pflag.BoolVar(&processTags, "tags", false, "process all tags")
70-
pflag.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
68+
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
7169

72-
pflag.VarP(
70+
flags.BoolVar(&processBranches, "branches", false, "process all branches")
71+
flags.BoolVar(&processTags, "tags", false, "process all tags")
72+
flags.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
73+
74+
flags.VarP(
7375
sizes.NewThresholdFlagValue(&threshold, 0),
7476
"verbose", "v", "report all statistics, whether concerning or not",
7577
)
76-
pflag.Lookup("verbose").NoOptDefVal = "true"
78+
flags.Lookup("verbose").NoOptDefVal = "true"
7779

78-
pflag.Var(
80+
flags.Var(
7981
&threshold, "threshold",
8082
"minimum level of concern (i.e., number of stars) that should be\n"+
8183
" reported",
8284
)
8385

84-
pflag.Var(
86+
flags.Var(
8587
sizes.NewThresholdFlagValue(&threshold, 30),
8688
"critical", "only report critical statistics",
8789
)
88-
pflag.Lookup("critical").NoOptDefVal = "true"
90+
flags.Lookup("critical").NoOptDefVal = "true"
8991

90-
pflag.Var(
92+
flags.Var(
9193
&nameStyle, "names",
9294
"display names of large objects in the specified `style`:\n"+
9395
" --names=none omit footnotes entirely\n"+
9496
" --names=hash show only the SHA-1s of objects\n"+
9597
" --names=full show full names",
9698
)
9799

98-
pflag.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
99-
pflag.UintVar(&jsonVersion, "json-version", 1, "JSON format version to output (1 or 2)")
100+
flags.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
101+
flags.UintVar(&jsonVersion, "json-version", 1, "JSON format version to output (1 or 2)")
100102

101103
atty, err := isatty.Isatty(os.Stderr.Fd())
102104
if err != nil {
103105
atty = false
104106
}
105-
pflag.BoolVar(&progress, "progress", atty, "report progress to stderr")
106-
pflag.BoolVar(&version, "version", false, "report the git-sizer version number")
107-
pflag.Var(&NegatedBoolValue{&progress}, "no-progress", "suppress progress output")
108-
pflag.Lookup("no-progress").NoOptDefVal = "true"
107+
flags.BoolVar(&progress, "progress", atty, "report progress to stderr")
108+
flags.BoolVar(&version, "version", false, "report the git-sizer version number")
109+
flags.Var(&NegatedBoolValue{&progress}, "no-progress", "suppress progress output")
110+
flags.Lookup("no-progress").NoOptDefVal = "true"
109111

110-
pflag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
111-
pflag.CommandLine.MarkHidden("cpuprofile")
112+
flags.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
113+
flags.MarkHidden("cpuprofile")
112114

113-
pflag.CommandLine.SortFlags = false
115+
flags.SortFlags = false
114116

115-
pflag.Parse()
117+
err = flags.Parse(os.Args[1:])
118+
if err != nil {
119+
return err
120+
}
116121

117122
if jsonOutput && !(jsonVersion == 1 || jsonVersion == 2) {
118123
return fmt.Errorf("JSON version must be 1 or 2")
@@ -136,7 +141,7 @@ func mainImplementation() error {
136141
return nil
137142
}
138143

139-
args := pflag.Args()
144+
args := flags.Args()
140145

141146
if len(args) != 0 {
142147
return errors.New("excess arguments")

0 commit comments

Comments
 (0)