Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): order flags by categories #1168

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pkg/cfg/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cfg

import (
"flag"
"fmt"
"os"
"sort"
"strings"

"github.com/cortexproject/cortex/pkg/util/flagext"
"github.com/pkg/errors"
Expand Down Expand Up @@ -31,6 +34,7 @@ func dDefaults(fs *flag.FlagSet) Source {
// Flags parses the flag from the command line, setting only user-supplied
// values on the flagext.Registerer passed to Defaults()
func Flags() Source {
flag.Usage = categorizedUsage(flag.CommandLine)
return dFlags(flag.CommandLine, os.Args[1:])
}

Expand All @@ -41,3 +45,64 @@ func dFlags(fs *flag.FlagSet, args []string) Source {
return fs.Parse(args)
}
}

func categorizedUsage(fs *flag.FlagSet) func() {
categories := make(map[string][]string)
return func() {
if fs.Name() == "" {
fmt.Fprintf(fs.Output(), "Usage:\n")
} else {
fmt.Fprintf(fs.Output(), "Usage of %s:\n", fs.Name())
}

fs.VisitAll(func(f *flag.Flag) {
id := ""
if strings.Contains(f.Name, ".") {
id = strings.Split(f.Name, ".")[0]
}

kind, usage := flag.UnquoteUsage(f)
if kind != "" {
kind = " " + kind
}
def := f.DefValue
if def != "" {
def = fmt.Sprintf(" (default %s)", def)
}
categories[id] = append(categories[id], fmt.Sprintf(" -%s%s:\n %s%s", f.Name, kind, usage, def))
})

for name, flags := range categories {
if len(flags) == 1 {
categories[""] = append(categories[""], flags[0])
delete(categories, name)
}
}

for name := range categories {
sort.Strings(categories[name])
}

for _, u := range categories[""] {
fmt.Fprintln(fs.Output(), u)
}
fmt.Fprintln(fs.Output())

keys := make([]string, 0, len(categories))
for k := range categories {
keys = append(keys, k)
}
sort.Strings(keys)

for _, name := range keys {
if name == "" {
continue
}
fmt.Fprintf(fs.Output(), " %s:\n", strings.Title(name))
for _, u := range categories[name] {
fmt.Fprintln(fs.Output(), u)
}
fmt.Fprintln(fs.Output())
}
}
}