Skip to content

Commit 6cda807

Browse files
authored
Merge pull request #8 from github/posix-flags
Switch to using POSIX-style flags
2 parents f0efe64 + f942a58 commit 6cda807

32 files changed

+3729
-35
lines changed

Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@
77
unused-packages = true
88
go-tests = true
99
non-go = true
10+
11+
[[constraint]]
12+
name = "github.com/spf13/pflag"
13+
version = "1.0.0"

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ releases/git-sizer-$$(VERSION)-$(1)-$(2).zip: bin/git-sizer-$(1)-$(2)$(3)
6363
mkdir -p releases/tmp-$$(VERSION)-$(1)-$(2)
6464
cp README.md LICENSE.md releases/tmp-$$(VERSION)-$(1)-$(2)
6565
cp bin/git-sizer-$(1)-$(2)$(3) releases/tmp-$$(VERSION)-$(1)-$(2)/git-sizer$(3)
66+
cp vendor/github.com/spf13/pflag/LICENSE releases/tmp-$$(VERSION)-$(1)-$(2)/LICENSE-spf13-pflag
6667
rm -f $$@
6768
zip -j $$@ releases/tmp-$$(VERSION)-$(1)-$(2)/*
6869
rm -rf releases/tmp-$$(VERSION)-$(1)-$(2)

git-sizer.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"errors"
6-
"flag"
76
"fmt"
87
"io"
98
"os"
@@ -13,6 +12,8 @@ import (
1312
"github.com/github/git-sizer/git"
1413
"github.com/github/git-sizer/isatty"
1514
"github.com/github/git-sizer/sizes"
15+
16+
"github.com/spf13/pflag"
1617
)
1718

1819
type NegatedBoolValue struct {
@@ -37,8 +38,8 @@ func (b *NegatedBoolValue) String() string {
3738
}
3839
}
3940

40-
func (v *NegatedBoolValue) IsBoolFlag() bool {
41-
return true
41+
func (v *NegatedBoolValue) Type() string {
42+
return "bool"
4243
}
4344

4445
func main() {
@@ -59,43 +60,52 @@ func mainImplementation() error {
5960
var threshold sizes.Threshold = 1
6061
var progress bool
6162

62-
flag.BoolVar(&processBranches, "branches", false, "process all branches")
63-
flag.BoolVar(&processTags, "tags", false, "process all tags")
64-
flag.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
65-
flag.Var(
63+
pflag.BoolVar(&processBranches, "branches", false, "process all branches")
64+
pflag.BoolVar(&processTags, "tags", false, "process all tags")
65+
pflag.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
66+
67+
pflag.VarP(
68+
sizes.NewThresholdFlagValue(&threshold, 0),
69+
"verbose", "v", "report all statistics, whether concerning or not",
70+
)
71+
pflag.Lookup("verbose").NoOptDefVal = "true"
72+
73+
pflag.Var(
6674
&threshold, "threshold",
6775
"minimum level of concern (i.e., number of stars) that should be\n"+
68-
" reported",
76+
" reported",
6977
)
70-
flag.Var(
78+
79+
pflag.Var(
7180
sizes.NewThresholdFlagValue(&threshold, 30),
7281
"critical", "only report critical statistics",
7382
)
74-
flag.Var(
75-
sizes.NewThresholdFlagValue(&threshold, 0),
76-
"verbose", "report all statistics, whether concerning or not",
77-
)
78-
flag.Var(
83+
pflag.Lookup("critical").NoOptDefVal = "true"
84+
85+
pflag.Var(
7986
&nameStyle, "names",
8087
"display names of large objects in the specified `style`:\n"+
81-
" --names=none omit footnotes entirely\n"+
82-
" --names=hash show only the SHA-1s of objects\n"+
83-
" --names=full show full names",
88+
" --names=none omit footnotes entirely\n"+
89+
" --names=hash show only the SHA-1s of objects\n"+
90+
" --names=full show full names",
8491
)
85-
flag.BoolVar(&jsonOutput, "json", false, "output results in JSON format")
86-
flag.BoolVar(&jsonOutput, "j", false, "output results in JSON format")
92+
93+
pflag.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")
8794

8895
atty, err := isatty.Isatty(os.Stderr.Fd())
8996
if err != nil {
9097
atty = false
9198
}
99+
pflag.BoolVar(&progress, "progress", atty, "report progress to stderr")
100+
pflag.Var(&NegatedBoolValue{&progress}, "no-progress", "suppress progress output")
101+
pflag.Lookup("no-progress").NoOptDefVal = "true"
92102

93-
flag.BoolVar(&progress, "progress", atty, "report progress to stderr")
94-
flag.Var(&NegatedBoolValue{&progress}, "no-progress", "suppress progress output")
103+
pflag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
104+
pflag.CommandLine.MarkHidden("cpuprofile")
95105

96-
flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
106+
pflag.CommandLine.SortFlags = false
97107

98-
flag.Parse()
108+
pflag.Parse()
99109

100110
if cpuprofile != "" {
101111
f, err := os.Create(cpuprofile)
@@ -106,7 +116,7 @@ func mainImplementation() error {
106116
defer pprof.StopCPUProfile()
107117
}
108118

109-
args := flag.Args()
119+
args := pflag.Args()
110120

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

sizes/output.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package sizes
22

33
import (
44
"bytes"
5-
"flag"
65
"fmt"
76
"io"
87
"strconv"
98

109
"github.com/github/git-sizer/counts"
1110
"github.com/github/git-sizer/git"
11+
12+
"github.com/spf13/pflag"
1213
)
1314

1415
func (s BlobSize) String() string {
@@ -200,7 +201,7 @@ func (l *item) levelOfConcern(t *table) (string, bool) {
200201

201202
type Threshold float64
202203

203-
// Methods to implement flag.Value:
204+
// Methods to implement pflag.Value:
204205
func (t *Threshold) String() string {
205206
if t == nil {
206207
return "UNSET"
@@ -227,10 +228,14 @@ func (t *Threshold) Set(s string) error {
227228
return nil
228229
}
229230

230-
// A `flag.Value` that can be used as a boolean option that sets a
231+
func (t *Threshold) Type() string {
232+
return "threshold"
233+
}
234+
235+
// A `pflag.Value` that can be used as a boolean option that sets a
231236
// `Threshold` variable to a fixed value. For example,
232237
//
233-
// flag.Var(
238+
// pflag.Var(
234239
// sizes.NewThresholdFlagValue(&threshold, 30),
235240
// "critical", "only report critical statistics",
236241
// )
@@ -242,14 +247,10 @@ type thresholdFlagValue struct {
242247
value Threshold
243248
}
244249

245-
func NewThresholdFlagValue(threshold *Threshold, value Threshold) flag.Value {
250+
func NewThresholdFlagValue(threshold *Threshold, value Threshold) pflag.Value {
246251
return &thresholdFlagValue{false, threshold, value}
247252
}
248253

249-
func (v *thresholdFlagValue) IsBoolFlag() bool {
250-
return true
251-
}
252-
253254
func (v *thresholdFlagValue) String() string {
254255
return strconv.FormatBool(v.b)
255256
}
@@ -268,6 +269,10 @@ func (v *thresholdFlagValue) Set(s string) error {
268269
return nil
269270
}
270271

272+
func (v *thresholdFlagValue) Type() string {
273+
return "bool"
274+
}
275+
271276
type NameStyle int
272277

273278
const (
@@ -276,7 +281,7 @@ const (
276281
NameStyleFull
277282
)
278283

279-
// Methods to implement flag.Value:
284+
// Methods to implement pflag.Value:
280285
func (n *NameStyle) String() string {
281286
if n == nil {
282287
return "UNSET"
@@ -308,6 +313,10 @@ func (n *NameStyle) Set(s string) error {
308313
return nil
309314
}
310315

316+
func (n *NameStyle) Type() string {
317+
return "nameStyle"
318+
}
319+
311320
type table struct {
312321
contents tableContents
313322
threshold Threshold

vendor/github.com/spf13/pflag/LICENSE

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/spf13/pflag/bool.go

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)