3. Arguments and absence
The program needs a file path. Accept an array of command-line arguments and use a nullable result when exactly one argument is not available.
Replace App.kalm:
space stats
fun requestedPath(args: Array<Text>) -> Text?:
if len(args) == 1:
return args[0]
return null
pub fun main(args: Array<Text>) -> Unit:
fix requested = requestedPath(args)
if requested != null:
sayLine(@"Will read $requested")
else:
sayLine("Usage: kalm run -- <file>")space stats
fun requestedPath(args: Array<Text>) -> Text? {
if len(args) == 1 {
return args[0]
}
return null
}
pub fun main(args: Array<Text>) -> Unit {
fix requested = requestedPath(args)
if requested != null {
sayLine(@"Will read $requested")
}
else {
sayLine("Usage: kalm run -- <file>")
}
}package stats;
public final class App {
static String requestedPath(String[] args) {
return args.length == 1 ? args[0] : null;
}
public static void main(String[] args) {
final String requested = requestedPath(args);
System.out.println(
requested != null
? "Will read " + requested
: "Usage: kalm run -- <file>"
);
}
}Kalm Script unavailable — showing KalmThis source file belongs to the project path; the script path uses one self-contained file.
Text? can contain text or null. Checking a stable fix binding refines it
inside the non-null branch. Checking the argument count first also avoids an
out-of-bounds access.
kalm.bat run -- notes.txtkalm.bat run -- notes.txtkalm run -- notes.txtkalm run -- notes.txtkalm run -- notes.txtkalm run -- notes.txtkalm run -- notes.txtkalm run -- notes.txtWill read notes.txtRun without an argument to see the usage message. Nothing reads the file yet.
The -- separates CLI options from your program's arguments.
Save the complete program below as App.kalms in a folder of your choice. Each lesson replaces this file; the shell does not load neighboring project files. The script host does not forward command-line arguments. Edit the args array to try a different path or an empty input.
fun requestedPath(args: Array<Text>) -> Text?:
if len(args) == 1:
return args[0]
return null
pub fun main(args: Array<Text>) -> Unit:
fix requested = requestedPath(args)
if requested != null:
sayLine(@"Will read $requested")
else:
sayLine("Usage: kalm run -- <file>")fun requestedPath(args: Array<Text>) -> Text? {
if len(args) == 1 {
return args[0]
}
return null
}
pub fun main(args: Array<Text>) -> Unit {
fix requested = requestedPath(args)
if requested != null {
sayLine(@"Will read $requested")
}
else {
sayLine("Usage: kalm run -- <file>")
}
}fun requestedPath(args: Array<Text>) -> Text?:
if len(args) == 1:
return args[0]
return null
fix args: Array<Text> = arrayOf("notes.txt")
fix requested = requestedPath(args)
if requested != null:
sayLine(@"Will read $requested")
else:
sayLine("Usage: kalm run -- <file>")fun requestedPath(args: Array<Text>) -> Text? {
if len(args) == 1 {
return args[0]
}
return null
}
fix args: Array<Text> = arrayOf("notes.txt")
fix requested = requestedPath(args)
if requested != null {
sayLine(@"Will read $requested")
}
else {
sayLine("Usage: kalm run -- <file>")
}static String requestedPath(String[] args) {
return args.length == 1 ? args[0] : null;
}
void main(String[] args) {
final String requested = requestedPath(args);
System.out.println(
requested != null
? "Will read " + requested
: "Usage: kalm run -- <file>"
);
}kalm.bat shell -f App.kalmskalm.bat shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmskalm shell -f App.kalmsWill read notes.txt