HelloWorld_Simple

If you want to minimize the amount of code handling the command line, JSAP offers a SimpleJSAP that does most of the work for you. An instance of SimpleJSAP works much like an instance of JSAP, but it accepts a command name, a command explanation, and an array of Parameter objects (there are several constructors that should cover most of the common cases, but you can also chain-invoke setters). If parsing fails for any reason, or if --help is specified, the user is fully informed, and the method messagePrinted() will return true.

    public static void main(String[] args) throws Exception {
        SimpleJSAP jsap = new SimpleJSAP( 
            "MyProgram", 
            "Repeats \"Hello, world!\" multiple times",
            new Parameter[] {
                new FlaggedOption( "count", JSAP.INTEGER_PARSER, "1", JSAP.REQUIRED, 'n', JSAP.NO_LONGFLAG, 
                    "The number of times to say hello." ),
                new QualifiedSwitch( "verbose", JSAP.STRING_PARSER, JSAP.NO_DEFAULT, JSAP.NOT_REQUIRED, 'v', "verbose", 
                    "Requests verbose output." ).setList( true ).setListSeparator( ',' ),
                new UnflaggedOption( "name", JSAP.STRING_PARSER, "World", JSAP.REQUIRED, JSAP.GREEDY, 
                    "One or more names of people you would like to greet." )
            }
        );
        
        JSAPResult config = jsap.parse(args);    
        if ( jsap.messagePrinted() ) System.exit( 1 );
                
        String[] names = config.getStringArray("name");
        String[] languages = config.getStringArray("verbose");
        for (int i = 0; i < languages.length; ++i) {
            System.out.println("language=" + languages[i]);
        }
        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_Simple -n 2 --verbose Zoidberg
Hello, Zoidberg!
Hello, Zoidberg!


[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_Simple --verbose:de Farnsworth
language=de
Hello, Farnsworth!


[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_Simple -v:de,en Branigan
language=de
language=en
Hello, Branigan!


[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_Simple Horrible_Gelatanous_Blob
Hi, Horrible_Gelatanous_Blob!


[mlamb@morbo]$ java com.martiansoftware.jsap.examples.Manual_HelloWorld_Simple --help

Usage:
  MyProgram [--help] -n <count> [(-v|--verbose)[:verbose1,verbose2,...,verboseN ]]
  name1 name2 ... nameN

Repeats "Hello, world!" multiple times


  [--help]
        Prints this help message.

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

  [(-v|--verbose)[:verbose1,verbose2,...,verboseN ]]
        Requests verbose output.

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