aboutsummaryrefslogtreecommitdiffhomepage
path: root/command/node.go
blob: 0dcdf162ecc8e35214ed6c4df6a75cde1b3fed47 (plain)
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
package command

import (
	"flag"
	"io"
)

// A node represents a command.
type node struct {
	child, next *node
	name, usage string

	out  io.Writer
	logf LogFunc

	// Names of commands preceding node.
	prefix []string
	// Short user-facing representations of flags received by node.
	suffix []string

	f   HandlerFunc
	set *flag.FlagSet
}

// adopt adds v as the last child of n.
func (n *node) adopt(v *node) bool {
	if n.child != nil {
		return n.child.append(v)
	}
	n.child = v
	return true
}

// append adds v as the last sibling of n.
func (n *node) append(v *node) bool {
	if n.name == v.name {
		return false
	}
	if n.next != nil {
		return n.next.append(v)
	}
	n.next = v
	return true
}