@Override public CliCommand parse(String[] cmdArgs) throws CliParseException { Parser parser = new PosixParser(); CommandLine cl; try { cl = parser.parse(options, cmdArgs); } catch (ParseException ex) { throw new CliParseException(ex); } args = cl.getArgs(); if (args.length < 2) { throw new CliParseException(getUsageStr()); } return this; }
@Test public void test31148() throws ParseException { Option multiArgOption = new Option("o","option with multiple args"); multiArgOption.setArgs(1); Options options = new Options(); options.addOption(multiArgOption); Parser parser = new PosixParser(); String[] args = new String[]{}; Properties props = new Properties(); props.setProperty("o","ovalue"); CommandLine cl = parser.parse(options,args,props); assertTrue(cl.hasOption('o')); assertEquals("ovalue",cl.getOptionValue('o')); }
private String[] handleHelp(String[] args, Options fullOptions) { Options options = new Options(); options.addOption(fullOptions.getOption("help")); Parser p = new PosixParser(); try { CommandLine line = p.parse(options, args, true); if (line.hasOption('?')) { showHelpAndExit(fullOptions, 0); new HelpFormatter().printHelp(getCommandLineSyntax(), fullOptions); System.exit(0); } return line.getArgs(); } catch (ParseException e) { // ignore } return args; }
@Override public CliCommand parse(String[] cmdArgs) throws CliParseException { Parser parser = new PosixParser(); try { cl = parser.parse(options, cmdArgs); } catch (ParseException ex) { throw new CliParseException(ex); } args = cl.getArgs(); if (args.length < 2) { throw new CliParseException(getUsageStr()); } return this; }
@Override public CliCommand parse(String[] cmdArgs) throws ParseException { Parser parser = new PosixParser(); cl = parser.parse(options, cmdArgs); args = cl.getArgs(); if (args.length < 2) { throw new ParseException(getUsageStr()); } return this; }
@Override public CliCommand parse(String[] cmdArgs) throws ParseException { Parser parser = new PosixParser(); CommandLine cl = parser.parse(options, cmdArgs); args = cl.getArgs(); if (args.length < 2) { throw new ParseException(getUsageStr()); } return this; }
@Test public void test13425() throws Exception { Options options = new Options(); Option oldpass = OptionBuilder.withLongOpt( "old-password" ) .withDescription( "Use this option to specify the old password" ) .hasArg() .create( 'o' ); Option newpass = OptionBuilder.withLongOpt( "new-password" ) .withDescription( "Use this option to specify the new password" ) .hasArg() .create( 'n' ); String[] args = { "-o", "-n", "newpassword" }; options.addOption( oldpass ); options.addOption( newpass ); Parser parser = new PosixParser(); try { parser.parse( options, args ); fail( "MissingArgumentException not caught." ); } catch( MissingArgumentException expected ) { } }
/** * Re-parse our options using the properties from the specified conf file. * @param properties the command line override properties * @param args command line arguments * @return any left-over command line arguments * that haven't been parsed. This method sets up and parses * only the option file argument, so the return would be everything * from the command line args except the --file if provided. * @throws ParseException if unable to parse the command line options * @throws IOException if unable to read the file */ @SuppressWarnings("static-access") protected String[] getConfigOverrides(Properties properties, String[] args) throws ParseException, IOException { // Parse with our config option only... Options options = new Options(); options.addOption( OptionBuilder.withLongOpt(OPTION_OPTIONS_FILE) .withDescription("Configuration file of key/value pairs with option overrides.") .withArgName("file") .hasArg() .create()); Parser parser = new IgnoreUnknownParser(); CommandLine line = parser.parse(options, args, true); if (line.hasOption(OPTION_OPTIONS_FILE)) { File confFile = new File(line.getOptionValue(OPTION_OPTIONS_FILE)); if (!confFile.exists()) { throw new IllegalArgumentException("Configuration file not found."); } Reader reader = null; try { reader = new FileReader(confFile); properties.load(reader); } finally { IOUtils.closeQuietly(reader); } } return line.getArgs(); }
/** * Parse the arguments. * * @param args the command line arguments * @throws Exception if the process fails * @throws ParseException if the command line parsing fails */ protected void parseFromArgs(String[] args) throws Exception { Options options = initOptions(); // Notify our listener if there is one... if (this.contextListener != null) { this.contextListener.afterInitOptions(options); } try { args = handleHelp(args, options); Properties props = new Properties(); args = getConfigOverrides(props, args); // Now parse the rest of 'em Parser parser = new PosixParserRequiredProps(); CommandLine line = null; line = parser.parse(options, args, props, false); // Populate our context values from the parsed command line populateContext(line); // Notify our listener if there is one... if (this.contextListener != null) { this.contextListener.afterParseCommandLine(line); } } catch (ParseException e) { showHelpAndExit(options, 1, e.getMessage()); } }
@Test(expected=ParseException.class) public void testCLI_insufficientArg() throws ParseException { UnitTestHelper.setLog4jLevel(Parser.class, Level.FATAL); CommandLine cli = new CLIBuilder().with(ParserTopologyCLI.ParserOptions.BROKER_URL, "mybroker") .with(ParserTopologyCLI.ParserOptions.ZK_QUORUM, "myzk") .build(true); UnitTestHelper.setLog4jLevel(Parser.class, Level.ERROR); }
Parser createParser() { Parser result = new BasicParser(); return result; }