MCPcopy Index your code
hub / github.com/rclone/rclone / setupRootCommand

Function setupRootCommand

cmd/help.go:133–192  ·  view source on GitHub ↗

setupRootCommand sets default usage, help, and error handling for the root command. Helpful example: https://github.com/moby/moby/blob/master/cli/cobra.go

(rootCmd *cobra.Command)

Source from the content-addressed store, hash-verified

131//
132// Helpful example: https://github.com/moby/moby/blob/master/cli/cobra.go
133func setupRootCommand(rootCmd *cobra.Command) {
134 ci := fs.GetConfig(context.Background())
135 // Add global flags
136 configflags.AddFlags(ci, pflag.CommandLine)
137 filterflags.AddFlags(pflag.CommandLine)
138 rcflags.AddFlags(pflag.CommandLine)
139 logflags.AddFlags(pflag.CommandLine)
140
141 Root.Run = runRoot
142 Root.Flags().BoolVarP(&version, "version", "V", false, "Print the version number")
143
144 cobra.AddTemplateFunc("showGlobalFlags", func(cmd *cobra.Command) bool {
145 return cmd.CalledAs() == "flags" || cmd.Annotations["groups"] != ""
146 })
147 cobra.AddTemplateFunc("showCommands", func(cmd *cobra.Command) bool {
148 return cmd.CalledAs() != "flags"
149 })
150 cobra.AddTemplateFunc("showLocalFlags", func(cmd *cobra.Command) bool {
151 // Don't show local flags (which are the global ones on the root) on "rclone" and
152 // "rclone help" (which shows the global help)
153 return cmd.CalledAs() != "rclone" && cmd.CalledAs() != ""
154 })
155 cobra.AddTemplateFunc("flagGroups", func(cmd *cobra.Command) []*flags.Group {
156 // Add the backend flags and check all flags
157 backendGroup := flags.All.NewGroup("Backend", "Backend-only flags (these can be set in the config file also)")
158 allRegistered := flags.All.AllRegistered()
159 cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
160 if _, ok := backendFlags[flag.Name]; ok {
161 backendGroup.Add(flag)
162 } else if _, ok := allRegistered[flag]; ok {
163 // flag is in a group already
164 } else {
165 fs.Errorf(nil, "Flag --%s is unknown", flag.Name)
166 }
167 })
168 groups := flags.All.Filter(filterFlagsGroup, filterFlagsRe, filterFlagsNamesOnly).Include(cmd.Annotations["groups"])
169 return groups.Groups
170 })
171 rootCmd.SetUsageTemplate(usageTemplate)
172 // rootCmd.SetHelpTemplate(helpTemplate)
173 // rootCmd.SetFlagErrorFunc(FlagErrorFunc)
174 rootCmd.SetHelpCommand(helpCommand)
175 // rootCmd.PersistentFlags().BoolP("help", "h", false, "Print usage")
176 // rootCmd.PersistentFlags().MarkShorthandDeprecated("help", "please use --help")
177
178 rootCmd.AddCommand(helpCommand)
179 helpCommand.AddCommand(helpFlags)
180 helpFlagsFlags := helpFlags.Flags()
181 flags.StringVarP(helpFlagsFlags, &filterFlagsGroup, "group", "", "", "Only include flags from specific group", "")
182 flags.BoolVarP(helpFlagsFlags, &filterFlagsNamesOnly, "name", "", false, "Apply filter only on flag names", "")
183 helpCommand.AddCommand(helpBackends)
184 helpCommand.AddCommand(helpBackend)
185
186 // Set command completion for all functions to be the same
187 traverseCommands(rootCmd, func(cmd *cobra.Command) {
188 cmd.ValidArgsFunction = validArgs
189 })
190

Callers 1

MainFunction · 0.85

Calls 14

AddMethod · 0.95
GetConfigFunction · 0.92
AddFlagsFunction · 0.92
AddFlagsFunction · 0.92
AddFlagsFunction · 0.92
AddFlagsFunction · 0.92
ErrorfFunction · 0.92
StringVarPFunction · 0.92
BoolVarPFunction · 0.92
traverseCommandsFunction · 0.85
NewGroupMethod · 0.80
AllRegisteredMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…