Command line quickstasrt
1) download the library.2) create a class like this:
public static class Cmd{
@Param(shortHelp="prefix all lines (including blank ones)")
public boolean all;
@Param(shortHelp="prefix to be printed")
public String prefix;
//this a naming convention, a field named like this receieves rest of the parameters
public File[] restParams;
}
IMPORTANT!: Field must be public to get picked up by the parser3) parse the command line in your main method
Cmd cmd = new Cmd();
ParamParser parser = new ParamParser();
parser.parseDefault(args, cmd);
... and you are ready to run your code using properties from "cmd".
NOTICE!: boolean field is interpreted as switch ("cmd.all" is set by adding "-a" parameter)
NOTICE!: other field types require an extra parameter ("cmd.prefix" is set by adding "-p myprefix" parameters)
:) plus if you call the app with -h or --help, you get the help screen like this:
options: -a --all prefix all lines (including blank ones) -p --prefix prefix to be printed
Full example for this simple usage: Simple.java
Getting started - Config
This small example is just enough to get you started/interested:Write a config(db.cfg):
driver =org.gjt.mm.mysql.Driver
url =jdbc:mysql://localhost/cbapp?charset=utf8
username=db_user
password=db_pass
It looks like classic properties file, but wait,write a following code:
public class Connector {
public String url;
public String username;
public String password;
public static void main(String[] args) throws Exception{
ConfigParser cParser = new ConfigParser();
Connector connector = new Connector();
cParser.parse(new FileInputStream("db.cfg"), connector);
Connection connection = connector.getConnection();
}
private void getConnection() throws SQLException{
DriverManager.getConnection(url,username,password);
}
//this setter will be called by the parser, we will not save the value but just add it as a new driver
public void setDriver(Class driver) throws SQLException, InstantiationException, IllegalAccessException {
DriverManager.registerDriver((Driver) driver.newInstance());
}
}
try it, it works !! No converting values checking, or if the value even exists before that ...