~samwhited/cli

ref: e61e7fcaa94afc5f08431923c74acc39a79fecbc cli/help.go -rw-r--r-- 1.2 KiB
e61e7fcaSam Whited cli: fix typo in docs 1 year, 24 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package cli

import (
	"fmt"
)

// Help returns a Command that prints help information about its command set to
// the command's Help output, or information about a specific command if one is
// provided as an argument.
//
// For example, in a program called "git" running:
//
//     git help commit
//
// would print information about the "commit" subcommand.
func Help(cs *Command) *Command {
	return &Command{
		Usage:       "help [command]",
		Description: `Print articles and detailed information about subcommands.`,
		Run: func(c *Command, args ...string) error {
			// If there aren't any arguments, print the main command help.
			if len(args) == 0 {
				cs.Help()
				return nil
			}

			// Print the help for the provided subcommand or help topic.
			for _, cmd := range cs.Commands {
				if cmd.Name() != args[0] {
					continue
				}
				// If this is the article, run its help command.
				if len(args) == 1 {
					cmd.Help()
					return nil
				}

				// Recurse into subcommands:
				return Help(cmd).Run(cmd, args[1:]...)
			}
			return fmt.Errorf("unknown help topic")
		},
	}
}