Debian Codegraph @

enum.go

raw symbols SCIP
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 "errors"
8
9 type enumValue string
10
11 var enumValues = make(map[*enumValue]map[string]struct{})
12
13 func (s *enumValue) Set(value string, opt Option) error {
14 es, ok := enumValues[s]
15 if !ok || es == nil {
16 return errors.New("this option has no values")
17 }
18 if _, ok := es[value]; !ok {
19 return errors.New("invalid value: " + value)
20 }
21 *s = enumValue(value)
22 return nil
23 }
24
25 func (s *enumValue) String() string {
26 return string(*s)
27 }
28
29 // Enum creates an option that can only be set to one of the enumerated strings
30 // passed in values. Passing nil or an empty slice results in an option that
31 // will always fail.
32 func Enum(name rune, values []string, helpvalue ...string) *string {
33 return CommandLine.Enum(name, values, helpvalue...)
34 }
35
36 func (s *Set) Enum(name rune, values []string, helpvalue ...string) *string {
37 var p string
38 s.EnumVarLong(&p, "", name, values, helpvalue...)
39 return &p
40 }
41
42 func EnumLong(name string, short rune, values []string, helpvalue ...string) *string {
43 return CommandLine.EnumLong(name, short, values, helpvalue...)
44 }
45
46 func (s *Set) EnumLong(name string, short rune, values []string, helpvalue ...string) *string {
47 var p string
48 s.EnumVarLong(&p, name, short, values, helpvalue...)
49 return &p
50 }
51
52 // EnumVar creates an enum option that defaults to the starting value of *p.
53 // If *p is not found in values then a reset of this option will fail.
54 func EnumVar(p *string, name rune, values []string, helpvalue ...string) Option {
55 return CommandLine.EnumVar(p, name, values, helpvalue...)
56 }
57
58 func (s *Set) EnumVar(p *string, name rune, values []string, helpvalue ...string) Option {
59 return s.EnumVarLong(p, "", name, values, helpvalue...)
60 }
61
62 func EnumVarLong(p *string, name string, short rune, values []string, helpvalue ...string) Option {
63 return CommandLine.EnumVarLong(p, name, short, values, helpvalue...)
64 }
65
66 func (s *Set) EnumVarLong(p *string, name string, short rune, values []string, helpvalue ...string) Option {
67 m := make(map[string]struct{})
68 for _, v := range values {
69 m[v] = struct{}{}
70 }
71 enumValues[(*enumValue)(p)] = m
72 return s.VarLong((*enumValue)(p), name, short, helpvalue...)
73 }