You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
argh/ast.go

51 lines
852 B

package argh
// ToAST accepts a slice of nodes as expected from ParseArgs and
// returns an AST with parse-time artifacts dropped and reorganized
// where applicable.
func ToAST(parseTree []Node) []Node {
ret := []Node{}
for i, node := range parseTree {
tracef("ToAST i=%d node type=%T", i, node)
if _, ok := node.(*ArgDelimiter); ok {
continue
}
if _, ok := node.(*StopFlag); ok {
continue
}
if v, ok := node.(*CompoundShortFlag); ok {
if v.Nodes != nil {
ret = append(ret, ToAST(v.Nodes)...)
}
continue
}
if v, ok := node.(*CommandFlag); ok {
astNodes := ToAST(v.Nodes)
if len(astNodes) == 0 {
astNodes = nil
}
ret = append(
ret,
&CommandFlag{
Name: v.Name,
Values: v.Values,
Nodes: astNodes,
})
continue
}
ret = append(ret, node)
}
return ret
}