Command Line Flags in Squirrel

Our program will demonstrate how to use command-line flags in Java. Here’s the full source code:

import java.util.Arrays;

public class CommandLineFlags {
    public static void main(String[] args) {
        // Define default values for our flags
        String word = "foo";
        int numb = 42;
        boolean fork = false;
        String svar = "bar";

        // Parse command-line arguments
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            switch (arg) {
                case "-word":
                    if (i + 1 < args.length) {
                        word = args[++i];
                    }
                    break;
                case "-numb":
                    if (i + 1 < args.length) {
                        numb = Integer.parseInt(args[++i]);
                    }
                    break;
                case "-fork":
                    fork = true;
                    break;
                case "-svar":
                    if (i + 1 < args.length) {
                        svar = args[++i];
                    }
                    break;
            }
        }

        // Print out the parsed options
        System.out.println("word: " + word);
        System.out.println("numb: " + numb);
        System.out.println("fork: " + fork);
        System.out.println("svar: " + svar);
        System.out.println("tail: " + Arrays.toString(Arrays.copyOfRange(args, args.length - 1, args.length)));
    }
}

This Java program demonstrates how to handle command-line flags. Unlike the original example which used a dedicated flag parsing library, we’re implementing a simple parsing mechanism manually.

To experiment with this program, first compile it:

$ javac CommandLineFlags.java

Now you can run the program with various command-line arguments:

$ java CommandLineFlags -word opt -numb 7 -fork -svar flag
word: opt
numb: 7
fork: true
svar: flag
tail: []

If you omit flags, they will keep their default values:

$ java CommandLineFlags -word opt
word: opt
numb: 42
fork: false
svar: bar
tail: []

You can also provide additional arguments after the flags:

$ java CommandLineFlags -word opt a1 a2 a3
word: opt
numb: 42
fork: false
svar: bar
tail: [a3]

Note that in this simple implementation, all arguments after the last recognized flag will be considered as trailing arguments. This differs from the original example where flags had to appear before positional arguments.

To get help text, you would typically implement a separate -h or --help option:

case "-h":
case "--help":
    System.out.println("Usage of CommandLineFlags:");
    System.out.println("  -word <string>: a string (default \"foo\")");
    System.out.println("  -numb <int>: an int (default 42)");
    System.out.println("  -fork: a bool");
    System.out.println("  -svar <string>: a string var (default \"bar\")");
    System.exit(0);

This implementation provides a basic command-line flag parsing functionality in Java. For more complex needs, consider using a dedicated library like Apache Commons CLI or JCommander.