From a6526af6ff1b65e5855b4ddd6b10000a68b737c5 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 25 May 2022 22:34:32 -0400 Subject: [PATCH] Implement value capture for last compound short flag --- argh/parser2.go | 38 +++++++++++++++---------- argh/parser2_test.go | 66 ++++++++++++++++++++++++++++---------------- 2 files changed, 65 insertions(+), 39 deletions(-) diff --git a/argh/parser2.go b/argh/parser2.go index 8d35ffa..0a12726 100644 --- a/argh/parser2.go +++ b/argh/parser2.go @@ -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{} diff --git a/argh/parser2_test.go b/argh/parser2_test.go index cfa0837..f9e1370 100644 --- a/argh/parser2_test.go +++ b/argh/parser2_test.go @@ -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{}, + }, + }, + }, + }, + }, + }, + }, + }, }, }, },