main and Exit Codes
main is a program’s entry point and comes in four forms, chosen by whether the program needs command-line arguments, a return value, or both: no return value, an exit code, command-line arguments, and the combination.
| Form | Signature | Use |
|---|---|---|
| Simplest | func main() | Just runs; returns nothing, takes nothing |
| Returns an exit code | func main() -> int | Returns a result the caller can check |
| Takes arguments | func main(args: string[]) | Reads command-line arguments |
| Combined | func main(args: string[]) -> int | Both at once |
The entry function needs no visibility modifier; public changes nothing either way.
Exit Codes
In the -> int form, the integer returned by return becomes the process exit code: a program whose main runs return 7 exits with code 7. 0 means all is well; non-zero values carry whatever meaning you assign them. Exit codes let you check whether a program ended successfully without parsing its printed output.
$ mush run app.ms
$ echo $?
0In PowerShell, read the exit code from $LASTEXITCODE; in bash, from $?.
Command-Line Arguments
Add an args: string[] parameter to main and the program receives command-line arguments. Running mush run args.ms a b c puts "a", "b", "c" in args, in order; args.ms itself is not included.
func main(args: string[]) -> int
{
if args.Length == 0
{
print("no args");
return 1;
}
print($"hello, {args[0]}");
return 0;
}$ mush run args.ms ada
hello, ada
$ echo $?
0
$ mush run args.ms
no args
$ echo $?
1The type of args, string[], is a string array: args.Length gives the count and args[0] indexes an element; the syntax unfolds across later pages. Declaring it as list<string> is equally valid — the two forms are equivalent, and the language passes the argument in the declared type: write string[] and you get an array, write list<string> and you get a list. Script entry points default to string[].
Notes
- The exit code is an
int; returning non-zero is not a compile error — it just hands information to the caller. - Without an explicit
-> int, the program exits with 0. argsmay be declared asstring[]orlist<string>; entry-point validation accepts only these two shapes and rejects any other parameter form at compile time.
