-
Notifications
You must be signed in to change notification settings - Fork 18
/
list.go
69 lines (56 loc) · 2.1 KB
/
list.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2013 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package getopt
import "strings"
type listValue []string
func (s *listValue) Set(value string, opt Option) error {
a := strings.Split(value, ",")
// If this is the first time we are seen then nil out the
// default value.
if opt.Count() <= 1 {
*s = nil
}
*s = append(*s, a...)
return nil
}
func (s *listValue) String() string {
return strings.Join([]string(*s), ",")
}
// List creates an option that returns a slice of strings. The parameters
// passed are converted from a comma seperated value list into a slice.
// Subsequent occurrences append to the list.
func List(name rune, helpvalue ...string) *[]string {
return CommandLine.List(name, helpvalue...)
}
func (s *Set) List(name rune, helpvalue ...string) *[]string {
p := []string{}
s.ListVar(&p, name, helpvalue...)
return &p
}
func ListLong(name string, short rune, helpvalue ...string) *[]string {
return CommandLine.ListLong(name, short, helpvalue...)
}
func (s *Set) ListLong(name string, short rune, helpvalue ...string) *[]string {
p := []string{}
s.ListVarLong(&p, name, short, helpvalue...)
return &p
}
// ListVar creats a list option and places the values in p. If p is pointing
// to a list of values then those are considered the default values. The first
// time name is seen in the options the list will be set to list specified by
// the parameter to the option. Subsequent instances of the option will append
// to the list.
func ListVar(p *[]string, name rune, helpvalue ...string) Option {
return CommandLine.ListVar(p, name, helpvalue...)
}
func (s *Set) ListVar(p *[]string, name rune, helpvalue ...string) Option {
return s.ListVarLong(p, "", name, helpvalue...)
}
func ListVarLong(p *[]string, name string, short rune, helpvalue ...string) Option {
return CommandLine.ListVarLong(p, name, short, helpvalue...)
}
func (s *Set) ListVarLong(p *[]string, name string, short rune, helpvalue ...string) Option {
opt := s.VarLong((*listValue)(p), name, short, helpvalue...)
return opt
}