Command Line Input
If you want your Kotlin program to use command line arguments, you must
modify the definition of main() so that it has an array of strings as
its sole parameter. The type of this parameter must be declared as
Array<String>. The name given to it has no special significance, but
args or argv are common and sensible choices.
You will also need to check that the correct number of arguments have been
supplied to the program. Arrays in Kotlin have a size property that you
can examine in order to check this. If the required number of arguments
haven’t been provided, it will be necessary to terminate the program
prematurely, with a suitable error message.
For example, if a program requires a filename as a single command line argument, you could check that this argument is present in the manner shown below. (Equivalent code in C and Python is also provided, for comparison.)
import kotlin.system.exitProcess
fun main(args: Array<String>) {
if (args.size != 1) {
println("Error: filename required as sole argument")
exitProcess(1)
}
// required argument available as args[0]
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
if (argc != 2) {
printf("Error: filename required as sole argument");
exit(1);
}
// required argument available as argv[1]
}
import sys
if len(sys.argv) != 2:
sys.exit("Error: filename required as sole argument")
# required argument available as sys.argv[1]
Kotlin stores arguments differently from C and Python.
In C, the first element of the argument array is the path to the executable file containing the program, and the second element of this array contains the first argument supplied to the program.
Python is like C. The first element in sys.argv, its list of command line
arguments, is the name of the Python program, and the second list element
is the first command line argument.
In Kotlin, the program name/file path is NOT stored in the array. Thus,
in the example above, args[0] will be the first argument supplied to the
program, not args[1].
If you are wondering about exitProcess(1) in the example above, its
purpose is to halt program execution, setting the program’s exit status
to 1.
By convention, an operating system will assume that a program has terminated normally if it has an exit status of zero1, and that it has terminated abnormally if the exit status has any non-zero integer value. It is good practice to signal program failure to the OS in this fashion.
Having the exit status available can sometimes be useful, e.g., if you are running the program from a shell script and need to halt the script if that program fails to run properly.
Task 3.1
-
Edit the file named
Args.kt, in thetasks/task3_1subdirectory of your repository.In this file, write a small Kotlin program that accepts command line arguments. Your
main()function should contain only two lines of code, which should print out the first and second command line arguments. Do not add anything else at this stage. -
Compile the program, then run it without supplying any arguments on the command line:
kotlin ArgsKtWhat happens?
-
Run the program with two command line arguments, e.g.,
kotlin ArgsKt arg1 arg2Then try running it like this:
kotlin ArgsKt 'arg1 arg2'What happens, and why do you see this behaviour?
-
Modify the program so that it tests for the existence of the two required command line arguments before attempting to print them. Use the code example above as a guide.
If the required arguments are not present, your code should call
exitProcess(), with a non-zero exit status.Recompile the program, then use the following commands to run it with varying numbers of command line arguments, displaying exit status each time:
kotlin ArgsKt arg1 echo $? kotlin ArgsKt arg1 arg2 echo $? kotlin ArgsKt arg1 arg2 arg3 echo $?Here,
$?is a shell variable that holds the exit status of the last command. This is what you would use in shell scripts to decide whether script execution should continue.
-
An exit status of 0 is the default. Thus you do not need to have an explicit
exitProcess(0)at the end of your program. You’d only need to use this if you wanted to halt the program before the end ofmain()and signal that this was a normal termination rather than an error of some kind. ↩