use command cli to parse args

This commit is contained in:
Imgaojp 2017-02-17 11:00:01 +08:00
commit aa8cb1b7c1
2 changed files with 232 additions and 0 deletions

View File

@ -0,0 +1,119 @@
package com.gabongao.jvm;
import org.apache.commons.cli.*;
/**
*            + +
*         + +
*               
*               ++ + + +
*        +
*                +
*              
*                + +
*           
*             
*              + + + +
*                 Code is far away from bug with the animal protecting
*              +     神兽保佑,代码无bug
*             
*               +
*               + +
*                  
*                  
*           + + + +
*            
*            + + + +
* Created by Imgaojp on 2017/2/16.
*/
public class Cmd {
boolean helpFlag = false;
boolean versionFlag = false;
String cpOption = "";
String className = "";
String[] args;
/**
* getter
*
* @return
*/
public boolean isHelpFlag() {
return helpFlag;
}
public boolean isVersionFlag() {
return versionFlag;
}
public String getCpOption() {
return cpOption;
}
public String getClassName() {
return className;
}
public String[] getArgs() {
return args;
}
/**
* reset properties
*/
public void reset() {
this.helpFlag = false;
this.versionFlag = false;
this.cpOption = "";
this.className = "";
this.args = null;
}
/**
* ParseCmd
*
* @param args
* @throws Exception
*/
public void parseCmd(String[] args) throws Exception {
CommandLineParser parser = new BasicParser();
Options options = new Options();
options.addOption("version", false, "print version and exit");
options.addOption("?", "help", false, "print help message");
options.addOption("cp", "classpath", true, "classpath");
CommandLine commandLine = parser.parse(options, args);
if (commandLine.hasOption("help") || commandLine.hasOption("?")) {
this.helpFlag = true;
}
if (commandLine.hasOption("version")) {
this.versionFlag = true;
}
if (commandLine.hasOption("cp") || commandLine.hasOption("classpath")) {
if (commandLine.hasOption("cp")) {
this.cpOption = commandLine.getOptionValue("cp");
} else {
this.cpOption = commandLine.getOptionValue("classpath");
}
}
String[] commandLineArgs = commandLine.getArgs();
if (commandLineArgs.length > 0) {
this.className = commandLineArgs[0];
}
if (commandLineArgs.length > 1) {
this.args = new String[commandLineArgs.length - 1];
System.arraycopy(commandLineArgs, 1, this.args, 0, commandLineArgs.length - 1);
}
}
/**
* 打印帮助
*
* @param args
*/
public static void printUsage(String[] args) {
System.out.println("Usage: java [-options] class [args...]\n");
}
}

View File

@ -0,0 +1,113 @@
package test.com.gabongao.jvm;
import com.gabongao.jvm.Cmd;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import junit.framework.*;
import java.util.Arrays;
/**
* Cmd Tester.
*
* @author <Authors name>
* @since <pre>þÔÂ 17, 2017</pre>
* @version 1.0
*/
public class CmdTest extends TestCase {
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
*
* Method: parseCmd(String[] args)
*
*/
@Test
public void testParseCmd() throws Exception {
//TODO: Test goes here...
Cmd cmd = new Cmd();
cmd.parseCmd(new String[]{"-cp","classpath1","MyApp"});
assertEquals("classpath1",cmd.getCpOption());
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
cmd.parseCmd(new String[]{"-version","MyApp","classpath1","args2"});
assertEquals(false,cmd.isHelpFlag());
assertEquals(true,cmd.isVersionFlag());
assertEquals("MyApp",cmd.getClassName());
assertEquals(true, Arrays.asList(new String[]{"classpath1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("",cmd.getCpOption());
cmd.reset();
cmd.parseCmd(new String[]{"-help","MyApp","classpath1","args2"});
assertEquals("",cmd.getCpOption());
assertEquals(true,cmd.isHelpFlag());
assertEquals(false,cmd.isVersionFlag());
assertEquals(true, Arrays.asList(new String[]{"classpath1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
cmd.parseCmd(new String[]{"-?","MyApp","classpath1","args2"});
assertEquals("",cmd.getCpOption());
assertEquals(true,cmd.isHelpFlag());
assertEquals(false,cmd.isVersionFlag());
assertEquals(true, Arrays.asList(new String[]{"classpath1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
cmd.parseCmd(new String[]{"-classpath","classpath1","MyApp","args1","args2"});
assertEquals("classpath1",cmd.getCpOption());
assertEquals(false,cmd.isHelpFlag());
assertEquals(false,cmd.isVersionFlag());
assertEquals(true, Arrays.asList(new String[]{"args1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
cmd.parseCmd(new String[]{"-classpath","classpath1","MyApp","args1","args2","-version"});
assertEquals("classpath1",cmd.getCpOption());
assertEquals(false,cmd.isHelpFlag());
assertEquals(true,cmd.isVersionFlag());
assertEquals(true, Arrays.asList(new String[]{"args1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
cmd.parseCmd(new String[]{"-classpath","classpath1","-version","MyApp","args1","args2"});
assertEquals("classpath1",cmd.getCpOption());
assertEquals(false,cmd.isHelpFlag());
assertEquals(true,cmd.isVersionFlag());
assertEquals(true, Arrays.asList(new String[]{"args1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
cmd.parseCmd(new String[]{"-classpath","classpath1","MyApp","-version","args1","args2"});
assertEquals("classpath1",cmd.getCpOption());
assertEquals(false,cmd.isHelpFlag());
assertEquals(true,cmd.isVersionFlag());
assertEquals(true, Arrays.asList(new String[]{"args1", "args2"}).equals(Arrays.asList(cmd.getArgs())));
assertEquals("MyApp",cmd.getClassName());
cmd.reset();
}
/**
*
* Method: printUsage(String[] args)
*
*/
@Test
public void testPrintUsage() throws Exception {
//TODO: Test goes here...
}
}