Skip to content

Commit

Permalink
treat \ as escape only before a separator
Browse files Browse the repository at this point in the history
  • Loading branch information
shueybubbles authored and alecthomas committed May 16, 2022
1 parent 5538b7f commit 29685e7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
7 changes: 5 additions & 2 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,15 @@ func SplitEscaped(s string, sep rune) (out []string) {
}
escaped := false
token := ""
for _, ch := range s {
for i, ch := range s {
switch {
case escaped:
if ch != sep {
token += `\`
}
token += string(ch)
escaped = false
case ch == '\\':
case ch == '\\' && i < len(s)-1:
escaped = true
case ch == sep && !escaped:
out = append(out, token)
Expand Down
26 changes: 26 additions & 0 deletions mapper_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build !windows
// +build !windows

package kong_test

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestPathMapper(t *testing.T) {
var cli struct {
Path string `arg:"" type:"path"`
}
p := mustNew(t, &cli)

_, err := p.Parse([]string{"/an/absolute/path"})
require.NoError(t, err)
require.Equal(t, "/an/absolute/path", cli.Path)

_, err = p.Parse([]string{"-"})
require.NoError(t, err)
require.Equal(t, "-", cli.Path)
}
22 changes: 6 additions & 16 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"reflect"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -412,25 +413,14 @@ func TestFileMapper(t *testing.T) {
_ = cli.File.Close()
_, err = p.Parse([]string{"testdata/missing.txt"})
require.Error(t, err)
require.Contains(t, err.Error(), "missing.txt: no such file or directory")
_, err = p.Parse([]string{"-"})
require.NoError(t, err)
require.Equal(t, os.Stdin, cli.File)
}

func TestPathMapper(t *testing.T) {
var cli struct {
Path string `arg:"" type:"path"`
if runtime.GOOS == "windows" {
require.Contains(t, err.Error(), "missing.txt: The system cannot find the file specified.")
} else {
require.Contains(t, err.Error(), "missing.txt: no such file or directory")
}
p := mustNew(t, &cli)

_, err := p.Parse([]string{"/an/absolute/path"})
require.NoError(t, err)
require.Equal(t, "/an/absolute/path", cli.Path)

_, err = p.Parse([]string{"-"})
require.NoError(t, err)
require.Equal(t, "-", cli.Path)
require.Equal(t, os.Stdin, cli.File)
}

func TestMapperPlaceHolder(t *testing.T) {
Expand Down
44 changes: 44 additions & 0 deletions mapper_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//go:build windows
// +build windows

package kong_test

import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestWindowsPathMapper(t *testing.T) {
var cli struct {
Path string `short:"p" type:"path"`
Files []string `short:"f" type:"path"`
}
wd, err := os.Getwd()
require.NoError(t, err, "Getwd failed")
p := mustNew(t, &cli)

_, err = p.Parse([]string{`-p`, `c:\an\absolute\path`, `-f`, `c:\second\absolute\path\`, `-f`, `relative\path\file`})
require.NoError(t, err)
require.Equal(t, `c:\an\absolute\path`, cli.Path)
require.Equal(t, []string{`c:\second\absolute\path\`, wd + `\relative\path\file`}, cli.Files)
}

func TestWindowsFileMapper(t *testing.T) {
type CLI struct {
File *os.File `arg:""`
}
var cli CLI
p := mustNew(t, &cli)
_, err := p.Parse([]string{"testdata\\file.txt"})
require.NoError(t, err)
require.NotNil(t, cli.File)
_ = cli.File.Close()
_, err = p.Parse([]string{"testdata\\missing.txt"})
require.Error(t, err)
require.Contains(t, err.Error(), "missing.txt: The system cannot find the file specified.")
_, err = p.Parse([]string{"-"})
require.NoError(t, err)
require.Equal(t, os.Stdin, cli.File)
}
7 changes: 5 additions & 2 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -62,7 +63,9 @@ func TestChangeDirFlag(t *testing.T) {
p := Must(&cli)
_, err = p.Parse([]string{"-C", dir, "out.txt"})
require.NoError(t, err)
file, err = filepath.EvalSymlinks(file) // Needed because OSX uses a symlinked tmp dir.
require.NoError(t, err)
if runtime.GOOS != "windows" {
file, err = filepath.EvalSymlinks(file) // Needed because OSX uses a symlinked tmp dir.
require.NoError(t, err)
}
require.Equal(t, file, cli.Path)
}

0 comments on commit 29685e7

Please sign in to comment.