Java Code Examples for org.apache.commons.cli.BasicParser#parse()

The following examples show how to use org.apache.commons.cli.BasicParser#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributedLogServerApp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void run() {
    try {
        logger.info("Running distributedlog server : args = {}", Arrays.toString(args));
        BasicParser parser = new BasicParser();
        CommandLine cmdline = parser.parse(options, args);
        runCmd(cmdline);
    } catch (ParseException pe) {
        logger.error("Argument error : {}", pe.getMessage());
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (IllegalArgumentException iae) {
        logger.error("Argument error : {}", iae.getMessage());
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (ConfigurationException ce) {
        logger.error("Configuration error : {}", ce.getMessage());
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (IOException ie) {
        logger.error("Failed to start distributedlog server : ", ie);
        Runtime.getRuntime().exit(-1);
    } catch (ClassNotFoundException cnf) {
      logger.error("Failed to start distributedlog server : ", cnf);
      Runtime.getRuntime().exit(-1);
    }
}
 
Example 2
Source File: DistributedLogServerApp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private void run() {
    try {
        logger.info("Running distributedlog server : args = {}", Arrays.toString(args));
        BasicParser parser = new BasicParser();
        CommandLine cmdline = parser.parse(options, args);
        runCmd(cmdline);
    } catch (ParseException pe) {
        logger.error("Argument error : {}", pe.getMessage());
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (IllegalArgumentException iae) {
        logger.error("Argument error : {}", iae.getMessage());
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (ConfigurationException ce) {
        logger.error("Configuration error : {}", ce.getMessage());
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (IOException ie) {
        logger.error("Failed to start distributedlog server : ", ie);
        Runtime.getRuntime().exit(-1);
    }
}
 
Example 3
Source File: Balancer.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/** parse command line arguments */
private void parse(String[] args) {
  Options cliOpts = setupOptions();
  BasicParser parser = new BasicParser();
  CommandLine cl = null;
  try {
    try {
     cl = parser.parse(cliOpts, args);
    } catch (ParseException ex) {
      throw new IllegalArgumentException("args = " + Arrays.toString(args));
    }

    int newThreshold = Integer.parseInt(cl.getOptionValue("threshold", "10"));
    int iterationTime = Integer.parseInt(cl.getOptionValue("iter_len",
                               String.valueOf(maxIterationTime/(60 * 1000))));
    maxConcurrentMoves = Integer.parseInt(cl.getOptionValue("node_par_moves",
                                   String.valueOf(MAX_NUM_CONCURRENT_MOVES)));
    moveThreads = Integer.parseInt(cl.getOptionValue("par_moves",
                                   String.valueOf(MOVER_THREAD_POOL_SIZE)));
    maxIterationTime = iterationTime * 60 * 1000L;
    
    threshold = checkThreshold(newThreshold);
    System.out.println("Running with threshold of " + threshold
        + " and iteration time of " + maxIterationTime + " milliseconds");
  } catch (RuntimeException e) {
    printUsage(cliOpts);
    throw e;
  }
}
 
Example 4
Source File: App.java    From ripme with MIT License 5 votes vote down vote up
/**
 * Tries to parse commandline arguments.
 * @param args Array of commandline arguments.
 * @return CommandLine object containing arguments.
 */
private static CommandLine getArgs(String[] args) {
    BasicParser parser = new BasicParser();
    try {
        return parser.parse(getOptions(), args, false);
    } catch (ParseException e) {
        logger.error("[!] Error while parsing command-line arguments: " + Arrays.toString(args), e);
        System.exit(-1);
        return null;
    }
}
 
Example 5
Source File: ImageIngesterCLI.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
/**
 * Parses command line arguments to load the configuration file.
 *
 * @throws ExitException
 *             Help option specified, syntax error, or error loading configuration file
 */
private void parse() throws ExitException {
    final BasicParser parser = new BasicParser();
    CommandLine cl = null;
    try {
        cl = parser.parse(OPTIONS, args);
    } catch (final ParseException e) {
        help();

    }
    if (cl.hasOption(OPTION_HELP_SHORT)) {
        help();
    }
    if (cl.hasOption(OPTION_FILE_SHORT) && cl.hasOption(OPTION_CLASSPATH_SHORT)) {
        throw new ExitException("May not specify both of the command line options " + OPTION_FILE_SHORT + " and "
            + OPTION_CLASSPATH_SHORT);
    }
    if (cl.hasOption(OPTION_FILE_SHORT)) {
        final String filePath = cl.getOptionValue(OPTION_FILE_SHORT);
        config = loadConfigFromFilepath(filePath);
    } else {
        config = loadConfigFromClasspath(DEFAULT_CONFIG_FILE_NAME);
    }
    if (cl.hasOption(OPTION_CLASSPATH_SHORT)) {
        final String file = cl.getOptionValue(OPTION_CLASSPATH_SHORT);
        config = loadConfigFromClasspath(file);
    }
}
 
Example 6
Source File: ParserTrainer.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 主文件
 * 
 * @param args
 *            : 训练文件;模型文件;循环次数
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
	args = new String[2];
	args[0] = "./tmp/CoNLL2009-ST-Chinese-train.txt";
	args[1] = "./tmp/modelConll.gz";
	Options opt = new Options();

	opt.addOption("h", false, "Print help for this application");
	opt.addOption("iter", true, "iterative num, default 50");
	opt.addOption("c", true, "parameters 1, default 1");

	BasicParser parser = new BasicParser();
	CommandLine cl;
	try {
		cl = parser.parse(opt, args);
	} catch (Exception e) {
		System.err.println("Parameters format error");
		return;
	}
	
	if (args.length == 0 || cl.hasOption('h')) {
		HelpFormatter f = new HelpFormatter();
		f.printHelp(
				"Tagger:\n"
						+ "ParserTrainer [option] train_file model_file;\n",
				opt);
		return;
	}
	args = cl.getArgs();
	String datafile = args[0];
	String modelfile = args[1];
	int maxite = Integer.parseInt(cl.getOptionValue("iter", "50"));
	float c = Float.parseFloat(cl.getOptionValue("c", "1"));
	
	ParserTrainer trainer = new ParserTrainer(modelfile);
	trainer.train(datafile, maxite, c);
}
 
Example 7
Source File: JointParerTrainer.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 主文件
 * 
 * @param args
 *            : 训练文件;模型文件;循环次数
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
	//		args = new String[2];
	//		args[0] = "./tmp/malt.train";
	//		args[1] = "./tmp/Malt2Model.gz";
	Options opt = new Options();

	opt.addOption("h", false, "Print help for this application");
	opt.addOption("iter", true, "iterative num, default 50");
	opt.addOption("c", true, "parameters 1, default 1");

	BasicParser parser = new BasicParser();
	CommandLine cl;
	try {
		cl = parser.parse(opt, args);
	} catch (Exception e) {
		System.err.println("Parameters format error");
		return;
	}

	if (args.length == 0 || cl.hasOption('h')) {
		HelpFormatter f = new HelpFormatter();
		f.printHelp(
				"Tagger:\n"
						+ "ParserTrainer [option] train_file model_file;\n",
						opt);
		return;
	}
	args = cl.getArgs();
	String datafile = args[0];
	String modelfile = args[1];
	int maxite = Integer.parseInt(cl.getOptionValue("iter", "50"));
	float c = Float.parseFloat(cl.getOptionValue("c", "1"));

	JointParerTrainer trainer = new JointParerTrainer(modelfile);
	trainer.train(datafile, maxite, c);
}
 
Example 8
Source File: JointParerTester.java    From fnlp with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

		Options opt = new Options();

		opt.addOption("h", false, "Print help for this application");

		BasicParser parser = new BasicParser();
		CommandLine cl;
		try {
			cl = parser.parse(opt, args);
		} catch (Exception e) {
			System.err.println("Parameters format error");
			return;
		}

		if (args.length == 0 || cl.hasOption('h')) {
			HelpFormatter f = new HelpFormatter();
			f.printHelp(
					"Tagger:\n"
							+ "ParserTester [option] model_file test_file result_file;\n",
					opt);
			return;
		}

		String[] args1 = cl.getArgs();
		String modelfile  = args1[0]; 
		String testfile = args1[1]; 
		String resultfile = args1[2];

		JointParerTester tester = new JointParerTester(modelfile);
		tester.test(testfile, resultfile, "UTF-8");
	}
 
Example 9
Source File: AbstractCommandLineTool.java    From kieker with Apache License 2.0 5 votes vote down vote up
private CommandLine parseCommandLineArguments(final Options options, final String[] arguments) {
	final BasicParser parser = new BasicParser();

	try {
		return parser.parse(options, arguments);
	} catch (final ParseException ex) {
		// Note that we append ex.getMessage() to the log message on purpose to improve the
		// logging output on the console.
		LOGGER.error("An error occurred while parsing the command line arguments: {}", ex.getMessage(), ex);
		LOGGER.info("Use the option `--{}` for usage information", CMD_OPT_NAME_HELP_LONG);

		return null;
	}
}
 
Example 10
Source File: MonitorServiceApp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private void run() {
    try {
        logger.info("Running monitor service.");
        BasicParser parser = new BasicParser();
        CommandLine cmdline = parser.parse(options, args);
        runCmd(cmdline);
    } catch (ParseException pe) {
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (IOException ie) {
        logger.error("Failed to start monitor service : ", ie);
        Runtime.getRuntime().exit(-1);
    }
}
 
Example 11
Source File: Tool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public int runCmd(String[] args) throws Exception {
    try {
        BasicParser parser = new BasicParser();
        CommandLine cmdline = parser.parse(getOptions(), args);
        return runCmd(cmdline);
    } catch (ParseException e) {
        printUsage();
        return -1;
    }
}
 
Example 12
Source File: CommandLineUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static CommandLine parseOptions(final String[] args, final Options opts) {
    final BasicParser parser = new BasicParser();
    final CommandLine cl;
    try {
        cl = parser.parse(opts, args);
    } catch (ParseException e) {
        throw new IllegalArgumentException(e);
    }
    return cl;
}
 
Example 13
Source File: Tool.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public int runCmd(String[] args) throws Exception {
    try {
        BasicParser parser = new BasicParser();
        CommandLine cmdline = parser.parse(getOptions(), args);
        return runCmd(cmdline);
    } catch (ParseException e) {
        printUsage();
        return -1;
    }
}
 
Example 14
Source File: MonitorServiceApp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private void run() {
    try {
        logger.info("Running monitor service.");
        BasicParser parser = new BasicParser();
        CommandLine cmdline = parser.parse(options, args);
        runCmd(cmdline);
    } catch (ParseException pe) {
        printUsage();
        Runtime.getRuntime().exit(-1);
    } catch (IOException ie) {
        logger.error("Failed to start monitor service : ", ie);
        Runtime.getRuntime().exit(-1);
    }
}
 
Example 15
Source File: WordClusterM.java    From fnlp with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {
	
	/**
	 * 分析命令参数
	 */
	Options opt = new Options();

	opt.addOption("path", true, "保存路径");
	opt.addOption("res", true, "评测结果保存路径");
	opt.addOption("slot", true, "槽大小");
	opt.addOption("thd", true, "线程个数");

	BasicParser parser = new BasicParser();
	CommandLine cl;
	try {
		cl = parser.parse(opt, args);
	} catch (Exception e) {
		System.err.println("Parameters format error");
		return;
	}
	
	int threads = Integer.parseInt(cl.getOptionValue("thd", "3"));
	System.out.println("线程数量:"+threads);
	
	int slotsize = Integer.parseInt(cl.getOptionValue("slot", "20"));
	System.out.println("槽大小:"+slotsize);
	
	String file = cl.getOptionValue("path", "./tmp/SogouCA.mini.txt");
	System.out.println("数据路径:"+file);
	
	String resfile = cl.getOptionValue("res", "./tmp/cluster.txt");
	System.out.println("测试结果:"+resfile);
	
	long starttime = System.currentTimeMillis();
	SougouCA sca = new SougouCA(file);
	
	WordClusterM wc = new WordClusterM(threads);
	wc.slotsize = slotsize;
	wc.read(sca);
	
	wc.startClustering();
	wc.saveModel(resfile+".m");
	wc.saveTxt(resfile);
	wc = (WordClusterM) WordCluster.loadFrom(resfile+".m");
	wc.saveTxt(resfile+"1");
	long endtime = System.currentTimeMillis();
	System.out.println("Total Time:"+(endtime-starttime)/60000);
	System.out.println("Done");
	System.exit(0);
}
 
Example 16
Source File: WordCluster.java    From fnlp with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @param args
 * @throws Exception 
 */
public static void main(String[] args) throws Exception {

	/**
	 * 分析命令参数
	 */
	Options opt = new Options();

	opt.addOption("path", true, "保存路径");
	opt.addOption("res", true, "评测结果保存路径");
	opt.addOption("slot", true, "槽大小");

	BasicParser parser = new BasicParser();
	CommandLine cl;
	try {
		cl = parser.parse(opt, args);
	} catch (Exception e) {
		System.err.println("Parameters format error");
		return;
	}

	int slotsize = Integer.parseInt(cl.getOptionValue("slot", "50"));
	System.out.println("槽大小:"+slotsize);

	String file = cl.getOptionValue("path", "./tmp/news.allsites.txt");
	System.out.println("数据路径:"+file);

	String resfile = cl.getOptionValue("res", "./tmp/res.txt");
	System.out.println("测试结果:"+resfile);


	SougouCA sca = new SougouCA(file);

	WordCluster wc = new WordCluster();
	wc.slotsize = slotsize;
	wc.read(sca);

	wc.startClustering();
	wc.saveModel(resfile+".m");
	wc.saveTxt(resfile);		
	wc = WordCluster.loadFrom(resfile+".m");
	wc.saveTxt(resfile+"1");
	System.out.println(new Date().toString());
	System.out.println("Done");
}
 
Example 17
Source File: NERTagger.java    From fnlp with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	Options opt = new Options();

	opt.addOption("h", false, "Print help for this application");
	opt.addOption("f", false, "segment file. Default string mode.");
	opt.addOption("s", false, "segment string");
	BasicParser parser = new BasicParser();
	CommandLine cl = parser.parse(opt, args);

	if (args.length == 0 || cl.hasOption('h')) {
		HelpFormatter f = new HelpFormatter();
		f.printHelp(
				"Tagger:\n"
						+ "java edu.fudan.nlp.tag.NERTagger -f segmodel posmodel input_file output_file;\n"
						+ "java edu.fudan.nlp.tag.NERTagger -s segmodel posmodel string_to_segement",
						opt);
		return;
	}
	String[] arg = cl.getArgs();
	String segmodel;
	String posmodel;
	String input;
	String output = null;
	if (cl.hasOption("f") && arg.length == 4) {
		segmodel = arg[0];
		posmodel = arg[1];
		input = arg[2];
		output = arg[3];
	} else if (arg.length == 3) {
		segmodel = arg[0];
		posmodel = arg[1];
		input = arg[2];
	} else {
		System.err.println("paramenters format error!");
		System.err.println("Print option \"-h\" for help.");
		return;
	}
	NERTagger ner = new NERTagger(segmodel,posmodel);
	if (cl.hasOption("f")) {
		ner.tagFile(input,output);
	} else {
		 HashMap<String, String> map = ner.tag(input);
		System.out.println(map);
	}
}
 
Example 18
Source File: addedTagger.java    From fnlp with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * 训练: java -classpath fudannlp.jar edu.fudan.nlp.tag.Tagger -train template train model 
 * 测试: java -classpath fudannlp.jar edu.fudan.nlp.tag.Tagger model test [result]
 * 
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {

	Options opt = new Options();

	opt.addOption("h", false, "Print help for this application");
	opt.addOption("iter", true, "iterative num, default 50");
	opt.addOption("c1", true, "parameters 1, default 1");
	opt.addOption("c2", true, "parameters 2, default 0.1");
	opt.addOption("train", false,
			"switch to training mode(Default: test model");
	opt.addOption("labelwise", false,
			"switch to labelwise mode(Default: viterbi model");
	opt.addOption("margin", false, "use hamming loss as margin threshold");
	opt.addOption("interim", false, "save interim model file");

	BasicParser parser = new BasicParser();
	CommandLine cl;
	try {
		cl = parser.parse(opt, args);
	} catch (Exception e) {
		System.err.println("Parameters format error");
		return;
	}

	if (args.length == 0 || cl.hasOption('h')) {
		HelpFormatter f = new HelpFormatter();
		f.printHelp(
				"Tagger:\n"
						+ "tagger [option] -train templet_file train_file model_file [test_file];\n"
						+ "tagger [option] model_file test_file output_file\n",
						opt);
		return;
	}
	addedTagger tagger = new addedTagger();
	tagger.iterNum = Integer.parseInt(cl.getOptionValue("iter", "50"));
	tagger.c1 = Float.parseFloat(cl.getOptionValue("c1", "1"));
	tagger.c2 = Float.parseFloat(cl.getOptionValue("c2", "0.1"));
	tagger.useLoss = cl.hasOption("margin");
	tagger.interim = cl.hasOption("interim");

	String[] arg = cl.getArgs();
	if (cl.hasOption("train") && arg.length == 3) {
		tagger.templateFile = arg[0];
		tagger.train = arg[1];
		tagger.model = arg[2];
		System.out.println("Training model ...");
		tagger.train();
	} else if (cl.hasOption("train") && arg.length == 4) {
		tagger.templateFile = arg[0];
		tagger.train = arg[1];
		tagger.model = arg[2];
		tagger.testfile = arg[3];
		System.out.println("Training model ...");
		tagger.train();
	} else if (cl.hasOption("train") && arg.length == 5) {
		tagger.templateFile = arg[0];
		tagger.train = arg[1];
		tagger.model = arg[2];
		tagger.testfile = arg[3];
		System.out.println("Training model ...");
		tagger.train();
		System.gc();
		tagger.output = arg[4];
		tagger.test();
	} else if (arg.length == 3) {
		tagger.model = arg[0];
		tagger.testfile = arg[1];
		tagger.output = arg[2];
		tagger.test();
	} else if (arg.length == 2) {
		tagger.model = arg[0];
		tagger.testfile = arg[1];
		tagger.test();
	} else {
		System.err.println("paramenters format error!");
		System.err.println("Print option \"-h\" for help.");
		return;
	}

	System.gc();

}