Show global options in usage (#1516)
This commit is contained in:
@@ -53,15 +53,6 @@ func ActsAsRootCommand(cmd *cobra.Command, filters []string, groups ...CommandGr
|
|||||||
return templater
|
return templater
|
||||||
}
|
}
|
||||||
|
|
||||||
func UseOptionsTemplates(cmd *cobra.Command) {
|
|
||||||
templater := &templater{
|
|
||||||
UsageTemplate: OptionsUsageTemplate(),
|
|
||||||
HelpTemplate: OptionsHelpTemplate(),
|
|
||||||
}
|
|
||||||
cmd.SetUsageFunc(templater.UsageFunc())
|
|
||||||
cmd.SetHelpFunc(templater.HelpFunc())
|
|
||||||
}
|
|
||||||
|
|
||||||
type templater struct {
|
type templater struct {
|
||||||
UsageTemplate string
|
UsageTemplate string
|
||||||
HelpTemplate string
|
HelpTemplate string
|
||||||
@@ -73,13 +64,8 @@ type templater struct {
|
|||||||
func (templater *templater) FlagErrorFunc(exposedFlags ...string) func(*cobra.Command, error) error {
|
func (templater *templater) FlagErrorFunc(exposedFlags ...string) func(*cobra.Command, error) error {
|
||||||
return func(c *cobra.Command, err error) error {
|
return func(c *cobra.Command, err error) error {
|
||||||
c.SilenceUsage = true
|
c.SilenceUsage = true
|
||||||
switch c.CalledAs() {
|
|
||||||
case "options":
|
|
||||||
return fmt.Errorf("%s\nRun '%s' without flags", err, c.CommandPath())
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("%s\nSee '%s --help' for usage", err, c.CommandPath())
|
return fmt.Errorf("%s\nSee '%s --help' for usage", err, c.CommandPath())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (templater *templater) ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer {
|
func (templater *templater) ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer {
|
||||||
@@ -211,12 +197,19 @@ func (t *templater) optionsCmdFor(c *cobra.Command) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *templater) usageLine(c *cobra.Command) string {
|
func (t *templater) usageLine(c *cobra.Command) string {
|
||||||
usage := c.UseLine()
|
var useline string
|
||||||
suffix := "[options]"
|
if c.HasParent() {
|
||||||
if c.HasFlags() && !strings.Contains(usage, suffix) {
|
useline = c.Parent().CommandPath() + " " + c.Use
|
||||||
usage += " " + suffix
|
} else {
|
||||||
|
useline = c.Use
|
||||||
}
|
}
|
||||||
return usage
|
if c.DisableFlagsInUseLine {
|
||||||
|
return useline
|
||||||
|
}
|
||||||
|
if c.HasAvailableFlags() && !strings.Contains(useline, "[options]") {
|
||||||
|
useline += " [options]"
|
||||||
|
}
|
||||||
|
return useline
|
||||||
}
|
}
|
||||||
|
|
||||||
func flagsUsages(f *flag.FlagSet) string {
|
func flagsUsages(f *flag.FlagSet) string {
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ const (
|
|||||||
`{{$rootCmd := rootCmd .}}` +
|
`{{$rootCmd := rootCmd .}}` +
|
||||||
`{{$visibleFlags := visibleFlags (flagsNotIntersected .LocalFlags .PersistentFlags)}}` +
|
`{{$visibleFlags := visibleFlags (flagsNotIntersected .LocalFlags .PersistentFlags)}}` +
|
||||||
`{{$explicitlyExposedFlags := exposed .}}` +
|
`{{$explicitlyExposedFlags := exposed .}}` +
|
||||||
`{{$optionsCmdFor := optionsCmdFor .}}` +
|
|
||||||
`{{$usageLine := usageLine .}}`
|
`{{$usageLine := usageLine .}}`
|
||||||
|
|
||||||
// SectionAliases is the help template section that displays command aliases.
|
// SectionAliases is the help template section that displays command aliases.
|
||||||
@@ -50,25 +49,25 @@ const (
|
|||||||
{{end}}`
|
{{end}}`
|
||||||
|
|
||||||
// SectionFlags is the help template section that displays the command's flags.
|
// SectionFlags is the help template section that displays the command's flags.
|
||||||
SectionFlags = `{{ if or $visibleFlags.HasFlags $explicitlyExposedFlags.HasFlags}}Options
|
SectionFlags = `{{ if $visibleFlags.HasFlags}}Options:
|
||||||
{{ if $visibleFlags.HasFlags}}{{trimRight (flagsUsages $visibleFlags)}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{ if $visibleFlags.HasFlags}}
|
{{trimRight (flagsUsages $visibleFlags)}}
|
||||||
{{end}}{{trimRight (flagsUsages $explicitlyExposedFlags)}}{{end}}
|
|
||||||
|
{{end}}`
|
||||||
|
|
||||||
|
// SectionGlobalFlags is the help template section that displays the command's global flags.
|
||||||
|
SectionGlobalFlags = `{{ if and (not $isRootCmd) (not .HasSubCommands) }}{{ if $explicitlyExposedFlags.HasFlags}}Global Options:
|
||||||
|
{{trimRight (flagsUsages $explicitlyExposedFlags)}}{{end}}
|
||||||
|
|
||||||
{{end}}`
|
{{end}}`
|
||||||
|
|
||||||
// SectionUsage is the help template section that displays the command's usage.
|
// SectionUsage is the help template section that displays the command's usage.
|
||||||
SectionUsage = `{{if and .Runnable (ne .UseLine "") (ne .UseLine $rootCmd)}}Usage:
|
SectionUsage = `{{if and .Runnable (ne .UseLine "") (ne .UseLine $rootCmd)}}Usage:
|
||||||
{{$usageLine}}
|
{{$usageLine}}
|
||||||
|
|
||||||
{{end}}`
|
{{end}}`
|
||||||
|
|
||||||
// SectionTipsHelp is the help template section that displays the '--help' hint.
|
// SectionTipsHelp is the help template section that displays the '--help' hint.
|
||||||
SectionTipsHelp = `{{if .HasSubCommands}}Use "{{$rootCmd}} <command> --help" for more information about a given command.
|
SectionTipsHelp = `{{if .HasSubCommands}}Use "{{$rootCmd}} <command> --help" for more information about a given command.
|
||||||
{{end}}`
|
{{end}}`
|
||||||
|
|
||||||
// SectionTipsGlobalOptions is the help template section that displays the 'options' hint for displaying global flags.
|
|
||||||
SectionTipsGlobalOptions = `{{if $optionsCmdFor}}Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).
|
|
||||||
{{end}}`
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MainHelpTemplate if the template for 'help' used by most commands.
|
// MainHelpTemplate if the template for 'help' used by most commands.
|
||||||
@@ -86,21 +85,9 @@ func MainUsageTemplate() string {
|
|||||||
SectionExamples,
|
SectionExamples,
|
||||||
SectionSubcommands,
|
SectionSubcommands,
|
||||||
SectionFlags,
|
SectionFlags,
|
||||||
|
SectionGlobalFlags,
|
||||||
SectionUsage,
|
SectionUsage,
|
||||||
SectionTipsHelp,
|
SectionTipsHelp,
|
||||||
SectionTipsGlobalOptions,
|
|
||||||
}
|
}
|
||||||
return strings.TrimRightFunc(strings.Join(sections, ""), unicode.IsSpace)
|
return strings.TrimRightFunc(strings.Join(sections, ""), unicode.IsSpace)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OptionsHelpTemplate if the template for 'help' used by the 'options' command.
|
|
||||||
func OptionsHelpTemplate() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// OptionsUsageTemplate if the template for 'usage' used by the 'options' command.
|
|
||||||
func OptionsUsageTemplate() string {
|
|
||||||
return `{{ if .HasInheritedFlags}}The following options can be passed to any command:
|
|
||||||
|
|
||||||
{{flagsUsages .InheritedFlags}}{{end}}`
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user