Add support for setting variables via tag.

This provides much more convenient composition when reusing structs in
different parts of the command grammar.

eg.

	type Embedded struct {
		Key string `help:"A key from ${where}."`
	}

	var cli struct {
		Embedded `set:"where=somewhere"`
	}
This commit is contained in:
Alec Thomas
2018-09-19 12:56:12 +10:00
parent 467352418f
commit 2e29ff8981
9 changed files with 134 additions and 50 deletions
+9
View File
@@ -43,6 +43,7 @@ type Node struct {
Positional []*Positional
Children []*Node
Target reflect.Value // Pointer to the value in the grammar that this Node is associated with.
Tag *Tag
Argument *Value // Populated when Type is ArgumentNode.
}
@@ -171,6 +172,14 @@ func (n *Node) FullPath() string {
return strings.TrimSpace(root.Name + " " + n.Path())
}
// Vars returns the combined Vars defined by all ancestors of this Node.
func (n *Node) Vars() Vars {
if n == nil {
return Vars{}
}
return n.Parent.Vars().CloneWith(n.Tag.Vars)
}
// Path through ancestors to this Node.
func (n *Node) Path() (out string) {
if n.Parent != nil {