HelloWorld_6
Prev | Chapter 3. Quick Start | Next

## HelloWorld_6

Of course, sometimes usage information doesn't provide enough detail. How about some actual program help?

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

            opt1.setHelp("The number of times to say hello.");
            jsap.registerParameter(opt1);

            Switch sw1 = new Switch("verbose")
                            .setShortFlag('v')
                            .setLongFlag("verbose");
            sw1.setHelp("Requests verbose output.");
            jsap.registerParameter(sw1);

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

            opt2.setHelp("One or more names of people you would like to greet.");
            jsap.registerParameter(opt2);

            JSAPResult config = jsap.parse(args);

            if (!config.success()) {
                System.err.println();
                System.err.println("Usage: java "
                                    + Manual_HelloWorld_6.class.getName());
                System.err.println("                "
                                    + jsap.getUsage());
                System.err.println();
                // show full help as well
                System.err.println(jsap.getHelp());
                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_6 -n 2 -n 4

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

      -n <count>
            The number of times to say hello. (default: 1)

      [-v|--verbose]
            Requests verbose output.

      name1 name2 ... nameN
            One or more names of people you would like to greet. (default: World)

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

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

      -n <count>
            The number of times to say hello. (default: 1)

      [-v|--verbose]
            Requests verbose output.

      name1 name2 ... nameN
            One or more names of people you would like to greet. (default: World)

In `HelloWorld_3` you learned how to add a `Switch` (verbose). Combine a -h / --help switch with the `getHelp()` method as used here, and you've made your program much friendlier.

Prev | Up | Next
HelloWorld_5 | Home | HelloWorld_7