The Artima Developer Community
Sponsored Link

Java Buzz Forum
Command Line Processing in Java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Nick Lothian

Posts: 397
Nickname: nicklothia
Registered: Jun, 2003

Nick Lothian is Java Developer & Team Leader
Command Line Processing in Java Posted: Jun 18, 2004 3:34 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Nick Lothian.
Original Post: Command Line Processing in Java
Feed Title: BadMagicNumber
Feed URL: http://feeds.feedburner.com/Badmagicnumber
Feed Description: Java, Development and Me
Latest Java Buzz Posts
Latest Java Buzz Posts by Nick Lothian
Latest Posts From BadMagicNumber

Advertisement

I've always found processing command line parameters in Java something of a hassle. It's not hard, but it can be error prone and having to write the same code over and over is annoying.

Here's a class I wrote that I find useful for this. Usage:

String usage = "java " + YourClass.class.getName() + " -a FirstParam -b
SecondParam";
CmdLineProcessor cmdLine = new CmdLineProcessor(args, usage);
cmdLine.setExpectedArgumentCount(4);
if (cmdLine.process()) {
	String firstParam = cmdLine.getArgument("-a");
	String secondParam = cmdLine.getArgument("-b");
	System.out.println("Parameters were " + firstParam + " and " +
secondParam);
}
// usage message is output if process() did not return true

The class:

public class CmdLineProcessor {
	private String[] args;
	private int expectedArgumentCount = 0;
	private String usageMessage;

	public CmdLineProcessor(String[] args, String usage) {
		this.args = args;
		setUsageMessage(usage);
	}

	public boolean process() {
		if ((args == null)
			|| (args.length != expectedArgumentCount)) {

			System.err.println(getUsageMessage());
			return false;
		}
		return true;
	}

	public String getArgument(String param)
			throws IllegalArgumentException {
		for (int i = 0; i < args.length; i++) {
			if (param.equals(args[i] )) {
				if (args.length > (i + 1)) {
					return args[i+1];
				}
			}
		}
		System.err.println(getUsageMessage());
		throw new IllegalArgumentException();
	}

	public int getExpectedArgumentCount() {
		return expectedArgumentCount;
	}

	public String getUsageMessage() {
		return usageMessage;
	}

	public void setExpectedArgumentCount(int i) {
		expectedArgumentCount = i;
	}

	public void setUsageMessage(String string) {
		usageMessage = string;
	}
}

Feel free to reuse it as you need.

Read: Command Line Processing in Java

Topic: QVGA Phones Previous Topic   Next Topic Topic: IBM developerWorks : Blogs

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use