empty cli args value & help flag support
This commit is contained in:
parent
1d9ffabfeb
commit
d36edc6e15
|
|
@ -11,7 +11,8 @@ import (
|
|||
|
||||
func Bench(sCmd string) {
|
||||
var err error
|
||||
var dbFile, srcFile, ipVersion, logLevel = "", "", "", ""
|
||||
var dbFile, srcFile, ipVersion = "", "", ""
|
||||
var help, logLevel = false, ""
|
||||
var ignoreError = false
|
||||
var fErr = IterateFlags(func(key string, val string) error {
|
||||
switch key {
|
||||
|
|
@ -23,6 +24,8 @@ func Bench(sCmd string) {
|
|||
ipVersion = val
|
||||
case "log-level":
|
||||
logLevel = val
|
||||
case "help":
|
||||
help = GetDefaultOnBool(val)
|
||||
case "ignore-error":
|
||||
switch val {
|
||||
case "true", "1":
|
||||
|
|
@ -42,7 +45,7 @@ func Bench(sCmd string) {
|
|||
return
|
||||
}
|
||||
|
||||
if dbFile == "" || srcFile == "" {
|
||||
if dbFile == "" || srcFile == "" || help {
|
||||
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||
fmt.Printf("options:\n")
|
||||
fmt.Printf(" --db string ip2region binary xdb file path\n")
|
||||
|
|
@ -50,6 +53,7 @@ func Bench(sCmd string) {
|
|||
fmt.Printf(" --version string IP version, options: ipv4/ipv6, specify this flag so you don't get confused \n")
|
||||
fmt.Printf(" --log-level string set the log level, options: debug/info/warn/error\n")
|
||||
fmt.Printf(" --ignore-error bool keep going if bench failed\n")
|
||||
fmt.Printf(" --help bool print this help menu\n")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,12 +21,15 @@ import (
|
|||
func Edit(sCmd string) {
|
||||
var err error
|
||||
var srcFile, ipVersion = "", ""
|
||||
var help = false
|
||||
var fErr = IterateFlags(func(key string, val string) error {
|
||||
switch key {
|
||||
case "src":
|
||||
srcFile = val
|
||||
case "version":
|
||||
ipVersion = val
|
||||
case "help":
|
||||
help = GetDefaultOnBool(val)
|
||||
default:
|
||||
return fmt.Errorf("undefined option '%s=%s'", key, val)
|
||||
}
|
||||
|
|
@ -37,11 +40,12 @@ func Edit(sCmd string) {
|
|||
return
|
||||
}
|
||||
|
||||
if srcFile == "" {
|
||||
if srcFile == "" || help {
|
||||
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||
fmt.Printf("options:\n")
|
||||
fmt.Printf(" --src string source ip text file path\n")
|
||||
fmt.Printf(" --version string IP version, options: ipv4/ipv6, specify this flag so you don't get confused \n")
|
||||
fmt.Printf(" --help bool print this help menu\n")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -72,7 +76,7 @@ func Edit(sCmd string) {
|
|||
}
|
||||
|
||||
fmt.Printf("all segments loaded, length: %d, elapsed: %s\n", editor.SegLen(), time.Since(tStart))
|
||||
var help = func() {
|
||||
var printHelp = func() {
|
||||
fmt.Printf("command list: \n")
|
||||
fmt.Printf(" put [segment] : put the specifield $segment\n")
|
||||
fmt.Printf(" put_file [file] : put all the segments from the specified $file\n")
|
||||
|
|
@ -82,7 +86,7 @@ func Edit(sCmd string) {
|
|||
fmt.Printf(" help : print this help menu\n")
|
||||
}
|
||||
|
||||
help()
|
||||
printHelp()
|
||||
var sTip = ""
|
||||
var reader = bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
|
|
@ -101,7 +105,7 @@ func Edit(sCmd string) {
|
|||
|
||||
cmd := strings.TrimSpace(line)
|
||||
if cmd == "help" {
|
||||
help()
|
||||
printHelp()
|
||||
} else if cmd == "quit" {
|
||||
if editor.NeedSave() {
|
||||
fmt.Printf("there are changes that need to save, type 'quit!' to force quit\n")
|
||||
|
|
@ -158,7 +162,7 @@ func Edit(sCmd string) {
|
|||
}
|
||||
fmt.Printf("PutFile(%s): Ok, with %d deletes and %d additions\n", file, o, n)
|
||||
} else if len(cmd) > 0 {
|
||||
help()
|
||||
printHelp()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func Generate(sCmd string) {
|
|||
var err error
|
||||
var srcFile, dstFile = "", ""
|
||||
var ipVersion, fieldList, logLevel = "", "", "info"
|
||||
var indexPolicy = xdb.VectorIndexPolicy
|
||||
var help, indexPolicy = false, xdb.VectorIndexPolicy
|
||||
var fErr = IterateFlags(func(key string, val string) error {
|
||||
switch key {
|
||||
case "src":
|
||||
|
|
@ -32,6 +32,8 @@ func Generate(sCmd string) {
|
|||
logLevel = val
|
||||
case "field-list":
|
||||
fieldList = val
|
||||
case "help":
|
||||
help = GetDefaultOnBool(val)
|
||||
case "index":
|
||||
indexPolicy, err = xdb.IndexPolicyFromString(val)
|
||||
if err != nil {
|
||||
|
|
@ -48,7 +50,7 @@ func Generate(sCmd string) {
|
|||
return
|
||||
}
|
||||
|
||||
if srcFile == "" || dstFile == "" {
|
||||
if srcFile == "" || dstFile == "" || help {
|
||||
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||
fmt.Printf("options:\n")
|
||||
fmt.Printf(" --src string source ip text file path\n")
|
||||
|
|
@ -56,6 +58,7 @@ func Generate(sCmd string) {
|
|||
fmt.Printf(" --version string IP version, options: ipv4/ipv6, specify this flag so you don't get confused \n")
|
||||
fmt.Printf(" --field-list string field index list imploded with ',' eg: 0,1,2,3-6,7\n")
|
||||
fmt.Printf(" --log-level string set the log level, options: debug/info/warn/error\n")
|
||||
fmt.Printf(" --help bool print this help menu\n")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ import (
|
|||
|
||||
func Process(sCmd string) {
|
||||
var err error
|
||||
var srcFile, dstFile = "", ""
|
||||
var fieldList, logLevel = "", ""
|
||||
var srcFile, dstFile, fieldList = "", "", ""
|
||||
var help, logLevel = false, ""
|
||||
var clearBasedIndex = -1
|
||||
var clearValueEqual, clearValueExcept = "", ""
|
||||
var fErr = IterateFlags(func(key string, val string) error {
|
||||
|
|
@ -41,6 +41,8 @@ func Process(sCmd string) {
|
|||
clearValueEqual = val
|
||||
case "clear-value-except":
|
||||
clearValueExcept = val
|
||||
case "help":
|
||||
help = GetDefaultOnBool(val)
|
||||
case "log-level":
|
||||
logLevel = val
|
||||
default:
|
||||
|
|
@ -53,7 +55,7 @@ func Process(sCmd string) {
|
|||
return
|
||||
}
|
||||
|
||||
if srcFile == "" || dstFile == "" {
|
||||
if srcFile == "" || dstFile == "" || help {
|
||||
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||
fmt.Printf("options:\n")
|
||||
fmt.Printf(" --src string source ip text file path\n")
|
||||
|
|
@ -63,6 +65,7 @@ func Process(sCmd string) {
|
|||
fmt.Printf(" --clear-value-equal string clear value equal to the specified one\n")
|
||||
fmt.Printf(" --clear-value-except string clear value except the specified one\n")
|
||||
fmt.Printf(" --log-level string set the log level, options: debug/info/warn/error\n")
|
||||
fmt.Printf(" --help bool print this help menu\n")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,11 +20,14 @@ import (
|
|||
|
||||
func Search(sCmd string) {
|
||||
var err error
|
||||
var dbFile = ""
|
||||
var dbFile, help = "", false
|
||||
var fErr = IterateFlags(func(key string, val string) error {
|
||||
if key == "db" {
|
||||
switch key {
|
||||
case "db":
|
||||
dbFile = val
|
||||
} else {
|
||||
case "help":
|
||||
help = GetDefaultOnBool(val)
|
||||
default:
|
||||
return fmt.Errorf("undefined option '%s=%s'", key, val)
|
||||
}
|
||||
return nil
|
||||
|
|
@ -34,10 +37,11 @@ func Search(sCmd string) {
|
|||
return
|
||||
}
|
||||
|
||||
if dbFile == "" {
|
||||
if dbFile == "" || help {
|
||||
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
|
||||
fmt.Printf("options:\n")
|
||||
fmt.Printf(" --db string ip2region binary xdb file path\n")
|
||||
fmt.Printf(" --help bool print this help menu\n")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ import (
|
|||
func IterateFlags(cb func(key string, val string) error) error {
|
||||
for i := 2; i < len(os.Args); i++ {
|
||||
r := os.Args[i]
|
||||
if len(r) < 5 {
|
||||
if len(r) < 3 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -22,12 +22,16 @@ func IterateFlags(cb func(key string, val string) error) error {
|
|||
continue
|
||||
}
|
||||
|
||||
var sIdx = strings.Index(r, "=")
|
||||
if sIdx < 0 {
|
||||
return fmt.Errorf("missing = for args pair '%s'", r)
|
||||
var k, v = "", "" // default empty value
|
||||
if idx := strings.Index(r, "="); idx == -1 {
|
||||
k = r[2:]
|
||||
} else {
|
||||
k = r[2:idx]
|
||||
v = r[idx+1:]
|
||||
}
|
||||
|
||||
if err := cb(r[2:sIdx], r[sIdx+1:]); err != nil {
|
||||
// fmt.Printf("k=%s, v=%s\n", k, v)
|
||||
if err := cb(k, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
@ -35,6 +39,15 @@ func IterateFlags(cb func(key string, val string) error) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Parse help
|
||||
func GetDefaultOnBool(val string) bool {
|
||||
if val == "false" || val == "0" || val == "off" {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func ApplyLogLevel(logLevel string) error {
|
||||
// check and apply the log level
|
||||
var levelLog = slog.LevelInfo
|
||||
|
|
|
|||
Loading…
Reference in New Issue