Skip to content

system, args and the terminal

These need no import:

FunctionDoes
print(value)writes the value and a line break on the output. Any value: print(rex) shows its fields.
write(value)the same, without the line break.
read_line -> String?the next line of the input, without its line break; none at the end of the input.
fn main {
var total: Int := 0
while true {
let line: String? := read_line
if line = none {
break
}
match line.to_int {
none => print("not a number: {line}")
n => total += n
}
}
print("total: {total}")
}
Terminal window
printf '4\n5\nsix\n' | kova run sum.kova
Terminal window
not a number: six
total: 9

A program that stops on an error (a broken contract, an index out of bounds) writes it on the error output and ends with the code 101.

For colors on the terminal, see String; for dated lines on the error output, log.

import system
FunctionGives
system.os -> Stringthe system the program is made for: "linux", "windows", "mac", "android", "ios" or "web". Known when the program is made, so one file can pick what suits each system.
system.cores -> Inthow many processors this program may use: a good number for workers(…).
system.env(name: String) -> String?the value of an environment variable, none if it is not set.
system.args -> List<String>the words given after the program’s name: kova run app.kova a b, ./app a b.
system.exit(code: Int)ends the program at once with this exit code (0 means success).
system.clock -> Floatthe seconds since 1970, with a fraction: for measuring how long something takes.
system.sleep(seconds: Float)waits that long.
system.run(program: String) -> system.Finished or system.RunErrorruns a program without arguments, and waits for it.
system.run(program) args(words: List<String>) -> system.Finished or system.RunErrorthe same with arguments.
import system
fn main {
print(system.env("KOVA_NO_SUCH_NAME"))
match system.run("kova-no-such-program") args(["--version"]) {
ok(done) => print("{done.output} (code {done.code})")
err(e) => print(e)
}
}
none
NotFound("kova-no-such-program")
  • The program is looked for in the folders of PATH when its name holds no folder.
  • Its input is empty; system.Finished holds output (what it wrote), errors (what it wrote on the error output) and code (its exit code, 128 + the signal’s number if a signal stopped it).
  • A program that ran and failed is still ok: look at code. err means it could not start: NotFound(program), Denied(program) or Other(program).
  • The words go to the program as they are. No shell reads them, so a space, a ; or a $ in one is just a character: nothing from a user can turn into another command.