From 456575db97b112641945b4f219495d0509e58fc0 Mon Sep 17 00:00:00 2001 From: Andrew Haines Date: Tue, 2 Mar 2021 05:26:10 +0000 Subject: [PATCH] Allow `-` for stdout in path mapper (#147) --- README.md | 4 ++-- mapper.go | 4 +++- mapper_test.go | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 78341a1..47bd1cb 100644 --- a/README.md +++ b/README.md @@ -385,8 +385,8 @@ function `NamedMapper(name, mapper)`. | Name | Description |-------------------|--------------------------------------------------- -| `path` | A path. ~ expansion is applied. -| `existingfile` | An existing file. ~ expansion is applied. `-` is accepted for stdin. +| `path` | A path. ~ expansion is applied. `-` is accepted for stdout, and will be passed unaltered. +| `existingfile` | An existing file. ~ expansion is applied. `-` is accepted for stdin, and will be passed unaltered. | `existingdir` | An existing directory. ~ expansion is applied. | `counter` | Increment a numeric field. Useful for `-vvv`. Can accept `-s`, `--long` or `--long=N`. diff --git a/mapper.go b/mapper.go index 183ec09..7eb77fb 100644 --- a/mapper.go +++ b/mapper.go @@ -554,7 +554,9 @@ func pathMapper(r *Registry) MapperFunc { if err != nil { return err } - path = ExpandPath(path) + if path != "-" { + path = ExpandPath(path) + } target.SetString(path) return nil } diff --git a/mapper_test.go b/mapper_test.go index 091322c..a8b84e9 100644 --- a/mapper_test.go +++ b/mapper_test.go @@ -378,3 +378,18 @@ func TestFileMapper(t *testing.T) { require.NoError(t, err) require.Equal(t, os.Stdin, cli.File) } + +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) +}