| 1 | // Copyright 2013 Google Inc. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package getopt |
| 6 | |
| 7 | import "strings" |
| 8 | |
| 9 | type listValue []string |
| 10 | |
| 11 | func (s *listValue) Set(value string, opt Option) error { |
| 12 | a := strings.Split(value, ",") |
| 13 | // If this is the first time we are seen then nil out the |
| 14 | // default value. |
| 15 | if opt.Count() <= 1 { |
| 16 | *s = nil |
| 17 | } |
| 18 | *s = append(*s, a...) |
| 19 | return nil |
| 20 | } |
| 21 | |
| 22 | func (s *listValue) String() string { |
| 23 | return strings.Join([]string(*s), ",") |
| 24 | } |
| 25 | |
| 26 | // List creates an option that returns a slice of strings. The parameters |
| 27 | // passed are converted from a comma separated value list into a slice. |
| 28 | // Subsequent occurrences append to the list. |
| 29 | func List(name rune, helpvalue ...string) *[]string { |
| 30 | return CommandLine.List(name, helpvalue...) |
| 31 | } |
| 32 | |
| 33 | func (s *Set) List(name rune, helpvalue ...string) *[]string { |
| 34 | p := []string{} |
| 35 | s.ListVar(&p, name, helpvalue...) |
| 36 | return &p |
| 37 | } |
| 38 | |
| 39 | func ListLong(name string, short rune, helpvalue ...string) *[]string { |
| 40 | return CommandLine.ListLong(name, short, helpvalue...) |
| 41 | } |
| 42 | |
| 43 | func (s *Set) ListLong(name string, short rune, helpvalue ...string) *[]string { |
| 44 | p := []string{} |
| 45 | s.ListVarLong(&p, name, short, helpvalue...) |
| 46 | return &p |
| 47 | } |
| 48 | |
| 49 | // ListVar creates a list option and places the values in p. If p is pointing |
| 50 | // to a list of values then those are considered the default values. The first |
| 51 | // time name is seen in the options the list will be set to list specified by |
| 52 | // the parameter to the option. Subsequent instances of the option will append |
| 53 | // to the list. |
| 54 | func ListVar(p *[]string, name rune, helpvalue ...string) Option { |
| 55 | return CommandLine.ListVar(p, name, helpvalue...) |
| 56 | } |
| 57 | |
| 58 | func (s *Set) ListVar(p *[]string, name rune, helpvalue ...string) Option { |
| 59 | return s.ListVarLong(p, "", name, helpvalue...) |
| 60 | } |
| 61 | |
| 62 | func ListVarLong(p *[]string, name string, short rune, helpvalue ...string) Option { |
| 63 | return CommandLine.ListVarLong(p, name, short, helpvalue...) |
| 64 | } |
| 65 | |
| 66 | func (s *Set) ListVarLong(p *[]string, name string, short rune, helpvalue ...string) Option { |
| 67 | opt := s.VarLong((*listValue)(p), name, short, helpvalue...) |
| 68 | return opt |
| 69 | } |