Reduce required args to new parser

This commit is contained in:
Dan Buch 2022-06-06 19:06:50 -04:00
parent e3872e316d
commit d0c96803c8
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 36 additions and 23 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.a
*.exe
*.o
*.out
*.so

View File

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

View File

@ -7,10 +7,7 @@ const (
)
var (
POSIXyParserConfig = NewParserConfig(
nil,
POSIXyScannerConfig,
)
POSIXyParserConfig = NewParserConfig()
)
type NValue int
@ -35,20 +32,24 @@ type ParserConfig struct {
ScannerConfig *ScannerConfig
}
func NewParserConfig(prog *CommandConfig, sCfg *ScannerConfig) *ParserConfig {
if sCfg == nil {
sCfg = POSIXyScannerConfig
type ParserOption func(*ParserConfig)
func NewParserConfig(opts ...ParserOption) *ParserConfig {
pCfg := &ParserConfig{}
for _, opt := range opts {
if opt != nil {
opt(pCfg)
}
}
if prog == nil {
prog = &CommandConfig{}
if pCfg.Prog.IsZero() {
pCfg.Prog = CommandConfig{}
pCfg.Prog.init()
}
prog.init()
pCfg := &ParserConfig{
Prog: *prog,
ScannerConfig: sCfg,
if pCfg.ScannerConfig == nil {
pCfg.ScannerConfig = POSIXyScannerConfig
}
return pCfg
@ -61,6 +62,13 @@ type CommandConfig struct {
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() {
if cCfg.ValueNames == nil {
cCfg.ValueNames = []string{}