Pointers and nils again

urfave-cli-integration
Dan Buch 2 years ago
parent d0c96803c8
commit 30449ba506
Signed by: meatballhat
GPG Key ID: A12F782281063434

@ -16,7 +16,7 @@ func main() {
log.SetFlags(0) log.SetFlags(0)
pCfg := argh.NewParserConfig() pCfg := argh.NewParserConfig()
pCfg.Prog = argh.CommandConfig{ pCfg.Prog = &argh.CommandConfig{
NValue: argh.OneOrMoreValue, NValue: argh.OneOrMoreValue,
ValueNames: []string{"topping"}, ValueNames: []string{"topping"},
Flags: &argh.Flags{ Flags: &argh.Flags{

@ -61,7 +61,7 @@ func (p *parser) 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) tracef("parseArgs() top level node is %T", prog)

@ -27,7 +27,7 @@ func (nv NValue) Contains(i int) bool {
} }
type ParserConfig struct { type ParserConfig struct {
Prog CommandConfig Prog *CommandConfig
ScannerConfig *ScannerConfig ScannerConfig *ScannerConfig
} }
@ -43,8 +43,8 @@ func NewParserConfig(opts ...ParserOption) *ParserConfig {
} }
} }
if pCfg.Prog.IsZero() { if pCfg.Prog == nil {
pCfg.Prog = CommandConfig{} pCfg.Prog = &CommandConfig{}
pCfg.Prog.init() pCfg.Prog.init()
} }
@ -62,13 +62,6 @@ type CommandConfig struct {
Commands *Commands 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() { func (cCfg *CommandConfig) init() {
if cCfg.ValueNames == nil { if cCfg.ValueNames == nil {
cCfg.ValueNames = []string{} cCfg.ValueNames = []string{}

@ -24,7 +24,7 @@ func TestParser(t *testing.T) {
"pies", "-eat", "--wat", "hello", "mario", "pies", "-eat", "--wat", "hello", "mario",
}, },
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"e": {}, "e": {},
@ -98,8 +98,8 @@ func TestParser(t *testing.T) {
"pies", "--wat", "hello", "mario", "-eat", "pies", "--wat", "hello", "mario", "-eat",
}, },
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: func() argh.CommandConfig { Prog: func() *argh.CommandConfig {
cmdCfg := argh.CommandConfig{ cmdCfg := &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"e": {Persist: true}, "e": {Persist: true},
@ -193,7 +193,7 @@ func TestParser(t *testing.T) {
name: "one positional arg", name: "one positional arg",
args: []string{"pizzas", "excel"}, args: []string{"pizzas", "excel"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{NValue: 1}, Prog: &argh.CommandConfig{NValue: 1},
}, },
expPT: []argh.Node{ expPT: []argh.Node{
&argh.Command{ &argh.Command{
@ -219,7 +219,7 @@ func TestParser(t *testing.T) {
name: "many positional args", name: "many positional args",
args: []string{"pizzas", "excel", "wildly", "when", "feral"}, args: []string{"pizzas", "excel", "wildly", "when", "feral"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
NValue: argh.OneOrMoreValue, NValue: argh.OneOrMoreValue,
ValueNames: []string{"word"}, ValueNames: []string{"word"},
}, },
@ -267,7 +267,7 @@ func TestParser(t *testing.T) {
name: "long value-less flags", name: "long value-less flags",
args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"}, args: []string{"pizzas", "--tasty", "--fresh", "--super-hot-right-now"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"tasty": {}, "tasty": {},
@ -312,7 +312,7 @@ func TestParser(t *testing.T) {
"--please", "--please",
}, },
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}}, Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}},
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
@ -391,7 +391,7 @@ func TestParser(t *testing.T) {
name: "short value-less flags", name: "short value-less flags",
args: []string{"pizzas", "-t", "-f", "-s"}, args: []string{"pizzas", "-t", "-f", "-s"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"t": {}, "t": {},
@ -429,7 +429,7 @@ func TestParser(t *testing.T) {
name: "compound short flags", name: "compound short flags",
args: []string{"pizzas", "-aca", "-blol"}, args: []string{"pizzas", "-aca", "-blol"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"a": {}, "a": {},
@ -484,7 +484,7 @@ func TestParser(t *testing.T) {
name: "mixed long short value flags", name: "mixed long short value flags",
args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"}, args: []string{"pizzas", "-a", "--ca", "-b", "1312", "-lol"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}}, Commands: &argh.Commands{Map: map[string]argh.CommandConfig{}},
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
@ -549,7 +549,7 @@ func TestParser(t *testing.T) {
name: "nested commands with positional args", name: "nested commands with positional args",
args: []string{"pizzas", "fly", "freely", "sometimes", "and", "other", "times", "fry", "deeply", "--forever"}, args: []string{"pizzas", "fly", "freely", "sometimes", "and", "other", "times", "fry", "deeply", "--forever"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Commands: &argh.Commands{ Commands: &argh.Commands{
Map: map[string]argh.CommandConfig{ Map: map[string]argh.CommandConfig{
"fly": argh.CommandConfig{ "fly": argh.CommandConfig{
@ -632,7 +632,7 @@ func TestParser(t *testing.T) {
name: "compound flags with values", name: "compound flags with values",
args: []string{"pizzas", "-need", "sauce", "heat", "love", "-also", "over9000"}, args: []string{"pizzas", "-need", "sauce", "heat", "love", "-also", "over9000"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"a": {NValue: argh.ZeroOrMoreValue}, "a": {NValue: argh.ZeroOrMoreValue},
@ -735,7 +735,7 @@ func TestParser(t *testing.T) {
name: "command specific flags", name: "command specific flags",
args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt", "hugs"}, args: []string{"pizzas", "fly", "--freely", "fry", "--deeply", "-wAt", "hugs"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Commands: &argh.Commands{ Commands: &argh.Commands{
Map: map[string]argh.CommandConfig{ Map: map[string]argh.CommandConfig{
"fly": argh.CommandConfig{ "fly": argh.CommandConfig{
@ -835,7 +835,7 @@ func TestParser(t *testing.T) {
name: "total weirdo", name: "total weirdo",
args: []string{"PIZZAs", "^wAT@golf", "^^hecKing", "goose", "bonk", "^^FIERCENESS@-2"}, args: []string{"PIZZAs", "^wAT@golf", "^^hecKing", "goose", "bonk", "^^FIERCENESS@-2"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Commands: &argh.Commands{ Commands: &argh.Commands{
Map: map[string]argh.CommandConfig{ Map: map[string]argh.CommandConfig{
"goose": argh.CommandConfig{ "goose": argh.CommandConfig{
@ -910,7 +910,7 @@ func TestParser(t *testing.T) {
name: "windows like", name: "windows like",
args: []string{"hotdog", "/f", "/L", "/o:ppy", "hats"}, args: []string{"hotdog", "/f", "/L", "/o:ppy", "hats"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"f": {}, "f": {},
@ -957,7 +957,7 @@ func TestParser(t *testing.T) {
name: "invalid bare assignment", name: "invalid bare assignment",
args: []string{"pizzas", "=", "--wat"}, args: []string{"pizzas", "=", "--wat"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Flags: &argh.Flags{ Flags: &argh.Flags{
Map: map[string]argh.FlagConfig{ Map: map[string]argh.FlagConfig{
"wat": {}, "wat": {},

@ -19,7 +19,7 @@ func TestQuerier_Program(t *testing.T) {
name: "typical", name: "typical",
args: []string{"pizzas", "ahoy", "--treatsa", "fun"}, args: []string{"pizzas", "ahoy", "--treatsa", "fun"},
cfg: &argh.ParserConfig{ cfg: &argh.ParserConfig{
Prog: argh.CommandConfig{ Prog: &argh.CommandConfig{
Commands: &argh.Commands{ Commands: &argh.Commands{
Map: map[string]argh.CommandConfig{ Map: map[string]argh.CommandConfig{
"ahoy": argh.CommandConfig{ "ahoy": argh.CommandConfig{

Loading…
Cancel
Save