Command Line Arguments in Minitab

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.

public class CommandLineArguments {
    public static void main(String[] args) {
        // args provides access to raw command-line arguments.
        // Note that unlike in some other languages, args does not include the program name.
        String[] argsWithoutProg = args;
        
        // To get an array that includes the program name, we can create a new array
        String[] argsWithProg = new String[args.length + 1];
        argsWithProg[0] = "CommandLineArguments";
        System.arraycopy(args, 0, argsWithProg, 1, args.length);

        // You can get individual args with normal indexing.
        String arg = args[2];  // Note: This will throw an exception if there are less than 3 arguments

        System.out.println(java.util.Arrays.toString(argsWithProg));
        System.out.println(java.util.Arrays.toString(argsWithoutProg));
        System.out.println(arg);
    }
}

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

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

Next, we’ll look at more advanced command-line processing with flags. In Java, this is typically done using libraries like Apache Commons CLI or Picocli.