Skip to content

Commit

Permalink
Wrap flag usage to terminal size (#351)
Browse files Browse the repository at this point in the history
* Wrap flag usage to terminal size

* Revert downgraded deps

* New vendored files I missed before
  • Loading branch information
sixolet authored and knative-prow-robot committed Aug 10, 2019
1 parent 284238d commit 2604117
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.3.1
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
Expand Down
14 changes: 14 additions & 0 deletions pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
Expand Down Expand Up @@ -140,6 +141,14 @@ func NewKnCommand(params ...commands.KnParams) *cobra.Command {
// Deal with empty and unknown sub command groups
EmptyAndUnknownSubCommands(rootCmd)

// Wrap usage.
w, err := width()
if err == nil {
newUsage := strings.ReplaceAll(rootCmd.UsageTemplate(), "FlagUsages ",
fmt.Sprintf("FlagUsagesWrapped %d ", w))
rootCmd.SetUsageTemplate(newUsage)
}

// For glog parse error.
flag.CommandLine.Parse([]string{})

Expand Down Expand Up @@ -256,3 +265,8 @@ func removeKnPluginFlags(args []string) []string {

return remainingArgs
}

func width() (int, error) {
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
return width, err
}

0 comments on commit 2604117

Please sign in to comment.