This is the mail archive of the frysk@sources.redhat.com mailing list for the frysk project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Getopt examples.


In the spirit of a tutorial on using getopt for your programs here is a
list of frysk files that now use getopt:

frysk-imports/frysk/expr/RunCppParser.java
frysk-imports/inua/util/readelf.java
frysk-imports/frysk/junit/Runner.java

Some things to note:

To create a parser use:

Parser parser = new Parser ("command name", "version string")
or 
parser = new Parser("command name, "version string", boolean longOnly)
if you want to set whether the parser runs in long option only mode.

When adding an option to the parser with
parser.add(new Option()...)

Option is an abstract class. There are many different constructors for
Option that have a variety of short name, long name, description, and
argument name combinations. 

If you have a command line option that takes an argument you need to
also provide an argument name so that getopt knows that the option takes
an argument.
parser.add(new Option ("long-name", 'l', "description of long name",
"<long name variable>"){});.

Option needs to implement the method:
void parsed(String arg0) throws OptionException.

The argument is always a string, if you need something else you have to
parse it from that string. For example for integers use:
int i = Integer.parseInt(String s);

putting this all together we have:

parser.add(new Option ("long-name", 'l', "description of long name",
"<long name variable>" 
{
	void parsed (String arg0) throws OptionException
	{
		try {
		global_int = Integer.parseInt(arg0);
		catch (Exception _) {
			throw new OptionException("Argument " + arg0 + "was not an integer");
	}
}

The convention is to put all the adds into a single function for
clarity.

Going back to the Parser class, you can override the validate() function
to check for more complicated inter-option issues. For example if you
absolutely need to get a pid or a command you would do that here.

Parser parser = new Parser("ftrace", "0.0", true) {
  protected void validate() throws OptionException {
    if (! requestedPid && commandAndArguments == null)
      throw new OptionException("no command or PID specified");
  }
};

set the usage message with:

parser.setHeader("Usage String");

and finally the parsing stage. The simple rule: anything that is not an
option or an argument is a "file" which has to be handled through the
function notifyFile with the FileArgumentCallback class.

LinkedList globalOtherArguments = new LinkedList();

parser.parse(args, new FileArgumentCallback() {
	public void notifyFile(String arg) throws OptionException
		{
			globalOtherArguments.add(arg);
		}
};

This concludes my tutorial. Lets see if I can find a spot for this on
the website somewhere.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]