diff --git a/tools/pd-ctl/main.go b/tools/pd-ctl/main.go index df3ed756c27..6b0a8d54259 100644 --- a/tools/pd-ctl/main.go +++ b/tools/pd-ctl/main.go @@ -52,6 +52,7 @@ func main() { if pdAddr != "" { os.Args = append(os.Args, "-u", pdAddr) } + flag.CommandLine.ParseErrorsWhitelist.UnknownFlags = true flag.Parse() if version { diff --git a/tools/pd-ctl/pdctl/command/region_command.go b/tools/pd-ctl/pdctl/command/region_command.go index 6af72af8e38..0d98b34cb04 100644 --- a/tools/pd-ctl/pdctl/command/region_command.go +++ b/tools/pd-ctl/pdctl/command/region_command.go @@ -199,7 +199,7 @@ func showRegionTopSizeCommandFunc(cmd *cobra.Command, args []string) { // NewRegionWithKeyCommand return a region with key subcommand of regionCmd func NewRegionWithKeyCommand() *cobra.Command { r := &cobra.Command{ - Use: "key [--format=raw|pb|proto|protobuf] ", + Use: "key [--format=raw|encode] ", Short: "show the region with key", Run: showRegionWithTableCommandFunc, } @@ -222,8 +222,8 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) { switch format { case "raw": key = args[0] - case "pb", "proto", "protobuf": - key, err = decodeProtobufText(args[0]) + case "encode": + key, err = decodeKey(args[0]) if err != nil { fmt.Println("Error: ", err) return @@ -240,10 +240,9 @@ func showRegionWithTableCommandFunc(cmd *cobra.Command, args []string) { return } fmt.Println(r) - } -func decodeProtobufText(text string) (string, error) { +func decodeKey(text string) (string, error) { var buf []byte r := bytes.NewBuffer([]byte(text)) for { @@ -254,13 +253,35 @@ func decodeProtobufText(text string) (string, error) { } break } - if c == '\\' { - _, err := fmt.Sscanf(string(r.Next(3)), "%03o", &c) + if c != '\\' { + buf = append(buf, c) + continue + } + n := r.Next(1) + switch n[0] { + case '"': + buf = append(buf, '"') + case '\'': + buf = append(buf, '\'') + case '\\': + buf = append(buf, '\\') + case 'n': + buf = append(buf, '\n') + case 't': + buf = append(buf, '\t') + case 'r': + buf = append(buf, '\r') + case 'x': + fmt.Sscanf(string(r.Next(2)), "%02x", &c) + buf = append(buf, c) + default: + n = append(n, r.Next(2)...) + _, err := fmt.Sscanf(string(n), "%03o", &c) if err != nil { return "", err } + buf = append(buf, c) } - buf = append(buf, c) } return string(buf), nil }