empty cli args value & help flag support

This commit is contained in:
lionsoul2014 2026-04-22 15:34:45 +08:00
parent 1d9ffabfeb
commit d36edc6e15
6 changed files with 52 additions and 21 deletions

View File

@ -11,7 +11,8 @@ import (
func Bench(sCmd string) { func Bench(sCmd string) {
var err error var err error
var dbFile, srcFile, ipVersion, logLevel = "", "", "", "" var dbFile, srcFile, ipVersion = "", "", ""
var help, logLevel = false, ""
var ignoreError = false var ignoreError = false
var fErr = IterateFlags(func(key string, val string) error { var fErr = IterateFlags(func(key string, val string) error {
switch key { switch key {
@ -23,6 +24,8 @@ func Bench(sCmd string) {
ipVersion = val ipVersion = val
case "log-level": case "log-level":
logLevel = val logLevel = val
case "help":
help = GetDefaultOnBool(val)
case "ignore-error": case "ignore-error":
switch val { switch val {
case "true", "1": case "true", "1":
@ -42,7 +45,7 @@ func Bench(sCmd string) {
return return
} }
if dbFile == "" || srcFile == "" { if dbFile == "" || srcFile == "" || help {
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd) fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
fmt.Printf("options:\n") fmt.Printf("options:\n")
fmt.Printf(" --db string ip2region binary xdb file path\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(" --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(" --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(" --ignore-error bool keep going if bench failed\n")
fmt.Printf(" --help bool print this help menu\n")
return return
} }

View File

@ -21,12 +21,15 @@ import (
func Edit(sCmd string) { func Edit(sCmd string) {
var err error var err error
var srcFile, ipVersion = "", "" var srcFile, ipVersion = "", ""
var help = false
var fErr = IterateFlags(func(key string, val string) error { var fErr = IterateFlags(func(key string, val string) error {
switch key { switch key {
case "src": case "src":
srcFile = val srcFile = val
case "version": case "version":
ipVersion = val ipVersion = val
case "help":
help = GetDefaultOnBool(val)
default: default:
return fmt.Errorf("undefined option '%s=%s'", key, val) return fmt.Errorf("undefined option '%s=%s'", key, val)
} }
@ -37,11 +40,12 @@ func Edit(sCmd string) {
return return
} }
if srcFile == "" { if srcFile == "" || help {
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd) fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
fmt.Printf("options:\n") fmt.Printf("options:\n")
fmt.Printf(" --src string source ip text file path\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(" --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 return
} }
@ -72,7 +76,7 @@ func Edit(sCmd string) {
} }
fmt.Printf("all segments loaded, length: %d, elapsed: %s\n", editor.SegLen(), time.Since(tStart)) 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("command list: \n")
fmt.Printf(" put [segment] : put the specifield $segment\n") fmt.Printf(" put [segment] : put the specifield $segment\n")
fmt.Printf(" put_file [file] : put all the segments from the specified $file\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") fmt.Printf(" help : print this help menu\n")
} }
help() printHelp()
var sTip = "" var sTip = ""
var reader = bufio.NewReader(os.Stdin) var reader = bufio.NewReader(os.Stdin)
for { for {
@ -101,7 +105,7 @@ func Edit(sCmd string) {
cmd := strings.TrimSpace(line) cmd := strings.TrimSpace(line)
if cmd == "help" { if cmd == "help" {
help() printHelp()
} else if cmd == "quit" { } else if cmd == "quit" {
if editor.NeedSave() { if editor.NeedSave() {
fmt.Printf("there are changes that need to save, type 'quit!' to force quit\n") 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) fmt.Printf("PutFile(%s): Ok, with %d deletes and %d additions\n", file, o, n)
} else if len(cmd) > 0 { } else if len(cmd) > 0 {
help() printHelp()
} }
} }
} }

View File

@ -19,7 +19,7 @@ func Generate(sCmd string) {
var err error var err error
var srcFile, dstFile = "", "" var srcFile, dstFile = "", ""
var ipVersion, fieldList, logLevel = "", "", "info" var ipVersion, fieldList, logLevel = "", "", "info"
var indexPolicy = xdb.VectorIndexPolicy var help, indexPolicy = false, xdb.VectorIndexPolicy
var fErr = IterateFlags(func(key string, val string) error { var fErr = IterateFlags(func(key string, val string) error {
switch key { switch key {
case "src": case "src":
@ -32,6 +32,8 @@ func Generate(sCmd string) {
logLevel = val logLevel = val
case "field-list": case "field-list":
fieldList = val fieldList = val
case "help":
help = GetDefaultOnBool(val)
case "index": case "index":
indexPolicy, err = xdb.IndexPolicyFromString(val) indexPolicy, err = xdb.IndexPolicyFromString(val)
if err != nil { if err != nil {
@ -48,7 +50,7 @@ func Generate(sCmd string) {
return return
} }
if srcFile == "" || dstFile == "" { if srcFile == "" || dstFile == "" || help {
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd) fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
fmt.Printf("options:\n") fmt.Printf("options:\n")
fmt.Printf(" --src string source ip text file path\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(" --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(" --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(" --log-level string set the log level, options: debug/info/warn/error\n")
fmt.Printf(" --help bool print this help menu\n")
return return
} }

View File

@ -18,8 +18,8 @@ import (
func Process(sCmd string) { func Process(sCmd string) {
var err error var err error
var srcFile, dstFile = "", "" var srcFile, dstFile, fieldList = "", "", ""
var fieldList, logLevel = "", "" var help, logLevel = false, ""
var clearBasedIndex = -1 var clearBasedIndex = -1
var clearValueEqual, clearValueExcept = "", "" var clearValueEqual, clearValueExcept = "", ""
var fErr = IterateFlags(func(key string, val string) error { var fErr = IterateFlags(func(key string, val string) error {
@ -41,6 +41,8 @@ func Process(sCmd string) {
clearValueEqual = val clearValueEqual = val
case "clear-value-except": case "clear-value-except":
clearValueExcept = val clearValueExcept = val
case "help":
help = GetDefaultOnBool(val)
case "log-level": case "log-level":
logLevel = val logLevel = val
default: default:
@ -53,7 +55,7 @@ func Process(sCmd string) {
return return
} }
if srcFile == "" || dstFile == "" { if srcFile == "" || dstFile == "" || help {
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd) fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
fmt.Printf("options:\n") fmt.Printf("options:\n")
fmt.Printf(" --src string source ip text file path\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-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(" --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(" --log-level string set the log level, options: debug/info/warn/error\n")
fmt.Printf(" --help bool print this help menu\n")
return return
} }

View File

@ -20,11 +20,14 @@ import (
func Search(sCmd string) { func Search(sCmd string) {
var err error var err error
var dbFile = "" var dbFile, help = "", false
var fErr = IterateFlags(func(key string, val string) error { var fErr = IterateFlags(func(key string, val string) error {
if key == "db" { switch key {
case "db":
dbFile = val dbFile = val
} else { case "help":
help = GetDefaultOnBool(val)
default:
return fmt.Errorf("undefined option '%s=%s'", key, val) return fmt.Errorf("undefined option '%s=%s'", key, val)
} }
return nil return nil
@ -34,10 +37,11 @@ func Search(sCmd string) {
return return
} }
if dbFile == "" { if dbFile == "" || help {
fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd) fmt.Printf("%s %s [command options]\n", os.Args[0], sCmd)
fmt.Printf("options:\n") fmt.Printf("options:\n")
fmt.Printf(" --db string ip2region binary xdb file path\n") fmt.Printf(" --db string ip2region binary xdb file path\n")
fmt.Printf(" --help bool print this help menu\n")
return return
} }

View File

@ -14,7 +14,7 @@ import (
func IterateFlags(cb func(key string, val string) error) error { func IterateFlags(cb func(key string, val string) error) error {
for i := 2; i < len(os.Args); i++ { for i := 2; i < len(os.Args); i++ {
r := os.Args[i] r := os.Args[i]
if len(r) < 5 { if len(r) < 3 {
continue continue
} }
@ -22,12 +22,16 @@ func IterateFlags(cb func(key string, val string) error) error {
continue continue
} }
var sIdx = strings.Index(r, "=") var k, v = "", "" // default empty value
if sIdx < 0 { if idx := strings.Index(r, "="); idx == -1 {
return fmt.Errorf("missing = for args pair '%s'", r) 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 return err
} }
} }
@ -35,6 +39,15 @@ func IterateFlags(cb func(key string, val string) error) error {
return nil 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 { func ApplyLogLevel(logLevel string) error {
// check and apply the log level // check and apply the log level
var levelLog = slog.LevelInfo var levelLog = slog.LevelInfo