Welcome to mirror list, hosted at ThFree Co, Russian Federation.

multi_string_flag.go - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1be02ef12fbcd14d2e0e0a6f08335692110a306a (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"errors"
	"strings"
)

var errMultiStringSetEmptyValue = errors.New("value cannot be empty")

const defaultSeparator = ","

// MultiStringFlag implements the flag.Value interface and allows a string flag
// to be specified multiple times on the command line.
//
// e.g.: -listen-http 127.0.0.1:80 -listen-http [::1]:80
type MultiStringFlag struct {
	value     []string
	separator string
}

// String returns the list of parameters joined with a commas (",")
func (s *MultiStringFlag) String() string {
	return strings.Join(s.value, s.sep())
}

// Set appends the value to the list of parameters
func (s *MultiStringFlag) Set(value string) error {
	if value == "" {
		return errMultiStringSetEmptyValue
	}

	s.value = append(s.value, value)
	return nil
}

// Split each flag
func (s *MultiStringFlag) Split() (result []string) {
	for _, str := range s.value {
		result = append(result, strings.Split(str, s.sep())...)
	}

	return
}

func (s *MultiStringFlag) sep() string {
	if s.separator == "" {
		return defaultSeparator
	}

	return s.separator
}