1package pflag
2
3import (
4	"fmt"
5	"strconv"
6)
7
8// -- float32 Value
9type float32Value float32
10
11func newFloat32Value(val float32, p *float32) *float32Value {
12	*p = val
13	return (*float32Value)(p)
14}
15
16func (f *float32Value) Set(s string) error {
17	v, err := strconv.ParseFloat(s, 32)
18	*f = float32Value(v)
19	return err
20}
21
22func (f *float32Value) Type() string {
23	return "float32"
24}
25
26func (f *float32Value) String() string { return fmt.Sprintf("%v", *f) }
27
28func float32Conv(sval string) (interface{}, error) {
29	v, err := strconv.ParseFloat(sval, 32)
30	if err != nil {
31		return 0, err
32	}
33	return float32(v), nil
34}
35
36// GetFloat32 return the float32 value of a flag with the given name
37func (f *FlagSet) GetFloat32(name string) (float32, error) {
38	val, err := f.getFlagType(name, "float32", float32Conv)
39	if err != nil {
40		return 0, err
41	}
42	return val.(float32), nil
43}
44
45// Float32Var defines a float32 flag with specified name, default value, and usage string.
46// The argument p points to a float32 variable in which to store the value of the flag.
47func (f *FlagSet) Float32Var(p *float32, name string, value float32, usage string) {
48	f.VarP(newFloat32Value(value, p), name, "", usage)
49}
50
51// Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
52func (f *FlagSet) Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
53	f.VarP(newFloat32Value(value, p), name, shorthand, usage)
54}
55
56// Float32Var defines a float32 flag with specified name, default value, and usage string.
57// The argument p points to a float32 variable in which to store the value of the flag.
58func Float32Var(p *float32, name string, value float32, usage string) {
59	CommandLine.VarP(newFloat32Value(value, p), name, "", usage)
60}
61
62// Float32VarP is like Float32Var, but accepts a shorthand letter that can be used after a single dash.
63func Float32VarP(p *float32, name, shorthand string, value float32, usage string) {
64	CommandLine.VarP(newFloat32Value(value, p), name, shorthand, usage)
65}
66
67// Float32 defines a float32 flag with specified name, default value, and usage string.
68// The return value is the address of a float32 variable that stores the value of the flag.
69func (f *FlagSet) Float32(name string, value float32, usage string) *float32 {
70	p := new(float32)
71	f.Float32VarP(p, name, "", value, usage)
72	return p
73}
74
75// Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
76func (f *FlagSet) Float32P(name, shorthand string, value float32, usage string) *float32 {
77	p := new(float32)
78	f.Float32VarP(p, name, shorthand, value, usage)
79	return p
80}
81
82// Float32 defines a float32 flag with specified name, default value, and usage string.
83// The return value is the address of a float32 variable that stores the value of the flag.
84func Float32(name string, value float32, usage string) *float32 {
85	return CommandLine.Float32P(name, "", value, usage)
86}
87
88// Float32P is like Float32, but accepts a shorthand letter that can be used after a single dash.
89func Float32P(name, shorthand string, value float32, usage string) *float32 {
90	return CommandLine.Float32P(name, shorthand, value, usage)
91}
92