File: string_array_flag.go

package info (click to toggle)
golang-github-grpc-ecosystem-grpc-gateway.v2 2.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,148 kB
  • sloc: javascript: 352; makefile: 136; sh: 26
file content (33 lines) | stat: -rw-r--r-- 931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package utilities

import (
	"flag"
	"strings"
)

// flagInterface is an cut down interface to `flag`
type flagInterface interface {
	Var(value flag.Value, name string, usage string)
}

// StringArrayFlag defines a flag with the specified name and usage string.
// The return value is the address of a `StringArrayFlags` variable that stores the repeated values of the flag.
func StringArrayFlag(f flagInterface, name string, usage string) *StringArrayFlags {
	value := &StringArrayFlags{}
	f.Var(value, name, usage)
	return value
}

// StringArrayFlags is a wrapper of `[]string` to provider an interface for `flag.Var`
type StringArrayFlags []string

// String returns a string representation of `StringArrayFlags`
func (i *StringArrayFlags) String() string {
	return strings.Join(*i, ",")
}

// Set appends a value to `StringArrayFlags`
func (i *StringArrayFlags) Set(value string) error {
	*i = append(*i, value)
	return nil
}