Making more messes with parser config
This commit is contained in:
parent
a73acedc6d
commit
3de96b813f
@ -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)
|
||||
}
|
@ -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,10 +116,16 @@ func (fl *Flags) Get(name string) (FlagConfig, bool) {
|
||||
}
|
||||
|
||||
flCfg, ok := fl.Map[name]
|
||||
if !ok && fl.Parent != nil {
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user