Command line Argument Processing


Parameter Details
args The command line arguments. Assuming that the main method is invoked by the Java launcher, args will be non-null, and will have no null elements.

Argument processing using GWT ToolBase


Example, the MyProgramHandler class now handles two arguments: an input file (file) and a count value (count). It uses ArgHandlerFile for handling the file argument and ArgHandlerInt for handling the count argument.

import com.google.gwt.dev.util.arg.ArgHandlerFile;
import com.google.gwt.dev.util.arg.ArgHandlerInt;
import com.google.gwt.dev.util.arg.OptionName;
 
import java.io.File;
 
public class MyProgramHandler extends ToolBase
{
	protected File file;
	protected int count;
 
	public MyProgramHandler()
	{
		this.registerHandler(new ArgHandlerFile()
			{
			@Override
			public void setFile(File file)
			{
				MyProgramHandler.this.file = file;
			}
 
			@Override
			public String[] getTagArgs()
			{
				return new String[]{"file"};
			}
 
			@Override
			public String getPurpose()
			{
				return "Specify input file";
			}
		});
 
		this.registerHandler(new ArgHandlerInt()
		{
			@Override
			public void setInt(int value)
			{
				MyProgramHandler.this.count = value;
			}
 
			@Override
			public String[] getTagArgs()
			{
				return new String[]{"count"};
			}
 
			@Override
			public String getPurpose()
			{
				return "Specify count value";
			}
		});
	}
 
	public File getFile()
	{
		return file;
	}
 
	public int getCount()
	{
		return count;
	}
 
	public static void main(String[] args)
	{
	MyProgramHandler myHandler = new MyProgramHandler();
		if (myHandler.processArgs(args))
		{
			System.out.println(String.format("File: %s; Count: %d",
					myHandler.getFile(), myHandler.getCount()));
			// Perform other operations based on the file and count
		}
		System.exit(0);
	}
}
 

When running this program from the command line, you might use something like:

java -cp your-classpath-here com.yourpackage.MyProgramHandler -file input.txt -count 10

Replace your-classpath-here and com.yourpackage.MyProgramHandler with the appropriate classpath and package structure for your MyProgramHandler class. -file input.txt and -count 10 are the command-line arguments for specifying the input file and count value, respectively. Adjust the file name and count value as needed.


Processing arguments by hand


When the command-line syntax for an application is simple, it is reasonable to do the command argument processing entirely in custom code.

In this example, we will present a series of simple case studies. In each case, the code will produce error messages if the arguments are unacceptable, and then call System.exit(1) to tell the shell that the command has failed. (We will assume in each case that the Java code is invoked using a wrapper whose name is "myapp".)

A command with no arguments

In this case-study, the command requires no arguments. The code illustrates that args.length gives us the number of command line arguments.

public class Main
{
	public static void main(String[] args)
	{
		if (args.length > 0)
		{
			System.err.println("usage: myapp");
			System.exit(1);
		}
		// Run the application
		System.out.println("It worked");
	}
}

A command with two arguments

In this case-study, the command requires at precisely two arguments.

public class Main
{
	public static void main(String[] args)
	{
		if (args.length != 2)
		{
			System.err.println("usage: myapp <arg1> <arg2>");
			System.exit(1);
		}
		// Run the application
		System.out.println("It worked: " + args[0] + ", " + args[1]);
	}
}

Note that if we neglected to check args.length, the command would crash if the user ran it with too few command-line arguments.

A command with "flag" options and at least one argument

In this case-study, the command has a couple of (optional) flag options, and requires at least one argument after the options.

package tommy;
 
public class Main
{
	public static void main(String[] args)
	{
		boolean feelMe = false;
		boolean seeMe = false;
		int index;
		loop: for (index = 0; index < args.length; index++)
		{
			String opt = args[index];
			switch (opt)
			{
				case "-c":
					seeMe = true;
					break;
				case "-f":
					feelMe = true;
					break;
				case "-h":
					displayHelp();
					System.exit(0);
					break;
				default:
					if (!opt.isEmpty() && opt.charAt(0) == '-')
					{
						error("Unknown option: '" + opt + "'");
					}
					break loop;
			}
		}
		if (index >= args.length)
		{
			error("Missing argument(s)");
		}
 
		// Run the new application logic
		String userInput = args[index]; // Assuming this argument is required
		System.out.println("User input: " + userInput);
		System.out.println("See me: " + seeMe);
		System.out.println("Feel me: " + feelMe);
		// ... Perform operations based on user input and options
	}
 
	private static void error(String message)
	{
		if (message != null)
		{
			System.err.println(message);
		}
		displayHelp();
		System.exit(1);
	}
 
	private static void displayHelp()
	{
		System.err.println("Usage: myapp [-f] [-c] [-h] <arg>");
		System.err.println("Options:");
		System.err.println("  -f       : Set feelMe flag");
		System.err.println("  -c       : Set seeMe flag");
		System.err.println("  -h       : Display this help message");
	}
}
 

When executing this program from the command line, it could be used as follows:

java tommy.Main -c -f userInput

This example assumes that -c and -f are optional flags that set boolean values, and userInput is a required argument. Adjust the command-line arguments and the application logic as needed for your specific use case.

Basic Programs