/** * Handles an unknown token. If the token starts with a dash an * UnrecognizedOptionException is thrown. Otherwise the token is added * to the arguments of the command line. If the stopAtNonOption flag * is set, this stops the parsing and the remaining tokens are added * as-is in the arguments of the command line. * * @param token the command line token to handle */ private void handleUnknownToken(String token) throws ParseException { if (token.startsWith("-") && token.length() > 1 && !stopAtNonOption) { throw new UnrecognizedOptionException("Unrecognized option: " + token, token); } cmd.addArg(token); if (stopAtNonOption) { skipParsing = true; } }
@Test public void testShortWithUnexpectedArgument() throws Exception { String[] args = new String[] { "-f=bar" }; Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("foo").create('f')); try { parser.parse(options, args); } catch (UnrecognizedOptionException e) { assertEquals("-f=bar", e.getOption()); return; } fail("UnrecognizedOptionException not thrown"); }
public void parse() { try { commandLine = new GnuParser().parse(options, cmdArguments); } catch (UnrecognizedOptionException e) { System.err.println(e.getMessage()); usageAndDie(); } catch (ParseException e) { System.err.println(e.getMessage()); usageAndDie(); } }
protected void parseCommandLine(String[] args) throws UnrecognizedOptionException, ParseException { CommandLineParser clp = new BasicParser(); try { cmd = clp.parse(getOptions(), args); } catch (UnrecognizedOptionException ux) { say("Error: unrecognized option + " + ux.getMessage()); say("Invoke with -help for more"); if (isVerbose()) { ux.printStackTrace(); } throw ux; } catch (ParseException e) { say("Error: could not parse a command line argument:" + e.getMessage()); if (isVerbose()) { e.printStackTrace(); } throw e; } }
} catch (UnrecognizedOptionException e) { e.getStackTrace(); return 1; } catch (BuildAbort e) {
public void parse() { try { commandLine = new GnuParser().parse(options, cmdArguments); } catch (UnrecognizedOptionException e) { System.err.println(e.getMessage()); usageAndDie(); } catch (ParseException e) { System.err.println(e.getMessage()); usageAndDie(); } }
protected void parseCommandLine(String[] args) { CommandLineParser clp = new BasicParser(); try { cmd = clp.parse(getOptions(), args); } catch (UnrecognizedOptionException ux) { String err = "Error: unrecognized option + " + ux.getMessage() ; say(err); say("Invoke with -help for more"); if (isVerbose()) { ux.printStackTrace(); } throw new GeneralException(err, ux); } catch (ParseException e) { String err= "Error: could not parse a command line argument:" + e.getMessage(); say(err); if (isVerbose()) { e.printStackTrace(); } throw new GeneralException(err,e); } }
return e.exitCode; } catch (UnrecognizedOptionException e) { e.getStackTrace(); return 1; } catch (BuildAbort e) {
if (opt == null) throw new UnrecognizedOptionException("Default option wasn't defined", option);
public static void main(String[] args) throws Exception { BasicConfigurator.configure(); Logger.getRootLogger().setLevel(Level.ERROR); try { parseCmdLine(args); } catch (org.apache.commons.cli.UnrecognizedOptionException e) { System.out.println(e.getMessage()); printHelp(buildOptions()); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testLongWithUnexpectedArgument1() throws Exception { String[] args = new String[] { "--foo=bar" }; Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("foo").create('f')); try { parser.parse(options, args); } catch (UnrecognizedOptionException e) { assertEquals("--foo=bar", e.getOption()); return; } fail("UnrecognizedOptionException not thrown"); }
if (opt == null) throw new UnrecognizedOptionException("Default option wasn't defined", option);
cmd = parser.parse(options, args); } catch (UnrecognizedOptionException e) { System.err.println(e.getMessage()); printHelp(options); System.exit(1);
@Test public void testLongWithUnexpectedArgument2() throws Exception { String[] args = new String[] { "-foobar" }; Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("foo").create('f')); try { parser.parse(options, args); } catch (UnrecognizedOptionException e) { assertEquals("-foobar", e.getOption()); return; } fail("UnrecognizedOptionException not thrown"); }
throw new UnrecognizedOptionException("Unrecognized option: " + arg, arg);
} catch (UnrecognizedOptionException e) { System.err.println(); System.err.println("ERROR: " + e.getMessage()); System.exit(2); } catch (IllegalArgumentException e) {
@Test public void testUnrecognizedOptionWithBursting() throws Exception { String[] args = new String[] { "-adbtoast", "foo", "bar" }; try { parser.parse(options, args); fail("UnrecognizedOptionException wasn't thrown"); } catch (UnrecognizedOptionException e) { assertEquals("-adbtoast", e.getOption()); } }
@Override public void execute(CommandLine options) throws Exception { String[] args = options.getArgs(); if (args.length < min) { throw new MissingArgumentException("missing required arguments"); } if (args.length > max) { throw new UnrecognizedOptionException("unknown extra argument \"" + args[max] + "\""); } } }
line = parser.parse(options, args); } catch (UnrecognizedOptionException exception) { System.err.println(exception.getMessage()); System.exit(-1); } catch (ParseException exception) {
@Test public void testUnrecognizedOption() throws Exception { String[] args = new String[] { "-a", "-d", "-b", "toast", "foo", "bar" }; try { parser.parse(options, args); fail("UnrecognizedOptionException wasn't thrown"); } catch (UnrecognizedOptionException e) { assertEquals("-d", e.getOption()); } }