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)
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 {
log.Fatal(err)
}

View File

@ -7,10 +7,10 @@ const (
)
var (
POSIXyParserConfig = &ParserConfig{
Prog: CommandConfig{},
ScannerConfig: POSIXyScannerConfig,
}
POSIXyParserConfig = NewParserConfig(
nil,
POSIXyScannerConfig,
)
)
type NValue int
@ -35,6 +35,25 @@ type ParserConfig struct {
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 {
NValue NValue
ValueNames []string
@ -42,6 +61,20 @@ type CommandConfig struct {
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) {
tracef("CommandConfig.GetCommandConfig(%q)", name)
@ -71,6 +104,8 @@ type FlagConfig struct {
type Flags struct {
Parent *Flags
Map map[string]FlagConfig
Automatic 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]
if !ok && fl.Parent != nil {
flCfg, ok = fl.Parent.Get(name)
return flCfg, ok && flCfg.Persist
if !ok {
if fl.Automatic {
return FlagConfig{}, true
}
if fl.Parent != nil {
flCfg, ok = fl.Parent.Get(name)
return flCfg, ok && flCfg.Persist
}
}
return flCfg, ok