/** 通过传递的参数,解析出对应的command * */ public static SkipCommand parse(List<String> optionList, ExecutionContext sessionContext) throws ODPSConsoleException { if (optionList.contains("-k")) { if (optionList.indexOf("-k") + 1 < optionList.size()) { int index = optionList.indexOf("-k"); // 一定要这样,不能 通过index来remove String stepStr = optionList.get(index + 1); int step = 0; ; try { step = Integer.valueOf(stepStr); } catch (NumberFormatException e) { throw new ODPSConsoleException( ODPSConsoleConstants.BAD_COMMAND + ":need int value[" + stepStr + "]"); } optionList.remove(optionList.indexOf("-k")); optionList.remove(stepStr); // skip只是设置一个context的内容,而且skip需要在parser阶段设置skip的值,因为命令需要在命令的统一入口skip掉command sessionContext.setStep(step); } } return null; }
/** 测试命令行->配置文件->默认值的优先级顺序 */ @Test public void testOptionsMix() throws Exception { String[] args; // 配置文件和命令行混合,级命令行优先 args = new String[] { "upload", "src/test/resources/test_data.txt", "test_table/ds='2113',pt='pttest'", "-record-delimiter=\t\t", }; ExecutionContext context = ExecutionContext.load("src/test/resources/test_config.ini"); DshipContext.INSTANCE.setExecutionContext(context); OptionsBuilder.buildUploadOption(args); assertEquals("charset not equal", "gbk", DshipContext.INSTANCE.get(Constants.CHARSET)); assertEquals( "FIELD_DELIMITER name not equal", "||", DshipContext.INSTANCE.get(Constants.FIELD_DELIMITER)); assertEquals( "RECORD_DELIMITER name not equal", "\t\t", DshipContext.INSTANCE.get(Constants.RECORD_DELIMITER)); assertEquals( "DISCARD_BAD_RECORDS name not equal", "true", DshipContext.INSTANCE.get(Constants.DISCARD_BAD_RECORDS)); assertEquals( "DATE_FORMAT_PATTERN name not equal", "yyyy-MM-dd HH:mm:ss", DshipContext.INSTANCE.get(Constants.DATE_FORMAT_PATTERN)); assertEquals( "NULL_INDICATOR name not equal", "NULL", DshipContext.INSTANCE.get(Constants.NULL_INDICATOR)); assertEquals("SCAN name not equal", "only", DshipContext.INSTANCE.get(Constants.SCAN)); }
@Before public void setup() throws ODPSConsoleException { DshipContext.INSTANCE.setExecutionContext(ExecutionContext.init()); }