Skip to content

Commit

Permalink
Merge pull request #8 from github/posix-flags
Browse files Browse the repository at this point in the history
Switch to using POSIX-style flags
  • Loading branch information
mhagger committed Mar 2, 2018
2 parents f0efe64 + f942a58 commit 6cda807
Show file tree
Hide file tree
Showing 32 changed files with 3,729 additions and 35 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
unused-packages = true
go-tests = true
non-go = true

[[constraint]]
name = "github.com/spf13/pflag"
version = "1.0.0"
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ releases/git-sizer-$$(VERSION)-$(1)-$(2).zip: bin/git-sizer-$(1)-$(2)$(3)
mkdir -p releases/tmp-$$(VERSION)-$(1)-$(2)
cp README.md LICENSE.md releases/tmp-$$(VERSION)-$(1)-$(2)
cp bin/git-sizer-$(1)-$(2)$(3) releases/tmp-$$(VERSION)-$(1)-$(2)/git-sizer$(3)
cp vendor/github.com/spf13/pflag/LICENSE releases/tmp-$$(VERSION)-$(1)-$(2)/LICENSE-spf13-pflag
rm -f $$@
zip -j $$@ releases/tmp-$$(VERSION)-$(1)-$(2)/*
rm -rf releases/tmp-$$(VERSION)-$(1)-$(2)
Expand Down
58 changes: 34 additions & 24 deletions git-sizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
Expand All @@ -13,6 +12,8 @@ import (
"github.com/github/git-sizer/git"
"github.com/github/git-sizer/isatty"
"github.com/github/git-sizer/sizes"

"github.com/spf13/pflag"
)

type NegatedBoolValue struct {
Expand All @@ -37,8 +38,8 @@ func (b *NegatedBoolValue) String() string {
}
}

func (v *NegatedBoolValue) IsBoolFlag() bool {
return true
func (v *NegatedBoolValue) Type() string {
return "bool"
}

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

flag.BoolVar(&processBranches, "branches", false, "process all branches")
flag.BoolVar(&processTags, "tags", false, "process all tags")
flag.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")
flag.Var(
pflag.BoolVar(&processBranches, "branches", false, "process all branches")
pflag.BoolVar(&processTags, "tags", false, "process all tags")
pflag.BoolVar(&processRemotes, "remotes", false, "process all remote-tracking branches")

pflag.VarP(
sizes.NewThresholdFlagValue(&threshold, 0),
"verbose", "v", "report all statistics, whether concerning or not",
)
pflag.Lookup("verbose").NoOptDefVal = "true"

pflag.Var(
&threshold, "threshold",
"minimum level of concern (i.e., number of stars) that should be\n"+
" reported",
" reported",
)
flag.Var(

pflag.Var(
sizes.NewThresholdFlagValue(&threshold, 30),
"critical", "only report critical statistics",
)
flag.Var(
sizes.NewThresholdFlagValue(&threshold, 0),
"verbose", "report all statistics, whether concerning or not",
)
flag.Var(
pflag.Lookup("critical").NoOptDefVal = "true"

pflag.Var(
&nameStyle, "names",
"display names of large objects in the specified `style`:\n"+
" --names=none omit footnotes entirely\n"+
" --names=hash show only the SHA-1s of objects\n"+
" --names=full show full names",
" --names=none omit footnotes entirely\n"+
" --names=hash show only the SHA-1s of objects\n"+
" --names=full show full names",
)
flag.BoolVar(&jsonOutput, "json", false, "output results in JSON format")
flag.BoolVar(&jsonOutput, "j", false, "output results in JSON format")

pflag.BoolVarP(&jsonOutput, "json", "j", false, "output results in JSON format")

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

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

flag.StringVar(&cpuprofile, "cpuprofile", "", "write cpu profile to file")
pflag.CommandLine.SortFlags = false

flag.Parse()
pflag.Parse()

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

args := flag.Args()
args := pflag.Args()

if len(args) != 0 {
return errors.New("excess arguments")
Expand Down
29 changes: 19 additions & 10 deletions sizes/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package sizes

import (
"bytes"
"flag"
"fmt"
"io"
"strconv"

"github.com/github/git-sizer/counts"
"github.com/github/git-sizer/git"

"github.com/spf13/pflag"
)

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

type Threshold float64

// Methods to implement flag.Value:
// Methods to implement pflag.Value:
func (t *Threshold) String() string {
if t == nil {
return "UNSET"
Expand All @@ -227,10 +228,14 @@ func (t *Threshold) Set(s string) error {
return nil
}

// A `flag.Value` that can be used as a boolean option that sets a
func (t *Threshold) Type() string {
return "threshold"
}

// A `pflag.Value` that can be used as a boolean option that sets a
// `Threshold` variable to a fixed value. For example,
//
// flag.Var(
// pflag.Var(
// sizes.NewThresholdFlagValue(&threshold, 30),
// "critical", "only report critical statistics",
// )
Expand All @@ -242,14 +247,10 @@ type thresholdFlagValue struct {
value Threshold
}

func NewThresholdFlagValue(threshold *Threshold, value Threshold) flag.Value {
func NewThresholdFlagValue(threshold *Threshold, value Threshold) pflag.Value {
return &thresholdFlagValue{false, threshold, value}
}

func (v *thresholdFlagValue) IsBoolFlag() bool {
return true
}

func (v *thresholdFlagValue) String() string {
return strconv.FormatBool(v.b)
}
Expand All @@ -268,6 +269,10 @@ func (v *thresholdFlagValue) Set(s string) error {
return nil
}

func (v *thresholdFlagValue) Type() string {
return "bool"
}

type NameStyle int

const (
Expand All @@ -276,7 +281,7 @@ const (
NameStyleFull
)

// Methods to implement flag.Value:
// Methods to implement pflag.Value:
func (n *NameStyle) String() string {
if n == nil {
return "UNSET"
Expand Down Expand Up @@ -308,6 +313,10 @@ func (n *NameStyle) Set(s string) error {
return nil
}

func (n *NameStyle) Type() string {
return "nameStyle"
}

type table struct {
contents tableContents
threshold Threshold
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/spf13/pflag/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions vendor/github.com/spf13/pflag/bool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6cda807

Please sign in to comment.