cmd: split flags up into global and backend flags #2541

This commit is contained in:
Nick Craig-Wood 2018-09-13 08:30:16 +01:00
parent 3c086f5f7f
commit 5fde7d8b12
2 changed files with 17 additions and 1 deletions

View File

@ -432,8 +432,11 @@ func resolveExitCode(err error) {
}
}
var backendFlags map[string]struct{}
// AddBackendFlags creates flags for all the backend options
func AddBackendFlags() {
backendFlags = map[string]struct{}{}
for _, fsInfo := range fs.Registry {
done := map[string]struct{}{}
for i := range fsInfo.Options {
@ -464,6 +467,7 @@ func AddBackendFlags() {
if opt.Hide&fs.OptionHideCommandLine != 0 {
flag.Hidden = true
}
backendFlags[name] = struct{}{}
} else {
fs.Errorf(nil, "Not adding duplicate flag --%s", name)
}

View File

@ -83,6 +83,15 @@ func setupRootCommand(rootCmd *cobra.Command) {
cobra.AddTemplateFunc("showLocalFlags", func(cmd *cobra.Command) bool {
return cmd.CalledAs() != "rclone"
})
cobra.AddTemplateFunc("backendFlags", func(cmd *cobra.Command, include bool) *pflag.FlagSet {
backendFlagSet := pflag.NewFlagSet("Backend Flags", pflag.ExitOnError)
cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
if _, ok := backendFlags[flag.Name]; ok == include {
backendFlagSet.AddFlag(flag)
}
})
return backendFlagSet
})
rootCmd.SetUsageTemplate(usageTemplate)
// rootCmd.SetHelpTemplate(helpTemplate)
// rootCmd.SetFlagErrorFunc(FlagErrorFunc)
@ -116,7 +125,10 @@ Flags:
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if and (showGlobalFlags .) .HasAvailableInheritedFlags}}
Global Flags:
{{.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
{{(backendFlags . false).FlagUsages | trimTrailingWhitespaces}}
Backend Flags:
{{(backendFlags . true).FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasHelpSubCommands}}
Additional help topics:{{range .Commands}}{{if .IsAdditionalHelpTopicCommand}}
{{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}