Example 1

The example below parses the command line for options and arguments.

import java.util.List;

import org.freehep.util.argv.ArgumentFormatException;
import org.freehep.util.argv.ArgumentParser;
import org.freehep.util.argv.BooleanOption;
import org.freehep.util.argv.ListParameter;
import org.freehep.util.argv.MissingArgumentException;
import org.freehep.util.argv.StringOption;
import org.freehep.util.argv.StringParameter;

public class Example1 {
    public static void main(String[] args) {
    
        // Setup all options and arguments
        BooleanOption help = new BooleanOption("-help", "-h", "Show this help page", true);
        BooleanOption version = new BooleanOption("-version", "-v", "Show product version", true);
        BooleanOption rtti = new BooleanOption("-rtti", "-r", "Print RTTI to stdout");
        BooleanOption noGenerate = new BooleanOption("-nogenerate", "-n", "Do not generate any code");
        StringOption directory = new StringOption("-directory", "-d", "output dir", ".", "Output into directory instead of current directory");
        StringOption property = new StringOption("-property", "-p", "property directory", ".", "Read user property files from directory instead of current directory");
        BooleanOption force = new BooleanOption("-force", "-f", "Force overwriting of output files");
        BooleanOption ignore = new BooleanOption("-ignore", "-i", "Ignore errors from the parser");
        BooleanOption verbose = new BooleanOption("-verbose", "-V", "Verbose");
        StringParameter generatorParameter = new StringParameter("generator", "A generator, one of JavaInterfaceGenerator, JavaClassGenerator, CPPHeaderGenerator, PythonClassGenerator, ...");
        ListParameter files = new ListParameter("files", "files");

                // create the command line parser
        ArgumentParser cl = new ArgumentParser("Example1 - example based on the AID compiler");
        cl.add(help);
        cl.add(version);
        cl.add(rtti);
        cl.add(noGenerate);
        cl.add(directory);
        cl.add(property);
        cl.add(force);
        cl.add(ignore);
        cl.add(verbose);
        cl.add(generatorParameter);
        cl.add(files);

                // parse the user supplied arguments
        try {
            List extra = cl.parse(args);
            
            if( !extra.isEmpty() || help.getValue()) {
                cl.printUsage( System.out );
                return 0;
            }
            
            if (version.getValue()) {
                System.out.println("0.1");
                return 0;
            }
            
        } catch (MissingArgumentException mae) {
            System.out.println(mae.getMessage());
            return 0;
        } catch (ArgumentFormatException afe) {
            System.out.println(afe.getMessage());
            return 0;
        }
        
        // access options and arguments
        boolean showRTTI = rtti.getValue();
        String outDirectory = directory.getValue();
        List fileList = files.getValue();
    }
}