From 30449ba506cb7c3d59df766e232009f30ac96397 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 7 Jun 2022 09:47:12 -0400 Subject: [PATCH] Pointers and nils again --- cmd/argh-example/main.go | 2 +- parser.go | 2 +- parser_config.go | 13 +++---------- parser_test.go | 32 ++++++++++++++++---------------- querier_test.go | 2 +- 5 files changed, 22 insertions(+), 29 deletions(-) diff --git a/cmd/argh-example/main.go b/cmd/argh-example/main.go index 4e192e9..0b243e2 100644 --- a/cmd/argh-example/main.go +++ b/cmd/argh-example/main.go @@ -16,7 +16,7 @@ func main() { log.SetFlags(0) pCfg := argh.NewParserConfig() - pCfg.Prog = argh.CommandConfig{ + pCfg.Prog = &argh.CommandConfig{ NValue: argh.OneOrMoreValue, ValueNames: []string{"topping"}, Flags: &argh.Flags{ diff --git a/parser.go b/parser.go index 72c15d9..4fcb4c5 100644 --- a/parser.go +++ b/parser.go @@ -61,7 +61,7 @@ func (p *parser) parseArgs() (*ParseTree, error) { } 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) diff --git a/parser_config.go b/parser_config.go index 65629da..0226425 100644 --- a/parser_config.go +++ b/parser_config.go @@ -27,7 +27,7 @@ func (nv NValue) Contains(i int) bool { } type ParserConfig struct { - Prog CommandConfig + Prog *CommandConfig ScannerConfig *ScannerConfig } @@ -43,8 +43,8 @@ func NewParserConfig(opts ...ParserOption) *ParserConfig { } } - if pCfg.Prog.IsZero() { - pCfg.Prog = CommandConfig{} + if pCfg.Prog == nil { + pCfg.Prog = &CommandConfig{} pCfg.Prog.init() } @@ -62,13 +62,6 @@ type CommandConfig struct { Commands *Commands } -func (cCfg *CommandConfig) IsZero() bool { - return cCfg.NValue == NValue(0) && - cCfg.ValueNames == nil && - cCfg.Flags == nil && - cCfg.Commands == nil -} - func (cCfg *CommandConfig) init() { if cCfg.ValueNames == nil { cCfg.ValueNames = []string{} diff --git a/parser_test.go b/parser_test.go index bea803f..a206d3b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -24,7 +24,7 @@ func TestParser(t *testing.T) { "pies", "-eat", "--wat", "hello", "mario", }, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "e": {}, @@ -98,8 +98,8 @@ func TestParser(t *testing.T) { "pies", "--wat", "hello", "mario", "-eat", }, cfg: &argh.ParserConfig{ - Prog: func() argh.CommandConfig { - cmdCfg := argh.CommandConfig{ + Prog: func() *argh.CommandConfig { + cmdCfg := &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "e": {Persist: true}, @@ -193,7 +193,7 @@ func TestParser(t *testing.T) { name: "one positional arg", args: []string{"pizzas", "excel"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{NValue: 1}, + Prog: &argh.CommandConfig{NValue: 1}, }, expPT: []argh.Node{ &argh.Command{ @@ -219,7 +219,7 @@ func TestParser(t *testing.T) { name: "many positional args", args: []string{"pizzas", "excel", "wildly", "when", "feral"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ NValue: argh.OneOrMoreValue, ValueNames: []string{"word"}, }, @@ -267,7 +267,7 @@ func TestParser(t *testing.T) { name: "long value-less flags", args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "tasty": {}, @@ -312,7 +312,7 @@ func TestParser(t *testing.T) { "--please", }, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}}, Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ @@ -391,7 +391,7 @@ func TestParser(t *testing.T) { name: "short value-less flags", args: []string{"pizzas", "-t", "-f", "-s"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "t": {}, @@ -429,7 +429,7 @@ func TestParser(t *testing.T) { name: "compound short flags", args: []string{"pizzas", "-aca", "-blol"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "a": {}, @@ -484,7 +484,7 @@ func TestParser(t *testing.T) { name: "mixed long short value flags", args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}}, Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ @@ -549,7 +549,7 @@ func TestParser(t *testing.T) { name: "nested commands with positional args", args: []string{"pizzas", "fly", "freely", "sometimes", "and", "other", "times", "fry", "deeply", "--forever"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Commands: &argh.Commands{ Map: map[string]argh.CommandConfig{ "fly": argh.CommandConfig{ @@ -632,7 +632,7 @@ func TestParser(t *testing.T) { name: "compound flags with values", args: []string{"pizzas", "-need", "sauce", "heat", "love", "-also", "over9000"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "a": {NValue: argh.ZeroOrMoreValue}, @@ -735,7 +735,7 @@ func TestParser(t *testing.T) { name: "command specific flags", args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt", "hugs"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Commands: &argh.Commands{ Map: map[string]argh.CommandConfig{ "fly": argh.CommandConfig{ @@ -835,7 +835,7 @@ func TestParser(t *testing.T) { name: "total weirdo", args: []string{"PIZZAs", "^wAT@golf", "^^hecKing", "goose", "bonk", "^^FIERCENESS@-2"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Commands: &argh.Commands{ Map: map[string]argh.CommandConfig{ "goose": argh.CommandConfig{ @@ -910,7 +910,7 @@ func TestParser(t *testing.T) { name: "windows like", args: []string{"hotdog", "/f", "/L", "/o:ppy", "hats"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "f": {}, @@ -957,7 +957,7 @@ func TestParser(t *testing.T) { name: "invalid bare assignment", args: []string{"pizzas", "=", "--wat"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Flags: &argh.Flags{ Map: map[string]argh.FlagConfig{ "wat": {}, diff --git a/querier_test.go b/querier_test.go index 962d869..c366e4e 100644 --- a/querier_test.go +++ b/querier_test.go @@ -19,7 +19,7 @@ func TestQuerier_Program(t *testing.T) { name: "typical", args: []string{"pizzas", "ahoy", "--treatsa", "fun"}, cfg: &argh.ParserConfig{ - Prog: argh.CommandConfig{ + Prog: &argh.CommandConfig{ Commands: &argh.Commands{ Map: map[string]argh.CommandConfig{ "ahoy": argh.CommandConfig{