Reduce required args to new parser
This commit is contained in:
parent
e3872e316d
commit
d0c96803c8
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
*.a
|
||||||
|
*.exe
|
||||||
|
*.o
|
||||||
|
*.out
|
||||||
|
*.so
|
@ -15,16 +15,16 @@ func main() {
|
|||||||
|
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
pt, err := argh.ParseArgs(os.Args, argh.NewParserConfig(
|
pCfg := argh.NewParserConfig()
|
||||||
&argh.CommandConfig{
|
pCfg.Prog = argh.CommandConfig{
|
||||||
NValue: argh.OneOrMoreValue,
|
NValue: argh.OneOrMoreValue,
|
||||||
ValueNames: []string{"topping"},
|
ValueNames: []string{"topping"},
|
||||||
Flags: &argh.Flags{
|
Flags: &argh.Flags{
|
||||||
Automatic: true,
|
Automatic: true,
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
nil,
|
|
||||||
))
|
pt, err := argh.ParseArgs(os.Args, pCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -7,10 +7,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
POSIXyParserConfig = NewParserConfig(
|
POSIXyParserConfig = NewParserConfig()
|
||||||
nil,
|
|
||||||
POSIXyScannerConfig,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type NValue int
|
type NValue int
|
||||||
@ -35,20 +32,24 @@ type ParserConfig struct {
|
|||||||
ScannerConfig *ScannerConfig
|
ScannerConfig *ScannerConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewParserConfig(prog *CommandConfig, sCfg *ScannerConfig) *ParserConfig {
|
type ParserOption func(*ParserConfig)
|
||||||
if sCfg == nil {
|
|
||||||
sCfg = POSIXyScannerConfig
|
func NewParserConfig(opts ...ParserOption) *ParserConfig {
|
||||||
|
pCfg := &ParserConfig{}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if opt != nil {
|
||||||
|
opt(pCfg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if prog == nil {
|
if pCfg.Prog.IsZero() {
|
||||||
prog = &CommandConfig{}
|
pCfg.Prog = CommandConfig{}
|
||||||
|
pCfg.Prog.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
prog.init()
|
if pCfg.ScannerConfig == nil {
|
||||||
|
pCfg.ScannerConfig = POSIXyScannerConfig
|
||||||
pCfg := &ParserConfig{
|
|
||||||
Prog: *prog,
|
|
||||||
ScannerConfig: sCfg,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pCfg
|
return pCfg
|
||||||
@ -61,6 +62,13 @@ 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{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user