Wednesday, April 2, 2008

Intralink Scripting: Processing Java Command Line Arguments

Using the Command Line in your Java Code

Accessing the command line arguments for a program is a very useful way to control its behavior during runtime. This is no different with Intralink, especially when running Intralink Scripting autonomously, where you can't prompt the user for information.

A little bit of digging will unveil the ILArgumentParser class. This allows access to the command line arguments used with Intralink. The following two functions are very helpful in working with this class.

Arguments are expected to be in this form:

ilink -- -loginfile login.txt

This function indicates whether a command line argument keyword exists:
public boolean isCmdLineArg ( String argName ) throws Exception {

String argSpec = "-" + argName.toLowerCase();

// Check command line args for argName
//
try {
ILArgumentParser ilargumentparser = Intralink.m_arguments;
if ( ilargumentparser.containsKey(argSpec) ) {
return true; // argName is a command line argument
}
}
catch (Exception e) {
// exception: command line arg does not exist, ignore
}

return false; // argName is not a command line argument
}

This function return a command line argument value:
public String getCmdLineArg ( String argName ) throws Exception {

String argValue = null;
String argSpec = "-" + argName.toLowerCase();

// Check command line args for argName
//
try {
ILArgumentParser ilargumentparser = Intralink.m_arguments;
if ( ilargumentparser.containsKey(argSpec) ) {
argValue = ilargumentparser.getValue(argSpec);
}
}
catch (Exception e) {
// exception: command line arg does not exist, ignore
}

return argValue;
}

Example usage:
String loginFile = "login.txt"; // default value
if (isCmdLineArg("loginFile")) { loginFile = getCmdLineArg("loginFile"); }

No comments: