修改flag解析参数 "-n 1 -i 1" to "-n1 -i1"

This commit is contained in:
殷勇 2023-08-28 14:58:49 +08:00
parent 3584ff5e7e
commit 0ff73b5f57

36
app.go
View File

@ -1,9 +1,10 @@
package f5
import (
"flag"
"fmt"
"os"
"q5"
"strings"
"sync"
"sync/atomic"
"time"
@ -75,10 +76,7 @@ func (this *app) init(userApp UserApp) {
_httpCliMgr = new(HttpCliMgr)
_httpCliMgr.init()
{
var tmpNodeId, tmpInstanceId int
flag.IntVar(&tmpNodeId, "n", 0, "node id")
flag.IntVar(&tmpInstanceId, "i", 0, "instance id")
flag.Parse()
tmpNodeId, tmpInstanceId := parseArgs()
this.nodeId = int32(tmpNodeId)
this.instanceId = int32(tmpInstanceId)
}
@ -256,3 +254,31 @@ func (this *app) installTimer() {
}
})
}
func parseArgs() (int, int) {
args := os.Args[1:]
if len(args) <= 0 {
return 0, 0
}
var nodeId, instanceId int
for i := 0; i < len(args); i++ {
arg := args[i]
if strings.HasPrefix(arg, "-n") {
if len(arg) > 2 {
fmt.Sscanf(arg[2:], "%d", &nodeId)
} else if i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &nodeId)
i++
}
} else if strings.HasPrefix(arg, "-i") {
if len(arg) > 2 {
fmt.Sscanf(arg[2:], "%d", &instanceId)
} else if i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &instanceId)
i++
}
}
}
return nodeId, instanceId
}