arghing #1
@ -55,6 +55,8 @@ func (p *parser2) parseArgs() (*ParseTree, error) {
|
|||||||
tracef("parseArgs() parsing %q as program command; cfg=%+#v", p.lit, p.cfg.Prog)
|
tracef("parseArgs() parsing %q as program command; cfg=%+#v", p.lit, p.cfg.Prog)
|
||||||
prog := p.parseCommand(&p.cfg.Prog)
|
prog := p.parseCommand(&p.cfg.Prog)
|
||||||
|
|
||||||
|
tracef("parseArgs() top level node is %T", prog)
|
||||||
|
|
||||||
nodes := []Node{prog}
|
nodes := []Node{prog}
|
||||||
if v := p.parsePassthrough(); v != nil {
|
if v := p.parsePassthrough(); v != nil {
|
||||||
tracef("parseArgs() appending passthrough argument %v", v)
|
tracef("parseArgs() appending passthrough argument %v", v)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package argh
|
package argh
|
||||||
|
|
||||||
type Querier interface {
|
type Querier interface {
|
||||||
Program() (Command, bool)
|
Program() (*Command, bool)
|
||||||
AST() []Node
|
AST() []Node
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -13,12 +13,19 @@ type defaultQuerier struct {
|
|||||||
nodes []Node
|
nodes []Node
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dq *defaultQuerier) Program() (Command, bool) {
|
func (dq *defaultQuerier) Program() (*Command, bool) {
|
||||||
if len(dq.nodes) == 0 {
|
if len(dq.nodes) == 0 {
|
||||||
return Command{}, false
|
tracef("Program nodes are empty")
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
tracef("Program node[0] is %T", dq.nodes[0])
|
||||||
|
|
||||||
|
v, ok := dq.nodes[0].(*Command)
|
||||||
|
if ok && v.Name == "" {
|
||||||
|
return v, false
|
||||||
}
|
}
|
||||||
|
|
||||||
v, ok := dq.nodes[0].(Command)
|
|
||||||
return v, ok
|
return v, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,41 +12,34 @@ func TestQuerier_Program(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
args []string
|
args []string
|
||||||
cfg *argh.ParserConfig
|
cfg *argh.ParserConfig
|
||||||
exp argh.Command
|
exp string
|
||||||
expOK bool
|
expOK bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "typical",
|
name: "typical",
|
||||||
args: []string{"pizzas", "ahoy", "--treatsa", "fun"},
|
args: []string{"pizzas", "ahoy", "--treatsa", "fun"},
|
||||||
exp: argh.Command{Name: "pizzas"},
|
exp: "pizzas",
|
||||||
expOK: true,
|
expOK: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "minimal",
|
name: "minimal",
|
||||||
args: []string{"pizzas"},
|
args: []string{"pizzas"},
|
||||||
exp: argh.Command{Name: "pizzas"},
|
exp: "pizzas",
|
||||||
expOK: true,
|
expOK: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "invalid",
|
name: "invalid",
|
||||||
args: []string{},
|
args: []string{},
|
||||||
exp: argh.Command{},
|
|
||||||
expOK: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid flag only",
|
|
||||||
args: []string{"--oh-no"},
|
|
||||||
exp: argh.Command{},
|
|
||||||
expOK: false,
|
expOK: false,
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(ct *testing.T) {
|
t.Run(tc.name, func(ct *testing.T) {
|
||||||
pt, err := argh.ParseArgs(tc.args, tc.cfg)
|
pt, err := argh.ParseArgs2(tc.args, tc.cfg)
|
||||||
require.Nil(ct, err)
|
require.Nil(ct, err)
|
||||||
|
|
||||||
prog, ok := argh.NewQuerier(pt.Nodes).Program()
|
prog, ok := argh.NewQuerier(pt.Nodes).Program()
|
||||||
require.Equal(ct, tc.exp, prog)
|
|
||||||
require.Equal(ct, tc.expOK, ok)
|
require.Equal(ct, tc.expOK, ok)
|
||||||
|
require.Equal(ct, tc.exp, prog.Name)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user