-
Notifications
You must be signed in to change notification settings - Fork 0
/
uwu.v
40 lines (35 loc) · 929 Bytes
/
uwu.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
module uwu
import uwu.str
import os
// get_args return the command-line arguments.
@[inline]
pub fn get_args() []string {
return os.args[1..].clone()
}
// need_args ensures that the user have passed at least
// the number of command-line arguments to the program.
pub fn need_args(min int) ![]string {
args := get_args()
argc := args.filter(!it.starts_with('-')).len
if argc < min {
msg := 'not enough arguments. expecting ${min}, got ${argc}'
return error_with_code(msg, 1)
}
return args
}
// exec execute a command and return it's output.
// this will throw an error if the return code is not zero.
@[inline]
pub fn exec(args ...string) !string {
cmd := safe_cmd(...args)
res := os.execute(cmd)
out := res.output.trim_space()
if res.exit_code != 0 {
return error_with_code(out, res.exit_code)
}
return out
}
@[inline]
fn safe_cmd(args ...string) string {
return args.map(str.safe_quote(it)).join(' ')
}