MCPcopy
hub / github.com/perkeep/perkeep / Main

Function Main

pkg/cmdmain/cmdmain.go:253–333  ·  view source on GitHub ↗

Main is meant to be the core of a command that has subcommands (modes), such as pk-put or pk.

()

Source from the content-addressed store, hash-verified

251// Main is meant to be the core of a command that has
252// subcommands (modes), such as pk-put or pk.
253func Main() {
254 registerFlagOnce.Do(ExtraFlagRegistration)
255 if setCommandLineOutput != nil {
256 setCommandLineOutput(Stderr)
257 }
258 flag.Usage = func() {
259 usage("")
260 }
261 flag.Parse()
262 flag.CommandLine.SetOutput(Stderr)
263 PostFlag()
264
265 args := flag.Args()
266 if *FlagVersion {
267 fmt.Fprintf(Stderr, "%s version: %s\n", os.Args[0], buildinfo.Summary())
268 return
269 }
270 if *FlagHelp {
271 usage("")
272 }
273 if *FlagLegal {
274 PrintLicenses()
275 return
276 }
277 if len(args) == 0 {
278 usage("No mode given.")
279 }
280
281 mode := args[0]
282 cmd, ok := modeCommand[mode]
283 if !ok {
284 usage(fmt.Sprintf("Unknown mode %q", mode))
285 }
286
287 if _, ok := asNewCommand[mode]; ok {
288 runAsNewCommand(cmd, mode)
289 return
290 }
291
292 cmdFlags := modeFlags[mode]
293 cmdFlags.SetOutput(Stderr)
294 err := cmdFlags.Parse(args[1:])
295 if err != nil {
296 // We want -h to behave as -help, but without having to define another flag for
297 // it, so we handle it here.
298 // TODO(mpl): maybe even remove -help and just let them both be handled here?
299 if err == flag.ErrHelp {
300 help(mode)
301 return
302 }
303 err = ErrUsage
304 } else {
305 if *wantHelp[mode] {
306 help(mode)
307 return
308 }
309 err = cmd.RunCommand(cmdFlags.Args())
310 }

Callers 4

mainFunction · 0.92
mainFunction · 0.92
RunMethod · 0.92
mainFunction · 0.92

Calls 11

SummaryFunction · 0.92
PrintLicensesFunction · 0.85
runAsNewCommandFunction · 0.85
ErrorfFunction · 0.85
hasFlagsFunction · 0.85
PreExitFunction · 0.85
ParseMethod · 0.80
usageFunction · 0.70
helpFunction · 0.70
RunCommandMethod · 0.65
UsageMethod · 0.65

Tested by 1

RunMethod · 0.74