Skip to content

Commit

Permalink
Add StringSetCSV
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Aug 30, 2018
1 parent e83f736 commit 84f0ae2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: go
go:
- "1.10"
script: go test ./...
script: go test ./...
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# flagvar
# flagvar

[![](https://godoc.org/github.com/sgreben/flagvar?status.svg)](http://godoc.org/github.com/sgreben/flagvar) [![](https://goreportcard.com/badge/github.com/sgreben/flagvar/goreportcard)](https://goreportcard.com/report/github.com/sgreben/flagvar) [![cover.run](https://cover.run/go/github.com/sgreben/flagvar.svg?style=flat&tag=golang-1.10)](https://cover.run/go?tag=golang-1.10&repo=github.com%2Fsgreben%2Fflagvar) [![Build Status](https://travis-ci.org/sgreben/flagvar.svg?branch=master)](https://travis-ci.org/sgreben/flagvar)

A collection of CLI argument types for the `flag` package.
A collection of CLI argument types for the `flag` package.

```go
import "github.com/sgreben/flagvar"
Expand All @@ -16,7 +16,7 @@ import "github.com/sgreben/flagvar/enum"
var enums flagvar.EnumsCSV
```

Or just copy & paste what you need. It's public domain.
Or just copy & paste what you need. It's public domain.

<!-- TOC -->

Expand Down Expand Up @@ -53,7 +53,7 @@ func main() {
```

```sh
$ go run main.go -set abc=xyz -url https://github.com
$ go run main.go -set abc=xyz -url https://github.com
# no error

$ go run main.go -set abc=xyz -url ://github.com
Expand Down Expand Up @@ -116,6 +116,7 @@ Here's a compact overview:
| [Regexps](https://godoc.org/github.com/sgreben/flagvar#Regexps) | [a-z]+ | []*regexp.Regexp |
| [Strings](https://godoc.org/github.com/sgreben/flagvar#Strings) | "xyxy" | []string |
| [StringSet](https://godoc.org/github.com/sgreben/flagvar#StringSet) | "xyxy" | []string |
| [StringSetCSV](https://godoc.org/github.com/sgreben/flagvar#StringSetCSV) | y,x,y | []string |
| [TCPAddr](https://godoc.org/github.com/sgreben/flagvar#TCPAddr) | 127.0.0.1:10 | net.TCPAddr |
| [TCPAddrs](https://godoc.org/github.com/sgreben/flagvar#TCPAddrs) | 127.0.0.1:10 | []net.TCPAddr |
| [TCPAddrsCSV](https://godoc.org/github.com/sgreben/flagvar#TCPAddrsCSV) | 127.0.0.1:10,:123 | []net.TCPAddr |
Expand Down Expand Up @@ -151,4 +152,4 @@ Here's a compact overview:
- Support "blind" usage
- Zero values should be useful
- Avoid introducing failure cases, handle any combination of parameters gracefully.
- All "obvious things to try" should work.
- All "obvious things to try" should work.
47 changes: 47 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package flagvar

import (
"fmt"
"sort"
"strings"
)
Expand Down Expand Up @@ -47,3 +48,49 @@ func (fv *StringSet) Set(v string) error {
func (fv *StringSet) String() string {
return strings.Join(fv.Values(), ",")
}

// StringSetCSV is a `flag.Value` for comma-separated string arguments.
// If `Accumulate` is set, the values of all instances of the flag are accumulated.
// The `Separator` field is used instead of the comma when set.
// If `CaseSensitive` is set to `true` (default `false`), the comparison is case-sensitive.
type StringSetCSV struct {
Separator string
Accumulate bool

Value map[string]bool
Values []string
}

// Help returns a string suitable for inclusion in a flag help message.
func (fv *StringSetCSV) Help() string {
separator := ","
if fv.Separator != "" {
separator = fv.Separator
}
return fmt.Sprintf("%q-separated list of strings", separator)
}

// Set is flag.Value.Set
func (fv *StringSetCSV) Set(v string) error {
separator := fv.Separator
if separator == "" {
separator = ","
}
if !fv.Accumulate || fv.Value == nil {
fv.Value = make(map[string]bool)
}
parts := strings.Split(v, separator)
for _, part := range parts {
part = strings.TrimSpace(part)
if fv.Value[part] {
continue
}
fv.Value[part] = true
fv.Values = append(fv.Values, part)
}
return nil
}

func (fv *StringSetCSV) String() string {
return strings.Join(fv.Values, ",")
}
28 changes: 28 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,31 @@ func TestStringSet(t *testing.T) {
t.Fail()
}
}

func TestStringSetCSV(t *testing.T) {
fv := flagvar.StringSetCSV{Accumulate: true}
var fs flag.FlagSet
fs.Var(&fv, "string-set-csv", "")

err := fs.Parse([]string{"-string-set-csv", "first,second", "-string-set-csv", "first"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(fv.Values, []string{"first", "second"}) {
t.Fail()
}
}

func TestStringSetCSVSeparator(t *testing.T) {
fv := flagvar.StringSetCSV{Separator: ";", Accumulate: true}
var fs flag.FlagSet
fs.Var(&fv, "string-set-csv", "")

err := fs.Parse([]string{"-string-set-csv", "first;second", "-string-set-csv", "first"})
if err != nil {
t.Fail()
}
if !reflect.DeepEqual(fv.Values, []string{"first", "second"}) {
t.Fail()
}
}

0 comments on commit 84f0ae2

Please sign in to comment.