자바에서 제공하지 않는 기능인 명령 옵션을 파싱하려면 부가적인 작업이 필요합니다. 이미 unix나 c에서는 getopt를 이용하여 명령행 옵션을 처리하고 있으나 java에서는 일일이 split을 해줘야 합니다.
여기에 gnu의 getop API를 이용하여 명령행 옵션을 받아들이는 샘플은 다음과 같습니다.
받아들일 옵션은 -h, -w, -p, -c를 받아들이며 각각 매핑은 아래와 같이 LongOpt를 이용하여 문자열을 기술하면 매핑됩니다.
이 결과를 이용하여 switch 안에서 처리하고 싶은 작업을 진행하면 됩니다.
여기에 gnu의 getop API를 이용하여 명령행 옵션을 받아들이는 샘플은 다음과 같습니다.
import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;
public class Main {
public void process(final String [] args) throws Exception {
processCommandLine(args);
}
private void processCommandLine(String[] args) throws Exception {
String programName = System.getProperty("program.name", "JBoss Chameleon");
String sopts = "-:h:w:p:c:";
LongOpt[] lopts =
{
new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'),
new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V'),
new LongOpt("wastype", LongOpt.REQUIRED_ARGUMENT, null, 'w'),
new LongOpt("sourcedd", LongOpt.REQUIRED_ARGUMENT, null, 'c'),
new LongOpt("target", LongOpt.REQUIRED_ARGUMENT, null, 'p')
};
Getopt getopt = new Getopt(programName, args, sopts, lopts);
int code;
String arg;
while ((code = getopt.getopt()) != -1)
{
switch (code)
{
case ':':
case '?':
// for now both of these should exit with error status
System.exit(1);
break; // for completeness
case 1:
System.err.println(programName +
": unused non-option argument: " +
getopt.getOptarg());
case 'h':
// show command line help
System.out.println("usage: " + programName + " [options]");
System.out.println();
System.out.println("options:");
System.out.println(" -h, --help Show this help message");
System.out.println(" -V, --version Show version information");
System.out.println(" -- Stop processing options");
System.out.println(" -w, --wastype=[<name>] Set proprietary app server; Must be one of belows");
System.out.println(" jeus4, jeus5, jeus6, weblogic8, weblogic9, weblogic10");
System.out.println(" -c, --sourcedd=<filename> Set deployement descriptor for converting");
System.out.println(" -p, --target=<jboss_ver> Set JBoss target version; Must be jboss4,jboss5 or jboss6");
System.out.println();
System.exit(0);
break; // for completeness
case 'c':
{
// set the patch URL
arg = getopt.getOptarg();
System.out.println("SOURCE DEPLOYMENT DESCRIPTOR : " + arg);
break;
}
case 'w':
{
// set the patch URL
arg = getopt.getOptarg();
System.out.println("SOURCE APP SERVER : " + arg);
break;
}
case 'p':
{
// set the patch URL
arg = getopt.getOptarg();
System.out.println("TARGET APP SERVER : " + arg);
break;
}
case 'V':
{
System.out.println("JBoss Chameleon 1.0 M1");
System.out.println();
System.out.println("Distributable under LGPL license.");
System.out.println("See terms of license at gnu.org.");
System.out.println();
System.exit(0);
break; // for completness
}
default:
// this should not happen,
// if it does throw an error so we know about it
throw new Error("unhandled option code: " + code);
}
}
}
public static void main(String[] args) throws Exception {
Main main = new Main();
main.process(args);
}
}
받아들일 옵션은 -h, -w, -p, -c를 받아들이며 각각 매핑은 아래와 같이 LongOpt를 이용하여 문자열을 기술하면 매핑됩니다.
이 결과를 이용하여 switch 안에서 처리하고 싶은 작업을 진행하면 됩니다.
http://www.javapattern.info/trackback/320





