Options options = new Options(); options.addOption(input); options.addOption(output); HelpFormatter formatter = new HelpFormatter(); CommandLine cmd; cmd = parser.parse(options, args); } catch (ParseException e) { System.out.println(e.getMessage()); formatter.printHelp("utility-name", options); String inputFilePath = cmd.getOptionValue("input"); String outputFilePath = cmd.getOptionValue("output"); System.out.println(inputFilePath); System.out.println(outputFilePath);
private static Params parseCommand(String[] args) { try { Options options = new Options(); options.addOption(new Option("l", "language", true, "language")); options.addOption(new Option("t", "thread", true, "thread")); options.addOption(new Option("f", "file", true, "script file")); options.addOption(new Option("i", "input", true, "input file")); options.addOption(new Option("s", "sleep", true, "sleep time")); options.addOption(new Option("g", "logger", true, "sleep time")); CommandLineParser commandLineParser = new PosixParser(); CommandLine commandLine = commandLineParser.parse(options, args); return readOptions(commandLine); } catch (Exception e) { e.printStackTrace(); exit(); return null; } }
@Test public void testCLI18() { Options options = new Options(); options.addOption(new Option("a", "aaa", false, "aaaaaaa")); options.addOption(new Option(null, "bbb", false, "bbbbbbb dksh fkshd fkhs dkfhsdk fhskd hksdks dhfowehfsdhfkjshf skfhkshf sf jkshfk sfh skfh skf f")); options.addOption(new Option("c", null, false, "ccccccc")); HelpFormatter formatter = new HelpFormatter(); StringWriter out = new StringWriter(); formatter.printHelp(new PrintWriter(out), 80, "foobar", "dsfkfsh kdh hsd hsdh fkshdf ksdh fskdh fsdh fkshfk sfdkjhskjh fkjh fkjsh khsdkj hfskdhf skjdfh ksf khf s", options, 2, 2, "blort j jgj j jg jhghjghjgjhgjhg jgjhgj jhg jhg hjg jgjhghjg jhg hjg jhgjg jgjhghjg jg jgjhgjgjg jhg jhgjh" + '\r' + '\n' + "rarrr", true); } }
SubCommand cmd = findSubCommand(args[1]); if (cmd != null) { Options options = ServerUtil.buildCommandlineOptions(new Options()); options = cmd.buildCommandlineOptions(options); if (options != null) { String[] subargs = parseSubArgs(args); Options options = ServerUtil.buildCommandlineOptions(new Options()); final CommandLine commandLine = ServerUtil.parseCmdLine("mqadmin " + cmd.commandName(), subargs, cmd.buildCommandlineOptions(options), if (commandLine.hasOption('n')) { String namesrvAddr = commandLine.getOptionValue('n'); System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, namesrvAddr);
@Test public void testOptionGroupLong() throws Exception { OptionGroup group = new OptionGroup(); group.addOption(OptionBuilder.withLongOpt("foo").create()); group.addOption(OptionBuilder.withLongOpt("bar").create()); Options options = new Options(); options.addOptionGroup(group); CommandLine cl = parser.parse(options, new String[] { "--bar" }); assertTrue(cl.hasOption("bar")); assertEquals("selected option", "bar", group.getSelected()); }
private String parseConfigLocation(String[] args) { Options options = new Options(); options.addOption(CLEANER_CONFIG); CommandLine cli; try { CommandLineParser parser = new DefaultParser(); cli = parser.parse(options, Arrays.copyOfRange(args, 1, args.length)); } catch (ParseException pe) { System.out.println("Command line parse exception: " + pe.getMessage()); printUsage(options); throw new RuntimeException(pe); } return cli.getOptionValue(CLEANER_CONFIG.getOpt()); }
@Test public void testUsageWithLongOptSeparator() throws Exception { Options options = new Options(); options.addOption( "f", true, "the file" ); options.addOption(Option.builder("s").longOpt("size").desc("the size").hasArg().argName("SIZE").build()); options.addOption(Option.builder().longOpt("age").desc("the age").hasArg().build()); HelpFormatter formatter = new HelpFormatter(); formatter.setLongOptSeparator("="); StringWriter out = new StringWriter(); formatter.printUsage(new PrintWriter(out), 80, "create", options); assertEquals("usage: create [--age=<arg>] [-f <arg>] [-s <SIZE>]", out.toString().trim()); } }
private static Options buildOptions() { Options options = new Options(); options.addOption(OPTION_SQL_FILE_SHORT, OPTION_SQL_FILE_LONG, true, "REQUIRED SQL file which has sql statements"); options.addOption(OPTION_SQL_TOPOLOGY_NAME_SHORT, OPTION_SQL_TOPOLOGY_NAME_LONG, true, "Topology name to submit"); options .addOption(OPTION_SQL_EXPLAIN_SHORT, OPTION_SQL_EXPLAIN_LONG, false, "Activate explain mode (topology name will be ignored)"); return options; }
@Test public void testGetParsedOptionValue() throws Exception { Options options = new Options(); options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i")); options.addOption(OptionBuilder.hasArg().create("f")); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, new String[] { "-i", "123", "-f", "foo" }); assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue()); assertEquals("foo", cmd.getParsedOptionValue("f")); }
Options options = ServerUtil.buildCommandlineOptions(new Options()); commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser()); if (null == commandLine) { final NettyServerConfig nettyServerConfig = new NettyServerConfig(); nettyServerConfig.setListenPort(9876); if (commandLine.hasOption('c')) { String file = commandLine.getOptionValue('c'); if (file != null) { InputStream in = new BufferedInputStream(new FileInputStream(file)); if (commandLine.hasOption('p')) { InternalLogger console = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_CONSOLE_NAME); MixAll.printObjectProperties(console, namesrvConfig);
@Test public void testPropertyOptionRequired() throws Exception { Options opts = new Options(); opts.addOption(OptionBuilder.isRequired().create("f")); Properties properties = new Properties(); properties.setProperty("f", "true"); CommandLine cmd = parse(parser, opts, null, properties); assertTrue(cmd.hasOption("f")); }
CommandLineParser cmdLineParser = new PosixParser(); Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("integer-option") .withDescription("description") .withType(Number.class) .hasArg() .withArgName("argname") .create()); try { CommandLine cmdLine = cmdLineParser.parse(options, args); int value = 0; // initialize to some meaningful default value if (cmdLine.hasOption("integer-option")) { value = ((Number)cmdLine.getParsedOptionValue("integer-option")).intValue(); } System.out.println(value); } catch (ParseException e) { e.printStackTrace(); }
@Test public void testOptionWithoutShortFormat() { // related to Bugzilla #19383 (CLI-67) Options options = new Options(); options.addOption(new Option("a", "aaa", false, "aaaaaaa")); options.addOption(new Option(null, "bbb", false, "bbbbbbb")); options.addOption(new Option("c", null, false, "ccccccc")); HelpFormatter formatter = new HelpFormatter(); StringWriter out = new StringWriter(); formatter.printHelp(new PrintWriter(out), 80, "foobar", "", options, 2, 2, "", true); assertEquals( "usage: foobar [-a] [--bbb] [-c]" + EOL + " -a,--aaa aaaaaaa" + EOL + " --bbb bbbbbbb" + EOL + " -c ccccccc" + EOL , out.toString()); }
@Override public Options getOptions() { final Options options = new Options(); options.addOption(CONFIG_DIR_OPTION); options.addOption(REST_PORT_OPTION); options.addOption(JOB_CLASS_NAME_OPTION); options.addOption(DYNAMIC_PROPERTY_OPTION); options.addOption(CliFrontendParser.SAVEPOINT_PATH_OPTION); options.addOption(CliFrontendParser.SAVEPOINT_ALLOW_NON_RESTORED_OPTION); return options; }
@Test public void testPropertiesOption2() throws Exception { String[] args = new String[] { "-Dparam1", "-Dparam2=value2", "-D"}; Options options = new Options(); options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D')); CommandLine cl = parser.parse(options, args); Properties props = cl.getOptionProperties("D"); assertNotNull("null properties", props); assertEquals("number of properties in " + props, 2, props.size()); assertEquals("property 1", "true", props.getProperty("param1")); assertEquals("property 2", "value2", props.getProperty("param2")); List<?> argsleft = cl.getArgList(); assertEquals("Should be no arg left", 0, argsleft.size()); }
public static void main(String[] args) throws Exception { LibrecTool tool = new DataDriver(); Options options = new Options(); options.addOption("build", false, "build model"); options.addOption("load", false, "load model"); options.addOption("save", false, "save model"); options.addOption("conf", true, "the path of configuration file"); options.addOption("jobconf", true, "a specified key-value pair for configuration"); options.addOption("D", true, "a specified key-value pair for configuration"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); if (cmd.hasOption("build")) { tool.run(args); } else if (cmd.hasOption("load")) { } else if (cmd.hasOption("save")) { } }
@Test public void testPrintSortedUsageWithNullComparator() { Options opts = new Options(); opts.addOption(new Option("c", "first")); opts.addOption(new Option("b", "second")); opts.addOption(new Option("a", "third")); HelpFormatter helpFormatter = new HelpFormatter(); helpFormatter.setOptionComparator(null); StringWriter out = new StringWriter(); helpFormatter.printUsage(new PrintWriter(out), 80, "app", opts); assertEquals("usage: app [-c] [-b] [-a]" + EOL, out.toString()); }
private static Options buildOptions() { Options options = new Options(); options.addOption(OPTION_NAMESPACE_SHORT, OPTION_NAMESPACE_LONG, true, "REQUIRED the list of namespace to migrate."); options.addOption(OPTION_REDIS_HOST_SHORT, OPTION_REDIS_HOST_LONG, true, "Redis hostname (default: localhost)"); options.addOption(OPTION_REDIS_PORT_SHORT, OPTION_REDIS_PORT_LONG, true, "Redis port (default: 6379)"); options.addOption(null, OPTION_REDIS_PASSWORD_LONG, true, "Redis password (default: no password)"); options.addOption(OPTION_REDIS_DB_NUM_SHORT, OPTION_REDIS_DB_NUM_LONG, true, "Redis DB number (default: 0)"); return options; }