-
Notifications
You must be signed in to change notification settings - Fork 0
/
arg_string.v
76 lines (66 loc) · 1.3 KB
/
arg_string.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
@[noinit]
pub struct StringArg {
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 StringArg.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(StringArg{
kind: 'string'
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 StringArg) 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 {
if v.trim_space().is_blank() {
return error(error_message)
}
}
return vals
}
pub fn (self StringArg) stored() ?[]string {
if self.values.len > 0 {
return self.values
}
return none
}
pub fn (mut self StringArg) store(input string) ! {
self.values = self.validate(input)!
}
pub fn (mut self StringArg) parse(input string) ! {
self.store(input)!
}