github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/commands/cli/parse_test.go (about) 1 package cli 2 3 import ( 4 //"fmt" 5 "testing" 6 7 "github.com/jbenet/go-ipfs/commands" 8 ) 9 10 func TestOptionParsing(t *testing.T) { 11 subCmd := &commands.Command{} 12 cmd := &commands.Command{ 13 Options: []commands.Option{ 14 commands.Option{Names: []string{"b"}, Type: commands.String}, 15 }, 16 Subcommands: map[string]*commands.Command{ 17 "test": subCmd, 18 }, 19 } 20 21 opts, input, err := parseOptions([]string{"--beep", "-boop=lol", "test2", "-c", "beep", "--foo=5"}) 22 /*for k, v := range opts { 23 fmt.Printf("%s: %s\n", k, v) 24 } 25 fmt.Printf("%s\n", input)*/ 26 if err != nil { 27 t.Error("Should have passed") 28 } 29 if len(opts) != 4 || opts["beep"] != "" || opts["boop"] != "lol" || opts["c"] != "" || opts["foo"] != "5" { 30 t.Errorf("Returned options were defferent than expected: %v", opts) 31 } 32 if len(input) != 2 || input[0] != "test2" || input[1] != "beep" { 33 t.Errorf("Returned input was different than expected: %v", input) 34 } 35 36 _, _, err = parseOptions([]string{"-beep=1", "-boop=2", "-beep=3"}) 37 if err == nil { 38 t.Error("Should have failed (duplicate option name)") 39 } 40 41 path, args, sub := parsePath([]string{"test", "beep", "boop"}, cmd) 42 if len(path) != 1 || path[0] != "test" { 43 t.Errorf("Returned path was defferent than expected: %v", path) 44 } 45 if len(args) != 2 || args[0] != "beep" || args[1] != "boop" { 46 t.Errorf("Returned args were different than expected: %v", args) 47 } 48 if sub != subCmd { 49 t.Errorf("Returned command was different than expected") 50 } 51 }