HelloWorld_5
Prev | Chapter 3. Quick Start | Next

## HelloWorld_5

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")
                                    .setStringParser(JSAP.INTEGER_PARSER)
                                    .setDefault("1")
                                    .setRequired(true)
                                    .setShortFlag('n')
                                    .setLongFlag(JSAP.NO_LONGFLAG);

            jsap.registerParameter(opt1);

            Switch sw1 = new Switch("verbose")
                            .setShortFlag('v')
                            .setLongFlag("verbose");

            jsap.registerParameter(sw1);

            UnflaggedOption opt2 = new UnflaggedOption("name")
                                    .setStringParser(JSAP.STRING_PARSER)
                                    .setDefault("World")
                                    .setRequired(true)
                                    .setGreedy(true);

            jsap.registerParameter(opt2);

            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();
                System.err.println("Usage: java "
                                    + Manual_HelloWorld_5.class.getName());
                System.err.println("                "
                                    + jsap.getUsage());
                System.err.println();
                System.exit(1);
            }

            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!)

Prev | Up | Next
HelloWorld_4 | Home | HelloWorld_6