Skip to content

Commit

Permalink
Switching sub-cmd by ARGV[0] is enabled to work when exec by full path.
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Aug 24, 2015
1 parent 54f3b1d commit b3da867
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
9 changes: 5 additions & 4 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"regexp"

"github.com/mitchellh/go-homedir"
"github.com/monochromegane/conflag"
Expand All @@ -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
}
Expand Down
29 changes: 29 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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",
Expand Down

0 comments on commit b3da867

Please sign in to comment.