Skip to content

Commit

Permalink
Allow - for stdout in path mapper (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
haines authored Mar 2, 2021
1 parent d48f4e5 commit 456575d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

Expand Down
4 changes: 3 additions & 1 deletion mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 456575d

Please sign in to comment.