From b3da8675c1e93b957119d53c81be2c7af1ef2c30 Mon Sep 17 00:00:00 2001 From: Masataka Kuwabara Date: Mon, 24 Aug 2015 10:24:20 +0900 Subject: [PATCH] Switching sub-cmd by ARGV[0] is enabled to work when exec by full path. --- flag.go | 9 +++++---- flag_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/flag.go b/flag.go index 0ad3236..d822f3b 100644 --- a/flag.go +++ b/flag.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "io/ioutil" + "regexp" "github.com/mitchellh/go-homedir" "github.com/monochromegane/conflag" @@ -23,14 +24,14 @@ func (c *CLI) FlagParse(args []string) error { func (c *CLI) getCommandType(args []string) (s CommandStyle, err error) { s = ALIAS - switch args[0] { - case "xdg-open": + switch { + case regexp.MustCompile(`/?xdg-open$`).MatchString(args[0]): c.Type = OPEN return - case "pbpaste": + case regexp.MustCompile(`/?pbpaste`).MatchString(args[0]): c.Type = PASTE return - case "pbcopy": + case regexp.MustCompile(`/?pbcopy`).MatchString(args[0]): c.Type = COPY return } diff --git a/flag_test.go b/flag_test.go index b7c1132..7dbcd54 100644 --- a/flag_test.go +++ b/flag_test.go @@ -31,6 +31,16 @@ func TestCLIParse(t *testing.T) { TransLocalfile: true, }) + assert([]string{"/usr/bin/xdg-open", "http://example.com"}, CLI{ + Type: OPEN, + Host: defaultHost, + Port: defaultPort, + Allow: defaultAllow, + DataSource: "http://example.com", + TransLoopback: true, + TransLocalfile: true, + }) + assert([]string{"xdg-open"}, CLI{ Type: OPEN, Host: defaultHost, @@ -49,6 +59,15 @@ func TestCLIParse(t *testing.T) { TransLocalfile: true, }) + assert([]string{"/usr/bin/pbpaste", "--port", "1124"}, CLI{ + Type: PASTE, + Host: defaultHost, + Port: 1124, + Allow: defaultAllow, + TransLoopback: true, + TransLocalfile: true, + }) + assert([]string{"pbcopy", "hogefuga"}, CLI{ Type: COPY, Host: defaultHost, @@ -59,6 +78,16 @@ func TestCLIParse(t *testing.T) { TransLocalfile: true, }) + assert([]string{"/usr/bin/pbcopy", "hogefuga"}, CLI{ + Type: COPY, + Host: defaultHost, + Port: defaultPort, + Allow: defaultAllow, + DataSource: "hogefuga", + TransLoopback: true, + TransLocalfile: true, + }) + assert([]string{"lemonade", "--host", "192.168.0.1", "--port", "1124", "open", "http://example.com"}, CLI{ Type: OPEN, Host: "192.168.0.1",