Skip to content

Commit

Permalink
refactor: using xconf.Usage as default error output
Browse files Browse the repository at this point in the history
  • Loading branch information
hui.wang committed Jan 13, 2022
1 parent 03eb8de commit e4e5aa4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
6 changes: 1 addition & 5 deletions tests/replit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package main

import (
"bytes"
"flag"
"fmt"
"os"

"github.com/sandwich-go/xconf"
"github.com/sandwich-go/xconf/tests/replit/config"
"github.com/sandwich-go/xconf/xflag"
)

func panicErr(err error) {
Expand Down Expand Up @@ -61,7 +59,5 @@ func main() {
fmt.Println("empty config etcd : ", empteOne.GetETCD()) // {[10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4]
fmt.Println("empty config redis: ", empteOne.GetRedis()) // {[192.168.0.1 192.168.0.2] true {16m40s 16m40s 16m40s}}

xconf.DumpInfo()
xflag.PrintDefaults(flag.CommandLine)

xconf.Usage()
}
18 changes: 7 additions & 11 deletions xconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,14 @@ func (x *XConf) parse(valPtr interface{}) (err error) {
if !got {
val, got = parsedOptions["h"]
}
if got {
if strings.EqualFold(xutil.StringTrim(val), "xconf") {
// 指定xconf_usage的FlagArgs为空,避免再次触发help逻辑
xx := New(WithFlagSet(newFlagSetContinueOnError("xconf_usage")), WithFlagArgs(), WithErrorHandling(ContinueOnError))
cc := NewOptions()
xutil.PanicErr(xx.Parse(cc))
xx.Usage()
} else {
x.Usage()
}
if got && strings.EqualFold(xutil.StringTrim(val), "xconf") {
// 指定xconf_usage的FlagArgs为空,避免再次触发help逻辑
xx := New(WithFlagSet(newFlagSetContinueOnError("xconf_usage")), WithFlagArgs(), WithErrorHandling(ContinueOnError))
cc := NewOptions()
xutil.PanicErr(xx.Parse(cc))
xx.Usage()
} else {
xflag.PrintDefaults(x.cc.FlagSet, x.optionUsage)
x.Usage()
}
}
}
Expand Down

0 comments on commit e4e5aa4

Please sign in to comment.