-
Notifications
You must be signed in to change notification settings - Fork 1
/
arg_float.v
76 lines (65 loc) · 1.3 KB
/
arg_float.v
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module clive
import strconv
@[noinit]
pub struct FloatArg {
pub:
kind string @[required]
name string @[required]
alias string
description string
separator string
multiple bool
required bool
default string
choices []string
mut:
values []string
}
// vfmt off
pub fn FloatArg.new(attrs struct {
name string
alias string
description string
separator string = default_separator
multiple bool
required bool
default string
choices []string
}) IArg {
// vfmt on
return IArg(FloatArg{
kind: 'float'
name: attrs.name
alias: attrs.alias
description: attrs.description
separator: attrs.separator
multiple: attrs.multiple
required: attrs.required
default: attrs.default
choices: attrs.choices
})
}
fn (self FloatArg) validate(input string) ![]string {
error_message := err_msg('arg_invalid', arg: self.name)
vals := if self.multiple {
input.split(self.separator)
} else {
[input]
}
for v in vals {
strconv.atof64(v) or { return error(error_message) }
}
return vals
}
pub fn (self FloatArg) stored() ?[]string {
if self.values.len > 0 {
return self.values
}
return none
}
pub fn (mut self FloatArg) store(input string) ! {
self.values = self.validate(input)!
}
pub fn (mut self FloatArg) parse(input string) ! {
self.store(input)!
}