Making more messes with parser config

This commit is contained in:
Dan Buch 2022-06-04 17:38:24 -04:00
parent a73acedc6d
commit 3de96b813f
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 58 additions and 8 deletions

View File

@ -15,7 +15,16 @@ func main() {
log.SetFlags(0) log.SetFlags(0)
pt, err := argh.ParseArgs(os.Args, nil) pt, err := argh.ParseArgs(os.Args, argh.NewParserConfig(
&argh.CommandConfig{
NValue: argh.OneOrMoreValue,
ValueNames: []string{"topping"},
Flags: &argh.Flags{
Automatic: true,
},
},
nil,
))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -7,10 +7,10 @@ const (
) )
var ( var (
POSIXyParserConfig = &ParserConfig{ POSIXyParserConfig = NewParserConfig(
Prog: CommandConfig{}, nil,
ScannerConfig: POSIXyScannerConfig, POSIXyScannerConfig,
} )
) )
type NValue int type NValue int
@ -35,6 +35,25 @@ type ParserConfig struct {
ScannerConfig *ScannerConfig ScannerConfig *ScannerConfig
} }
func NewParserConfig(prog *CommandConfig, sCfg *ScannerConfig) *ParserConfig {
if sCfg == nil {
sCfg = POSIXyScannerConfig
}
if prog == nil {
prog = &CommandConfig{}
}
prog.init()
pCfg := &ParserConfig{
Prog: *prog,
ScannerConfig: sCfg,
}
return pCfg
}
type CommandConfig struct { type CommandConfig struct {
NValue NValue NValue NValue
ValueNames []string ValueNames []string
@ -42,6 +61,20 @@ type CommandConfig struct {
Commands *Commands Commands *Commands
} }
func (cCfg *CommandConfig) init() {
if cCfg.ValueNames == nil {
cCfg.ValueNames = []string{}
}
if cCfg.Flags == nil {
cCfg.Flags = &Flags{}
}
if cCfg.Commands == nil {
cCfg.Commands = &Commands{}
}
}
func (cCfg *CommandConfig) GetCommandConfig(name string) (CommandConfig, bool) { func (cCfg *CommandConfig) GetCommandConfig(name string) (CommandConfig, bool) {
tracef("CommandConfig.GetCommandConfig(%q)", name) tracef("CommandConfig.GetCommandConfig(%q)", name)
@ -71,6 +104,8 @@ type FlagConfig struct {
type Flags struct { type Flags struct {
Parent *Flags Parent *Flags
Map map[string]FlagConfig Map map[string]FlagConfig
Automatic bool
} }
func (fl *Flags) Get(name string) (FlagConfig, bool) { func (fl *Flags) Get(name string) (FlagConfig, bool) {
@ -81,9 +116,15 @@ func (fl *Flags) Get(name string) (FlagConfig, bool) {
} }
flCfg, ok := fl.Map[name] flCfg, ok := fl.Map[name]
if !ok && fl.Parent != nil { if !ok {
flCfg, ok = fl.Parent.Get(name) if fl.Automatic {
return flCfg, ok && flCfg.Persist return FlagConfig{}, true
}
if fl.Parent != nil {
flCfg, ok = fl.Parent.Get(name)
return flCfg, ok && flCfg.Persist
}
} }
return flCfg, ok return flCfg, ok