List of usage examples for org.apache.commons.cli OptionBuilder withValueSeparator
public static OptionBuilder withValueSeparator()
=
' as a means to separate argument values. From source file:com.rockagen.commons.util.CLITest.java
@Test @Ignore// w w w. j ava 2 s . c o m public void testCLI() { // create the Options Options options = new Options(); options.addOption("h", "help", false, "print help for the command."); options.addOption("v", "verbose", false, "verbose"); OptionBuilder.withArgName("property=value"); OptionBuilder.hasArgs(2); OptionBuilder.withLongOpt("desc"); OptionBuilder.withValueSeparator(); OptionBuilder.withDescription("use value for given property"); options.addOption(OptionBuilder.create("D")); OptionBuilder.withArgName("file1,file2..."); OptionBuilder.hasArgs(); OptionBuilder.withLongOpt("input"); OptionBuilder.withValueSeparator(' '); OptionBuilder.withDescription("file name"); options.addOption(OptionBuilder.create("i")); String formatstr = "CLITest [-h/--help][-v/--verbose].."; try { String[] args = { "CLITest", "--help", "-v", "-s", "--desc", "name=value", "--input", "file1", "file2", "file3" }; // parse the command line arguments CommandLine line = CmdUtil.parse(options, args); if (line.hasOption("h")) { CmdUtil.printHelp(formatstr, "weclome usa", options, "If you hava some quesion,please mail to agen@rockagen.com"); } if (line.hasOption("v")) { System.out.println("VERSION 0.0.1"); } if (line.hasOption("D")) { System.out.println("hey,guys,you input " + ArrayUtil.toString(line.getOptionValues("D"))); } if (line.hasOption("i")) { System.out.println("hey,guys,you input " + ArrayUtil.toString(line.getOptionValues("i"))); } else { CmdUtil.printHelp(formatstr, options); } } catch (ParseException exp) { CmdUtil.printHelp(formatstr, options); System.err.println(); System.err.println(exp.getMessage()); } }
From source file:ape.TouchCommand.java
/** * The constructor for this command simply creates its Option object (used by * the CLI parser)/*www . j a v a 2s . c o m*/ */ public TouchCommand() { option = OptionBuilder.withValueSeparator().withDescription("Touches a file called /tmp/foo.tst") .withLongOpt("touch").create("t"); }
From source file:ape.ForkBombCommand.java
/** * The constructor for this command simply creates its Option object (used by * the CLI parser)//from w w w .j av a 2 s . c o m */ public ForkBombCommand() { option = OptionBuilder.withValueSeparator().withDescription("Hangs a host by executing a fork bomb") .withLongOpt("forkbomb").create("F"); }
From source file:ape.KernelPanicCommand.java
/** * The constructor for this command simply creates its Option object (used by * the CLI parser)/*from w w w .j a v a 2s .c om*/ */ public KernelPanicCommand() { option = OptionBuilder.withValueSeparator() .withDescription("Forces a kernel panic and does not restart the system.").withLongOpt("panic") .create("P"); }
From source file:ape.RemountFileSysReadOnlyCommand.java
/** * The constructor for this command simply creates its Option object (used by * the CLI parser)/*from w w w . j av a2 s.c o m*/ */ public RemountFileSysReadOnlyCommand() { option = OptionBuilder.withValueSeparator().withDescription("Remounts all filesystems as read-only") .withLongOpt("remount").create("r"); }
From source file:consumer.kafka.client.Consumer.java
private void init(String[] args) throws Exception { Options options = new Options(); this._props = new Properties(); options.addOption("p", true, "properties filename from the classpath"); options.addOption("P", true, "external properties filename"); OptionBuilder.withArgName("property=value"); OptionBuilder.hasArgs(2);/*from ww w . j a va 2 s .c om*/ OptionBuilder.withValueSeparator(); OptionBuilder.withDescription("use value for given property"); options.addOption(OptionBuilder.create("D")); CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption('p')) { this._props.load(ClassLoader.getSystemClassLoader().getResourceAsStream(cmd.getOptionValue('p'))); } if (cmd.hasOption('P')) { File file = new File(cmd.getOptionValue('P')); FileInputStream fStream = new FileInputStream(file); this._props.load(fStream); } this._props.putAll(cmd.getOptionProperties("D")); }
From source file:net.nicholaswilliams.java.licensing.licensor.interfaces.cli.spi.CliOptionsBuilder.java
public CliOptionsBuilder withValueSeparator() { OptionBuilder.withValueSeparator(); return this; }
From source file:cn.edu.pku.cbi.mosaichunter.MosaicHunter.java
private static boolean loadConfiguration(String[] args, String configFile) throws Exception { OptionBuilder.withArgName("file"); OptionBuilder.hasArg();/*from w w w.j a va 2 s . co m*/ OptionBuilder.withDescription("config file"); OptionBuilder.withLongOpt("config"); Option configFileOption = OptionBuilder.create("C"); OptionBuilder.withArgName("property=value"); OptionBuilder.hasArgs(2); OptionBuilder.withValueSeparator(); OptionBuilder.withDescription("properties that overrides the ones in config file"); OptionBuilder.withLongOpt("properties"); Option propertiesOption = OptionBuilder.create("P"); Options options = new Options(); options.addOption(configFileOption); options.addOption(propertiesOption); CommandLineParser parser = new BasicParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException pe) { System.out.println(pe.getMessage()); return false; } InputStream in = null; if (configFile == null || configFile.trim().isEmpty()) { configFile = cmd.getOptionValue("C"); if (configFile != null && new File(configFile).isFile()) { in = new FileInputStream(configFile); } } else { in = MosaicHunter.class.getClassLoader().getResourceAsStream(configFile); } if (in != null) { try { ConfigManager.getInstance().loadProperties(in); } catch (IOException ioe) { System.out.println("invalid config file: " + configFile); return false; } finally { in.close(); } } Properties properties = cmd.getOptionProperties("P"); if (properties != null) { ConfigManager.getInstance().putAll(properties); } return !ConfigManager.getInstance().getProperties().isEmpty(); }
From source file:net.sf.clichart.main.OptionParser.java
private void addOption(org.apache.commons.cli.Options optionDefs, String[] optionDetails, boolean hasArgument) { // OptionBuilder is a stateful class OptionBuilder.withLongOpt(optionDetails[1]); OptionBuilder.withDescription(optionDetails[2]); if (hasArgument) { OptionBuilder.hasArg(true);//www . j a v a 2 s . co m OptionBuilder.withValueSeparator(); } if (optionDetails[0] != null) { optionDefs.addOption(OptionBuilder.create(optionDetails[0])); } else { optionDefs.addOption(OptionBuilder.create()); } }
From source file:ape.Main.java
/** * This method generates all the options, and stores them in opts *//*from www. ja va2 s .co m*/ public static void createOptions() { //System.out.println("creating options ..."); Options options = new Options(); options.addOption("h", "help", false, "Displays this help menu"); options.addOption("V", "version", false, "Displays the version number"); options.addOption(OptionBuilder.withValueSeparator().withDescription("Turn on verbose mode") .withLongOpt("verbose").create("v")); // Adds all of the commands in the service loader to an OptionGroup so that they are all mutually exclusive OptionGroup apeCommands = new OptionGroup(); Iterator<ApeCommand> iter = loader.iterator(); while (iter.hasNext()) { ApeCommand ac = iter.next(); apeCommands.addOption(ac.getOption()); //System.out.println(ac.getOption()); } options.addOptionGroup(apeCommands); // Makes the local and remote commands mutually exclusive OptionGroup remoteOrLocal = new OptionGroup(); remoteOrLocal.addOption(OptionBuilder.withArgName("HostnameList").hasArgs().withValueSeparator() .withDescription("Run commands remotely").withLongOpt("remote").create("R")); remoteOrLocal.addOption(OptionBuilder.withArgName("Command").withValueSeparator() .withDescription("Run commands locally").withLongOpt("local").create("L")); options.addOptionGroup(remoteOrLocal); opts = options; }