Implement value capture for last compound short flag

This commit is contained in:
Dan Buch 2022-05-25 22:34:32 -04:00
parent 3ea30a997a
commit a6526af6ff
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 65 additions and 39 deletions

View File

@ -209,6 +209,29 @@ func (p *parser2) parseLongFlag(flCfgMap map[string]FlagConfig) Node {
return p.parseConfiguredFlag(node, flCfg)
}
func (p *parser2) parseCompoundShortFlag(flCfgMap map[string]FlagConfig) Node {
flagNodes := []Node{}
withoutFlagPrefix := p.lit[1:]
for i, r := range withoutFlagPrefix {
node := &Flag{Name: string(r)}
if i == len(withoutFlagPrefix)-1 {
flCfg, ok := flCfgMap[node.Name]
if ok {
flagNodes = append(flagNodes, p.parseConfiguredFlag(node, flCfg))
continue
}
}
flagNodes = append(flagNodes, node)
}
return &CompoundShortFlag{Nodes: flagNodes}
}
func (p *parser2) parseConfiguredFlag(node *Flag, flCfg FlagConfig) Node {
values := map[string]string{}
nodes := []Node{}
@ -271,21 +294,6 @@ func (p *parser2) parseConfiguredFlag(node *Flag, flCfg FlagConfig) Node {
return node
}
func (p *parser2) parseCompoundShortFlag(flCfgMap map[string]FlagConfig) Node {
flagNodes := []Node{}
withoutFlagPrefix := p.lit[1:]
for i, r := range withoutFlagPrefix {
if i == len(withoutFlagPrefix)-1 {
tracef("parseCompoundShortFlag(...) TODO capture flag value(s)")
}
flagNodes = append(flagNodes, &Flag{Name: string(r)})
}
return &CompoundShortFlag{Nodes: flagNodes}
}
func (p *parser2) parsePassthrough() Node {
nodes := []Node{}

View File

@ -413,10 +413,8 @@ func TestParser2(t *testing.T) {
},
},
{
skip: true,
name: "command specific flags",
args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt"},
args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt", "hugs"},
cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{
Commands: map[string]argh.CommandConfig{
@ -424,13 +422,15 @@ func TestParser2(t *testing.T) {
Flags: map[string]argh.FlagConfig{
"freely": {},
},
},
"fry": argh.CommandConfig{
Flags: map[string]argh.FlagConfig{
"deeply": {},
"w": {},
"A": {},
"t": {},
Commands: map[string]argh.CommandConfig{
"fry": argh.CommandConfig{
Flags: map[string]argh.FlagConfig{
"deeply": {},
"w": {},
"A": {},
"t": argh.FlagConfig{NValue: 1},
},
},
},
},
},
@ -438,21 +438,39 @@ func TestParser2(t *testing.T) {
},
},
expPT: []argh.Node{
argh.Command{Name: "pizzas"},
argh.ArgDelimiter{},
argh.Command{Name: "fly"},
argh.ArgDelimiter{},
argh.Flag{Name: "freely"},
argh.ArgDelimiter{},
argh.Command{Name: "fry"},
argh.ArgDelimiter{},
argh.Flag{Name: "deeply"},
argh.ArgDelimiter{},
argh.CompoundShortFlag{
&argh.Command{
Name: "pizzas",
Nodes: []argh.Node{
argh.Flag{Name: "w"},
argh.Flag{Name: "A"},
argh.Flag{Name: "t"},
&argh.ArgDelimiter{},
&argh.Command{
Name: "fly",
Nodes: []argh.Node{
&argh.ArgDelimiter{},
&argh.Flag{Name: "freely"},
&argh.ArgDelimiter{},
&argh.Command{
Name: "fry",
Nodes: []argh.Node{
&argh.ArgDelimiter{},
&argh.Flag{Name: "deeply"},
&argh.ArgDelimiter{},
&argh.CompoundShortFlag{
Nodes: []argh.Node{
&argh.Flag{Name: "w"},
&argh.Flag{Name: "A"},
&argh.Flag{
Name: "t",
Values: map[string]string{"0": "hugs"},
Nodes: []argh.Node{
&argh.ArgDelimiter{},
},
},
},
},
},
},
},
},
},
},
},