Debian Codegraph @

v2/counter.go

raw symbols SCIP
1 // Copyright 2017 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 (
8 "fmt"
9 "strconv"
10 )
11
12 type counterValue int
13
14 func (b *counterValue) Set(value string, opt Option) error {
15 if value == "" {
16 *b++
17 } else {
18 v, err := strconv.ParseInt(value, 0, strconv.IntSize)
19 if err != nil {
20 if e, ok := err.(*strconv.NumError); ok {
21 switch e.Err {
22 case strconv.ErrRange:
23 err = fmt.Errorf("value out of range: %s", value)
24 case strconv.ErrSyntax:
25 err = fmt.Errorf("not a valid number: %s", value)
26 }
27 }
28 return err
29 }
30 *b = counterValue(v)
31 }
32 return nil
33 }
34
35 func (b *counterValue) String() string {
36 return strconv.Itoa(int(*b))
37 }
38
39 // Counter creates a counting flag stored as an int. Each time the option
40 // is seen while parsing the value is incremented. The value of the counter
41 // may be explicitly set by using the long form:
42 //
43 // --counter=5
44 // --c=5
45 //
46 // Further instances of the option will increment from the set value.
47 func Counter(name rune, helpvalue ...string) *int {
48 var p int
49 CommandLine.FlagLong((*counterValue)(&p), "", name, helpvalue...).SetFlag()
50 return &p
51 }
52
53 func (s *Set) Counter(name rune, helpvalue ...string) *int {
54 var p int
55 s.FlagLong((*counterValue)(&p), "", name, helpvalue...).SetFlag()
56 return &p
57 }
58
59 func CounterLong(name string, short rune, helpvalue ...string) *int {
60 var p int
61 CommandLine.FlagLong((*counterValue)(&p), name, short, helpvalue...).SetFlag()
62 return &p
63 }
64
65 func (s *Set) CounterLong(name string, short rune, helpvalue ...string) *int {
66 var p int
67 s.FlagLong((*counterValue)(&p), name, short, helpvalue...).SetFlag()
68 return &p
69 }
70
71 func CounterLongVar(p *int, name string, short rune, helpvalue ...string) Option {
72 return CommandLine.FlagLong((*counterValue)(p), name, short, helpvalue...).SetFlag()
73 }
74
75 func (s *Set) CounterLongVar(p *int, name string, short rune, helpvalue ...string) Option {
76 return s.FlagLong((*counterValue)(p), name, short, helpvalue...).SetFlag()
77 }