All or the previous examples showed command lines flawlessly typed by the user. This, of course, never happens. One way to handle this is to print usage information:

    public static void main(String[] args) throws Exception {
        JSAP jsap = new JSAP();
        FlaggedOption opt1 = new FlaggedOption("count")
        Switch sw1 = new Switch("verbose")
        UnflaggedOption opt2 = new UnflaggedOption("name")
        JSAPResult config = jsap.parse(args);    

        // check whether the command line was valid, and if it wasn't,
        // display usage information and exit.
        if (!config.success()) {
            System.err.println("Usage: java "
                                + Manual_HelloWorld_5.class.getName());
            System.err.println("                "
                                + jsap.getUsage());
        String[] names = config.getStringArray("name");
        for (int i = 0; i < config.getInt("count"); ++i) {
            for (int j = 0; j < names.length; ++j) {
                System.out.println((config.getBoolean("verbose") ? "Hello" : "Hi")
                                + ", "
                                + names[j]
                                + "!");
[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_5 -n 2 -n 4

Usage: java com.martiansoftware.jsap.examples.Manual_HelloWorld_5
                -n <count> [-v|--verbose] name1 name2 ... nameN

[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_5 --nosuchflag

Usage: java com.martiansoftware.jsap.examples.Manual_HelloWorld_5
                -n <count> [-v|--verbose] name1 name2 ... nameN

Note that the IDs of the "count" FlaggedOption and the "name" UnflaggedOption were used in the automatically-generated usage information. As of v1.4, this can be overridden via setUsageName(String) on any parameters. (Thanks to Andreas Hochsteger for the suggestion!)