mirror of
https://github.com/rclone/rclone.git
synced 2024-11-25 09:54:44 +01:00
help: global flags help command extended filtering
This commit is contained in:
parent
c6352231e4
commit
1720d3e11c
18
cmd/help.go
18
cmd/help.go
@ -59,20 +59,25 @@ var helpCommand = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// to filter the flags with
|
// to filter the flags with
|
||||||
var flagsRe *regexp.Regexp
|
var (
|
||||||
|
filterFlagsGroup string
|
||||||
|
filterFlagsRe *regexp.Regexp
|
||||||
|
filterFlagsNamesOnly bool
|
||||||
|
)
|
||||||
|
|
||||||
// Show the flags
|
// Show the flags
|
||||||
var helpFlags = &cobra.Command{
|
var helpFlags = &cobra.Command{
|
||||||
Use: "flags [<filter>]",
|
Use: "flags [<filter>]",
|
||||||
Short: "Show the global flags for rclone",
|
Short: "Show the global flags for rclone",
|
||||||
Run: func(command *cobra.Command, args []string) {
|
Run: func(command *cobra.Command, args []string) {
|
||||||
|
command.Flags()
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
re, err := filter.GlobStringToRegexp(args[0], false)
|
re, err := filter.GlobStringToRegexp(args[0], false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to compile flags regexp: %v", err)
|
log.Fatalf("Invalid flag filter: %v", err)
|
||||||
}
|
}
|
||||||
fs.Debugf(nil, "Flags filter: %s", re.String())
|
fs.Debugf(nil, "Flag filter: %s", re.String())
|
||||||
flagsRe = re
|
filterFlagsRe = re
|
||||||
}
|
}
|
||||||
if GeneratingDocs {
|
if GeneratingDocs {
|
||||||
Root.SetUsageTemplate(docFlagsTemplate)
|
Root.SetUsageTemplate(docFlagsTemplate)
|
||||||
@ -159,7 +164,7 @@ func setupRootCommand(rootCmd *cobra.Command) {
|
|||||||
fs.Errorf(nil, "Flag --%s is unknown", flag.Name)
|
fs.Errorf(nil, "Flag --%s is unknown", flag.Name)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
groups := flags.All.Filter(flagsRe).Include(cmd.Annotations["groups"])
|
groups := flags.All.Filter(filterFlagsGroup, filterFlagsRe, filterFlagsNamesOnly).Include(cmd.Annotations["groups"])
|
||||||
return groups.Groups
|
return groups.Groups
|
||||||
})
|
})
|
||||||
rootCmd.SetUsageTemplate(usageTemplate)
|
rootCmd.SetUsageTemplate(usageTemplate)
|
||||||
@ -171,6 +176,9 @@ func setupRootCommand(rootCmd *cobra.Command) {
|
|||||||
|
|
||||||
rootCmd.AddCommand(helpCommand)
|
rootCmd.AddCommand(helpCommand)
|
||||||
helpCommand.AddCommand(helpFlags)
|
helpCommand.AddCommand(helpFlags)
|
||||||
|
helpFlagsFlags := helpFlags.Flags()
|
||||||
|
flags.StringVarP(helpFlagsFlags, &filterFlagsGroup, "group", "", "", "Only include flags from specific group", "")
|
||||||
|
flags.BoolVarP(helpFlagsFlags, &filterFlagsNamesOnly, "name", "", false, "Apply filter only on flag names", "")
|
||||||
helpCommand.AddCommand(helpBackends)
|
helpCommand.AddCommand(helpBackends)
|
||||||
helpCommand.AddCommand(helpBackend)
|
helpCommand.AddCommand(helpBackend)
|
||||||
|
|
||||||
|
@ -50,17 +50,18 @@ func (gs *Groups) NewGroup(name, help string) *Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Filter makes a copy of groups filtered by flagsRe
|
// Filter makes a copy of groups filtered by flagsRe
|
||||||
func (gs *Groups) Filter(flagsRe *regexp.Regexp) *Groups {
|
func (gs *Groups) Filter(group string, filterRe *regexp.Regexp, filterNamesOnly bool) *Groups {
|
||||||
newGs := NewGroups()
|
newGs := NewGroups()
|
||||||
for _, g := range gs.Groups {
|
for _, g := range gs.Groups {
|
||||||
|
if group == "" || strings.EqualFold(g.Name, group) {
|
||||||
newG := newGs.NewGroup(g.Name, g.Help)
|
newG := newGs.NewGroup(g.Name, g.Help)
|
||||||
g.Flags.VisitAll(func(f *pflag.Flag) {
|
g.Flags.VisitAll(func(f *pflag.Flag) {
|
||||||
matched := flagsRe == nil || flagsRe.MatchString(f.Name) || flagsRe.MatchString(f.Usage)
|
if filterRe == nil || filterRe.MatchString(f.Name) || (!filterNamesOnly && filterRe.MatchString(f.Usage)) {
|
||||||
if matched {
|
|
||||||
newG.Flags.AddFlag(f)
|
newG.Flags.AddFlag(f)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return newGs
|
return newGs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user