Wrap flag usage to terminal size (#351)

* Wrap flag usage to terminal size

* Revert downgraded deps

* New vendored files I missed before
This commit is contained in:
Naomi Seyfer 2019-08-10 01:47:05 -07:00 committed by Knative Prow Robot
parent 284238d4f6
commit 260411724c
2 changed files with 15 additions and 0 deletions

1
go.mod
View File

@ -28,6 +28,7 @@ require (
github.com/spf13/cobra v0.0.3 github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.3.1 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/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect

View File

@ -32,6 +32,7 @@ import (
homedir "github.com/mitchellh/go-homedir" homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "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/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
) )
@ -140,6 +141,14 @@ func NewKnCommand(params ...commands.KnParams) *cobra.Command {
// Deal with empty and unknown sub command groups // Deal with empty and unknown sub command groups
EmptyAndUnknownSubCommands(rootCmd) 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. // For glog parse error.
flag.CommandLine.Parse([]string{}) flag.CommandLine.Parse([]string{})
@ -256,3 +265,8 @@ func removeKnPluginFlags(args []string) []string {
return remainingArgs return remainingArgs
} }
func width() (int, error) {
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))
return width, err
}