add help support in CLI

This commit is contained in:
holys 2014-07-18 12:20:25 +08:00
parent 82711d6fa8
commit feb3c68f61
2 changed files with 131 additions and 8 deletions

84
cmd/ledis-cli/const.go Normal file
View File

@ -0,0 +1,84 @@
package main
var helpCommands = [][]string{
{"DECR", "key", "KV"},
{"DECRBY", "key decrement", "KV"},
{"DEL", "key [key ...]", "KV"},
{"EXISTS", "key", "KV"},
{"GET", "key", "KV"},
{"GETSET", " key value", "KV"},
{"INCR", "key", "KV"},
{"INCRBY", "key increment", "KV"},
{"MGET", "key [key ...]", "KV"},
{"MSET", "key value [key value ...]", "KV"},
{"SET", "key value", "KV"},
{"SETNX", "key value", "KV"},
{"EXPIRE", "key seconds", "KV"},
{"EXPIREAT", "key timestamp", "KV"},
{"TTL", "PERSIST", "KV"},
{"HDEL", "key field [field ...]", "Hash"},
{"HEXISTS", "key field", "Hash"},
{"HGET", "key field", "Hash"},
{"HGETALL", "key", "Hash"},
{"HINCRBY", "key field increment", "Hash"},
{"HKEYS", "key", "Hash"},
{"HLEN", "key", "Hash"},
{"HMGET", "key field [field ...]", "Hash"},
{"HMSET", "key field value [field value ...]", "Hash"},
{"HSET", "key field value", "Hash"},
{"HVALS", "key", "Hash"},
{"HCLEAR", "key", "Hash"},
{"HMCLEAR", "key [key ...]", "Hash"},
{"HEXPIRE", "key seconds", "Hash"},
{"HEXPIREAT", "key timestamp", "Hash"},
{"HTTL", "key", "Hash"},
{"HPERSIST", "key", "Hash"},
{"LINDEX", "key index", "List"},
{"LLEN", "key", "List"},
{"LPOP", "key", "List"},
{"LPUSH", "key value [value ...]", "List"},
{"LRANGE", "key start stop", "List"},
{"RPOP", "key", "List"},
{"RPUSH", "key value [value ...]", "List"},
{"LCLEAR", "key", "List"},
{"LMCLEAR", "key [key ...]", "List"},
{"LEXPIRE", "key seconds", "List"},
{"LEXPIREAT", "key timestamp", "List"},
{"LTTL", "key", "List"},
{"LPERSIST", "key", "List"},
{"ZADD", "key score member [score member ...]", "ZSet"},
{"ZCARD", "key", "ZSet"},
{"ZCOUNT", "key min max", "ZSet"},
{"ZINCRBY", "key increment member", "ZSet"},
{"ZRANGE", "key start stop [WITHSCORES]", "ZSet"},
{"ZRANGEBYSCORE", "key min max [WITHSCORES] [LIMIT offset count]", "ZSet"},
{"ZRANK", "key member", "ZSet"},
{"ZREM", "key member [member ...]", "ZSet"},
{"ZREMRANGEBYRANK", "key start stop", "ZSet"},
{"ZREMRANGEBYSCORE", "key min max", "ZSet"},
{"ZREVRANGEBYSCORE", "key max min [WITHSCORES][LIMIT offset count]", "ZSet"},
{"ZREVRANK", "key member", "ZSet"},
{"ZSCORE", "key member", "ZSet"},
{"ZCLEAR", "key", "ZSet"},
{"ZMCLEAR", "key [key ...]", "ZSet"},
{"ZEXPIRE", "key seconds", "ZSet"},
{"ZEXPIREAT", "key timestamp", "ZSet"},
{"ZTTL", "key", "ZSet"},
{"ZPERSIST", "key", "ZSet"},
{"BGET", "key", "Bitmap"},
{"BGETBIT", "key offset", "Bitmap"},
{"BSETBIT", "key offset value", "Bitmap"},
{"BMSETBIT", "key offset value [offset value ...]", "Bitmap"},
{"BOPT", "operation destkey key [key ...]", "Bitmap"},
{"BCOUNT", "key [start end]", "Bitmap"},
{"BEXPIRE", "key seconds", "Bitmap"},
{"BEXPIREAT", "key timestamp", "Bitmap"},
{"BTTL", "key", "Bitmap"},
{"BPERSIST", "key", "Bitmap"},
{"SLAVEOF", "host port", "Replication"},
{"FULLSYNC", "-", "Replication"},
{"SYNC", "index offset", "Replication"},
{"PING", "-", "Server"},
{"ECHO", "message", "Server"},
{"SELECT", "index", "Server"},
}

View File

@ -58,21 +58,29 @@ func main() {
addHistory(cmd)
args := make([]interface{}, len(cmds[1:]))
for i := range args {
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
}
r, err := c.Do(cmds[0], args...)
if err != nil {
fmt.Printf("%s", err.Error())
} else if nb, _ := strconv.Atoi(cmds[1]); strings.ToLower(cmds[0]) == "select" && nb < 16 {
*dbn = nb
printReply(cmd, r)
cmd := cmds[0]
if strings.ToLower(cmd) == "help" || cmd == "?" {
printHelp(cmds)
} else {
printReply(cmd, r)
r, err := c.Do(cmds[0], args...)
if err != nil {
fmt.Printf("%s", err.Error())
} else if nb, _ := strconv.Atoi(cmds[1]); strings.ToLower(cmds[0]) == "select" && nb < 16 {
*dbn = nb
printReply(cmd, r)
} else {
printReply(cmd, r)
}
fmt.Printf("\n")
}
fmt.Printf("\n")
}
}
}
@ -105,3 +113,34 @@ func printReply(cmd string, reply interface{}) {
fmt.Printf("invalid ledis reply")
}
}
func printGenericHelp() {
msg :=
`ledis-cli
Type: "help <command>" for help on <command>
`
fmt.Println(msg)
}
func printCommandHelp(arr []string) {
fmt.Println()
fmt.Printf("\t%s %s \n", arr[0], arr[1])
fmt.Printf("\tGroup: %s \n", arr[2])
fmt.Println()
}
func printHelp(cmds []string) {
args := cmds[1:]
if len(args) == 0 {
printGenericHelp()
} else if len(args) > 1 {
fmt.Println()
} else {
cmd := strings.ToUpper(args[0])
for i := 0; i < len(helpCommands); i++ {
if helpCommands[i][0] == cmd {
printCommandHelp(helpCommands[i])
}
}
}
}