Browse Source

Merge pull request #40 from rauls5382/abbrev

Add support for abbreviated commands on interative command like
Josef Jelinek 8 years ago
parent
commit
13020e904d
2 changed files with 31 additions and 2 deletions
  1. 11
    1
      internal/driver/interactive.go
  2. 20
    1
      internal/driver/interactive_test.go

+ 11
- 1
internal/driver/interactive.go View File

@@ -17,6 +17,7 @@ package driver
17 17
 import (
18 18
 	"fmt"
19 19
 	"io"
20
+	"regexp"
20 21
 	"sort"
21 22
 	"strconv"
22 23
 	"strings"
@@ -27,6 +28,7 @@ import (
27 28
 )
28 29
 
29 30
 var commentStart = "//:" // Sentinel for comments on options
31
+var tailDigitsRE = regexp.MustCompile("[0-9]+$")
30 32
 
31 33
 // interactive starts a shell to read pprof commands.
32 34
 func interactive(p *profile.Profile, o *plugin.Options) error {
@@ -218,7 +220,15 @@ func parseCommandLine(input []string) ([]string, variables, error) {
218 220
 	cmd, args := input[:1], input[1:]
219 221
 	name := cmd[0]
220 222
 
221
-	c := pprofCommands[cmd[0]]
223
+	c := pprofCommands[name]
224
+	if c == nil {
225
+		// Attempt splitting digits on abbreviated commands (eg top10)
226
+		if d := tailDigitsRE.FindString(name); d != "" && d != name {
227
+			name = name[:len(name)-len(d)]
228
+			cmd[0], args = name, append([]string{d}, args...)
229
+			c = pprofCommands[name]
230
+		}
231
+	}
222 232
 	if c == nil {
223 233
 		return nil, nil, fmt.Errorf("Unrecognized command: %q", name)
224 234
 	}

+ 20
- 1
internal/driver/interactive_test.go View File

@@ -247,6 +247,16 @@ func TestInteractiveCommands(t *testing.T) {
247 247
 				"ignore":    "ignore",
248 248
 			},
249 249
 		},
250
+		{
251
+			"top10 --cum focus1 -ignore focus2",
252
+			map[string]string{
253
+				"functions": "true",
254
+				"nodecount": "10",
255
+				"cum":       "true",
256
+				"focus":     "focus1|focus2",
257
+				"ignore":    "ignore",
258
+			},
259
+		},
250 260
 		{
251 261
 			"dot",
252 262
 			map[string]string{
@@ -288,14 +298,23 @@ func TestInteractiveCommands(t *testing.T) {
288 298
 				"output":    "out",
289 299
 			},
290 300
 		},
301
+		{
302
+			"999",
303
+			nil, // Error
304
+		},
291 305
 	}
292 306
 
293 307
 	for _, tc := range testcases {
294 308
 		cmd, vars, err := parseCommandLine(strings.Fields(tc.input))
295
-		vars = applyCommandOverrides(cmd, vars)
309
+		if tc.want == nil && err != nil {
310
+			// Error expected
311
+			continue
312
+		}
296 313
 		if err != nil {
297 314
 			t.Errorf("failed on %q: %v", tc.input, err)
315
+			continue
298 316
 		}
317
+		vars = applyCommandOverrides(cmd, vars)
299 318
 
300 319
 		for n, want := range tc.want {
301 320
 			if got := vars[n].stringValue(); got != want {