CLI Tools

Kodo provides built-in functions for reading command-line arguments, standard input, and controlling process exit. Combined with File I/O and JSON, you can build complete CLI tools.

Command-Line Arguments

args() -> List<String>

Returns all command-line arguments as a List<String>, including the binary name as the first element.

module cli_args {
    meta {
        purpose: "demonstrates reading command-line arguments"
        version: "0.1.0"
    }

    fn main() -> Int {
        let arg_list: List<String> = args()
        println("arguments received")
        return 0
    }
}

Compile and run:

kodoc build cli_args.ko -o cli_args
./cli_args foo bar baz

Standard Input

readln() -> String

Reads a single line from standard input, stripping the trailing newline.

println("Enter your name:")
let name: String = readln()
println(f"Hello, {name}!")

Process Exit

exit(code: Int)

Exits the process immediately with the given exit code.

if error_occurred {
    println("fatal error")
    exit(1)
}

File I/O

Kodo provides a complete set of file and directory operations:

BuiltinParametersReturnsDescription
file_readpath: StringResult<String, String>Read file contents
file_writepath: String, content: StringResult<String, String>Write file (overwrite)
file_appendpath: String, content: StringResult<String, String>Append to file
file_existspath: StringBoolCheck if file exists
file_deletepath: StringInt (0=ok)Delete a file
dir_listpath: StringList<String>List directory entries
dir_existspath: StringBoolCheck if directory exists

Extended Math

BuiltinParametersReturnsDescription
sqrtx: Float64Float64Square root
powbase: Float64, exp: Float64Float64Power
sinx: Float64Float64Sine
cosx: Float64Float64Cosine
logx: Float64Float64Natural logarithm
floorx: Float64Float64Floor
ceilx: Float64Float64Ceiling
roundx: Float64Float64Round to nearest
rand_intmin: Int, max: IntIntRandom integer in range

Next Steps