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: ab22b14d928e58dc86c7a3addfa8b11b815c4093 (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 {
	if s.separator == "" {
		s.separator = defaultSeparator
	}

	return strings.Join(s.value, s.separator)
}

// 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) {
	if s.separator == "" {
		s.separator = defaultSeparator
	}

	for _, str := range s.value {
		result = append(result, strings.Split(str, s.separator)...)
	}

	return
}