public static void main(String[] args) throws Exception {
        JSAP jsap = new JSAP();
        // create a flagged option we'll access using the id "count".
        // it's going to be an integer, with a default value of 1.
        // it's required (which has no effect since there's a default value)
        // its short flag is "n", so a command line containing "-n 5"
        //    will print our message five times.
        // it has no long flag.
        FlaggedOption opt1 = new FlaggedOption("count")

        JSAPResult config = jsap.parse(args);    

        for (int i = 0; i < config.getInt("count"); ++i) {
            System.out.println("Hello, World!");

That's pretty verbose. Later on you'll see ways to trim this down quite a bit. Here's what's going on:

  1. We create a new JSAP to do the parsing.

  2. We create a new FlaggedOption so we can tell the program how many times to print the message.

    1. It has an ID of "count", which is only used internally by our program to retrieve the value.

    2. It's using an IntegerStringParser to convert the value to an Integer, and it has a default value of 1.

    3. It's required, but since we're specifying a default value, the requirement will always be met.

    4. It has a short flag "n", so the syntax is "-n 5" to print the message five times, for example.

    5. It has no long flag.

  3. We register the flagged option with the parser we created in the beginning.

  4. We tell the parser to parse the command line arguments, and store the result in a variable called "config".

  5. We print the message.

The output of the program looks like this:

[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_2
Hello, World!

[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_2 -n 5
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!

So far, so good. Let's add a switch.