[Jsap-users] Cmdline parsing
Jari Matilainen
vague at vague.se
Mon Feb 22 00:57:36 CST 2010
Hi,
I have a problem that I figure is down to how JSAP parses the options, namely if
I run a command like 'java client -c check_subsystem -a "-l QGPL -s EASY.PRD"',
the -a flaggedoption accepts a string which is to be sent to another class for
further parsing, but the client class tries to parse the string as if it was part
of the arguments.
I hope you can bring some clarity to the problem.
The part of my code this problem occurs in:
/**********************************************************************/
try
{
FlaggedOption opt1 = new FlaggedOption("cmd").setStringParser(JSAP.STRING_PARSER).setRequired(true).setShortFlag('c').setLongFlag("command").setAllowMultipleDeclarations(false);
FlaggedOption opt2 = new FlaggedOption("args").setStringParser(JSAP.STRING_PARSER).setRequired(false).setShortFlag('a').setLongFlag("args").setAllowMultipleDeclarations(false);
FlaggedOption opt3 = new FlaggedOption("warn").setStringParser(JSAP.LONG_PARSER).setRequired(false).setShortFlag('W').setLongFlag("warning").setAllowMultipleDeclarations(false);
FlaggedOption opt4 = new FlaggedOption("crit").setStringParser(JSAP.LONG_PARSER).setRequired(false).setShortFlag('C').setLongFlag("critical").setAllowMultipleDeclarations(false);
FlaggedOption opt5 = new FlaggedOption("tout").setStringParser(JSAP.INTEGER_PARSER).setRequired(false).setShortFlag('T').setLongFlag("timeout").setAllowMultipleDeclarations(false);
FlaggedOption opt6 = new FlaggedOption("port").setStringParser(JSAP.INTEGER_PARSER).setRequired(false).setShortFlag('P').setLongFlag("port").setAllowMultipleDeclarations(false);
FlaggedOption opt9 = new FlaggedOption("host").setStringParser(JSAP.STRING_PARSER).setRequired(false).setShortFlag('H').setLongFlag("host").setAllowMultipleDeclarations(false);
Switch opt7 = new Switch("debug").setShortFlag('d').setLongFlag("debug");
Switch opt8 = new Switch("help").setShortFlag('h').setLongFlag("help");
opt1.setHelp("Specify the java class to call");
jsap.registerParameter(opt1);
opt2.setHelp("Arguments to the java class in the form -a 'arg1 arg2 arg3 ... argN'");
jsap.registerParameter(opt2);
opt3.setHelp("Warning threshold");
jsap.registerParameter(opt3);
opt4.setHelp("Critical threshold");
jsap.registerParameter(opt4);
opt5.setHelp("Timeout in seconds");
jsap.registerParameter(opt5);
opt6.setHelp("Port to connect to");
jsap.registerParameter(opt6);
opt9.setHelp("Host to connect to");
jsap.registerParameter(opt9);
opt7.setHelp("Enable debugging output");
jsap.registerParameter(opt7);
opt8.setHelp("Display help");
jsap.registerParameter(opt8);
}
catch (Exception e)
{
System.out.println("Error building argument list: " + e.getMessage());
System.exit(2);
}
JSAPResult config = jsap.parse(args);
if(config.getBoolean("help"))
{
System.err.println("Usage: java " + client.class.getName());
System.err.println("\t" + jsap.getUsage());
System.err.println(jsap.getHelp());
System.exit(0);
}
if(!config.success())
{
System.err.println();
for(java.util.Iterator errs = config.getErrorMessageIterator(); errs.hasNext();)
{
System.err.println("Error: " + errs.next());
}
System.err.println();
System.err.println("Usage: java " + client.class.getName());
System.err.println();
System.err.println(jsap.getHelp());
System.exit(2);
}
String cmd = config.getString("cmd");
String cmdargs = config.getString("args", "");
String[] argv;
// The statement below is just for cosmetics. If argv isn't an empty string array, the argument parsing in the plugins
// would think there is one argument to parse
if(cmdargs.equals(""))
{
argv = new String[] {};
}
else
{
argv = cmdargs.split(" ");
}
long warn = config.getLong("warn", 1);
long crit = config.getLong("crit", 1);
int timeout = config.getInt("tout", 10);
int port = config.getInt("port", 3333);
String host = config.getString("host","localhost");
boolean debug = config.getBoolean("debug");
/**********************************************************************/
/Jari
More information about the Jsap-users
mailing list