Command Line Arguments in Fortress

Here’s the translated code and explanation in Markdown format suitable for Hugo:

Command-line arguments are a common way to parameterize execution of programs. For example, java MyProgram arg1 arg2 uses arg1 and arg2 as arguments to the MyProgram class.

import java.util.Arrays;

public class CommandLineArguments {
    public static void main(String[] args) {
        // args provides access to raw command-line arguments.
        // Note that unlike some other languages, args does not include the program name.
        String[] argsWithoutProg = args;
        
        // You can get individual args with normal indexing.
        String arg = args[2];

        System.out.println(Arrays.toString(args));
        System.out.println(Arrays.toString(argsWithoutProg));
        System.out.println(arg);
    }
}

To experiment with command-line arguments, it’s best to compile the Java file first.

$ javac CommandLineArguments.java
$ java CommandLineArguments a b c d
[a, b, c, d]
[a, b, c, d]
c

In Java, command-line arguments are passed to the main method as an array of strings. Unlike some other languages, this array does not include the program name as the first element. The entire array represents the arguments passed to the program.

You can access individual arguments using array indexing, just like in the example above where we access the third argument with args[2].

Next, we’ll look at more advanced command-line processing with option parsing libraries available in Java.