2022-05-14 00:58:55 +00:00
|
|
|
package argh
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type ParseTree struct {
|
|
|
|
Nodes []Node `json:"nodes"`
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
func (pt *ParseTree) typedAST() []TypedNode {
|
2022-05-14 00:58:55 +00:00
|
|
|
ret := []TypedNode{}
|
|
|
|
|
|
|
|
for _, node := range pt.Nodes {
|
|
|
|
if _, ok := node.(ArgDelimiter); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := node.(StopFlag); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(
|
|
|
|
ret,
|
|
|
|
TypedNode{
|
|
|
|
Type: fmt.Sprintf("%T", node),
|
|
|
|
Node: node,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:58:09 +00:00
|
|
|
func (pt *ParseTree) ast() []Node {
|
|
|
|
ret := []Node{}
|
|
|
|
|
|
|
|
for _, node := range pt.Nodes {
|
|
|
|
if _, ok := node.(ArgDelimiter); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := node.(StopFlag); ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-15 18:22:56 +00:00
|
|
|
if v, ok := node.(CompoundShortFlag); ok {
|
2022-05-15 00:58:09 +00:00
|
|
|
for _, subNode := range v.Nodes {
|
|
|
|
ret = append(ret, subNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = append(ret, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|