-
Notifications
You must be signed in to change notification settings - Fork 63
/
flags_int.go
49 lines (40 loc) · 1.21 KB
/
flags_int.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2012 Alex Ogier.
// Copyright (c) 2012 The Go Authors.
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package cmdfactory
import (
"strconv"
"github.com/spf13/pflag"
)
type intValue int
func newIntValue(val int, p *int) *intValue {
*p = val
return (*intValue)(p)
}
func (s *intValue) Set(val string) error {
i, err := strconv.Atoi(val)
if err != nil {
return err
}
*s = intValue(i)
return nil
}
func (s *intValue) Type() string {
return "int"
}
func (s *intValue) String() string {
return strconv.Itoa(int(*s))
}
// IntVar returns an instantiated flag for to an associated pointer string
// value with a given name, default value and usage line.
func IntVar(p *int, name string, value int, usage string) *pflag.Flag {
return VarF(newIntValue(value, p), name, usage)
}
// IntVarP is like IntVar, but accepts a shorthand letter that can be used
// after a single dash.
func IntVarP(p *int, name, shorthand string, value int, usage string) *pflag.Flag {
return VarPF(newIntValue(value, p), name, shorthand, usage)
}