2022-05-14 00:58:55 +00:00
|
|
|
package argh
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2022-05-19 00:15:31 +00:00
|
|
|
type Querier interface {
|
|
|
|
Program() (Program, bool)
|
|
|
|
TypedAST() []TypedNode
|
|
|
|
AST() []Node
|
2022-05-14 00:58:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 00:15:31 +00:00
|
|
|
func NewQuerier(pt *ParseTree) Querier {
|
|
|
|
return &defaultQuerier{pt: pt}
|
|
|
|
}
|
|
|
|
|
|
|
|
type defaultQuerier struct {
|
|
|
|
pt *ParseTree
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dq *defaultQuerier) Program() (Program, bool) {
|
|
|
|
if len(dq.pt.Nodes) == 0 {
|
|
|
|
return Program{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := dq.pt.Nodes[0].(Program)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dq *defaultQuerier) TypedAST() []TypedNode {
|
2022-05-14 00:58:55 +00:00
|
|
|
ret := []TypedNode{}
|
|
|
|
|
2022-05-19 00:15:31 +00:00
|
|
|
for _, node := range dq.pt.Nodes {
|
2022-05-14 00:58:55 +00:00
|
|
|
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-19 00:15:31 +00:00
|
|
|
func (dq *defaultQuerier) AST() []Node {
|
2022-05-15 00:58:09 +00:00
|
|
|
ret := []Node{}
|
|
|
|
|
2022-05-19 00:15:31 +00:00
|
|
|
for _, node := range dq.pt.Nodes {
|
2022-05-15 00:58:09 +00:00
|
|
|
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
|
|
|
|
}
|