[ Back | Previous | Next ]

Option parsing for the main() method?

Package:
java.lang.*
Product:
JDK
Release:
1.0.x
Related Links:
General
General
General
General
General
General
General
General
General
Runtime
System
Comment:
/**
 * Starts the application.
 * @param args an array of command-line arguments
 */
public static void main(java.lang.String[] argv) {
	// Insert code to start the application here.
	int msgnum = -1;
	int optind;
	String usage = "Usage: ServerMonitor [-v] [-u url] [-t interval (ms)] ";
	String url = null;
	String filename = null;
	String intrvl = null;
	boolean verbosity = false;
	for (optind = 0; optind < argv.length; optind++) {
		System.out.println(""+ argv[optind]);
		if (argv[optind].equals("-u")) {
			url = argv[++optind];
		} else
			if (argv[optind].equals("-f")) {
				filename = argv[++optind];
			} else
				if (argv[optind].equals("-v")) {
					verbosity = true;
				} else
					if (argv[optind].equals("-t")) {
						intrvl = argv[++optind];
					} else
						if (argv[optind].equals("--")) {
							optind++;
							break;
						} else
							if (argv[optind].startsWith("-")) {
								System.out.println("" + usage);
								System.out.println("-- Invalid option " + argv[optind]);
								System.exit(1);
							} else {
								break;
							}
	}
}
1