Refine search
public void execute(String[] args) throws Exception { try { CmdLineParser parser = new CmdLineParser(this); parser.parseArgument(args); if ((_subCommand == null) || _help) { printUsage(); } else if (_subCommand.getHelp()) { _subCommand.printUsage(); } else { _subCommand.execute(); } } catch (CmdLineException e) { LOGGER.error("Error: {}", e.getMessage()); } catch (Exception e) { LOGGER.error("Exception caught: ", e); } }
@Override public int parseArguments(Parameters params) throws CmdLineException { String param = params.getParameter(0); Result v = fromString(param.replace('-', '_')); if (v==null) throw new CmdLineException(owner,"No such status '"+param+"'. Did you mean "+ EditDistance.findNearest(param.replace('-', '_').toUpperCase(), getNames())); setter.addValue(v); return 1; }
private int run(String[] args) { CmdLineParser p = new CmdLineParser(this); try { p.parseArgument(args); run(); return 0; } catch (CmdLineException e) { System.err.println(e.getMessage()); p.printUsage(System.err); return 1; } }
private void parseArgs(String[] args) throws CmdLineException { CmdLineParser parser = new CmdLineParser(this); try { parser.parseArgument(args); } catch (CmdLineException e) { System.err.println(e.getMessage() + "\n"); parser.setUsageWidth(120); parser.printUsage(System.err); throw new CmdLineException("Exiting..."); } }
public static void main(String[] args) throws IOException, InterruptedException { try { _main(args); } catch (CmdLineException e) { System.err.println(e.getMessage()); System.err.println("java -jar slave.jar [options...] <secret key> <slave name>"); new CmdLineParser(new Main()).printUsage(System.err); } }
parser.parseArgument(args); stderr.println("ERROR: " + e.getMessage()); printUsage(stderr, parser); return 2;
public static void main(final String[] args) { Arguments arguments = new Arguments(); try { CmdLineParser parser = new CmdLineParser(arguments); parser.parseArgument(args); } catch (CmdLineException e) { System.out.println("Error: " + e.getLocalizedMessage()); return; } start(arguments); }
private void invalidArgs(CmdLineParser parser, CmdLineException e) { System.err.println(e.getMessage()); System.err.println("java DownloadStatement [options...] arguments..."); // print the list of available options parser.printUsage(System.err); System.err.println(); // print option sample. This is useful some time System.err.println(" Example: java DownloadStatement " + parser.printExample(ExampleMode.ALL)); System.exit(1); }
/** * Parse the given args list. */ private void parse(List<String> args) throws CmdLineException { parser.parseArgument(args.toArray(new String[] {})); compilationLevelParsed = COMPILATION_LEVEL_MAP.get(compilationLevel.toUpperCase()); if (compilationLevelParsed == null) { throw new CmdLineException( parser, "Bad value for --compilation_level: " + compilationLevel); } }
@CLIResolver public RunT getBuildForCLI(@Argument(required=true,metaVar="BUILD#",usage="Build number") String id) throws CmdLineException { try { int n = Integer.parseInt(id); RunT r = getBuildByNumber(n); if (r==null) throw new CmdLineException(null, "No such build '#"+n+"' exists"); return r; } catch (NumberFormatException e) { throw new CmdLineException(null, id+ "is not a number"); } }
@Override public String getLocalizedMessage() { if (localizedMessage != null) { return localizedMessage; } else { return getMessage(); } }
@Test public void testRunError () throws Exception { System.out.println("\n+++ testRunError"); String[] args = new String[]{"-run", "fooBar"}; try { instance.parseParameters(args); fail(); } catch (CmdLineException ex) { System.out.println(ex.getMessage()); System.out.println(ex.getLocalizedMessage()); assertTrue(ex.getMessage().contains("java.lang.ClassNotFoundException")); } }
@Override public int parseArguments(Parameters params) throws CmdLineException { if (params.size() != 1) throw new CmdLineException ("an option of type log level should have one parameter [trace,debug,info,warn,error,fatal,off]"); Level result = Level.toLevel(params.getParameter(0).toLowerCase()); setter.addValue(result); log.debug("set log level " + result); return 1; }
@Override public int parseArguments(Parameters params) throws CmdLineException { String macString = params.getParameter(0); String[] macStringArray = null; throw new CmdLineException(owner, Messages.ILLEGAL_MAC_ADDRESS, macString); setter.asFieldSetter().addValue(mac); return 1;
expandedArgs = expandAtFiles(args); if( isOption(arg) ) { currentOptionHandler = isKeyValuePair ? findOptionHandler(arg) : findOptionByName(arg); throw new CmdLineException(this, Messages.UNDEFINED_OPTION, arg); if (argIndex >= arguments.size()) { Messages msg = arguments.size() == 0 ? Messages.NO_ARGUMENT_ALLOWED : Messages.TOO_MANY_ARGUMENTS; throw new CmdLineException(this, msg, arg);
private void checkRequiredOptionsAndArguments(Set<OptionHandler> present) throws CmdLineException { // make sure that all mandatory options are present for (OptionHandler handler : options) { if(handler.option.required() && !present.contains(handler)) { throw new CmdLineException(this, Messages.REQUIRED_OPTION_MISSING, handler.option.toString()); } } // make sure that all mandatory arguments are present for (OptionHandler handler : arguments) { if(handler.option.required() && !present.contains(handler)) { throw new CmdLineException(this, Messages.REQUIRED_ARGUMENT_MISSING, handler.option.toString()); } } //make sure that all requires arguments are present for (OptionHandler handler : present) { if (handler.option instanceof NamedOptionDef && !isHandlerHasHisOptions((NamedOptionDef)handler.option, present)) { throw new CmdLineException(this, Messages.REQUIRES_OPTION_MISSING, handler.option.toString(), Arrays.toString(((NamedOptionDef)handler.option).depends())); } } //make sure that all forbids arguments are not present for (OptionHandler handler : present) { if (handler.option instanceof NamedOptionDef && !isHandlerAllowOtherOptions((NamedOptionDef) handler.option, present)) { throw new CmdLineException(this, Messages.FORBIDDEN_OPTION_PRESENT, handler.option.toString(), Arrays.toString(((NamedOptionDef) handler.option).forbids())); } } }
/** * Expands every entry prefixed with the AT sign by * reading the file. The AT sign is used to reference * another file that contains command line options separated * by line breaks. * @param args the command line arguments to be preprocessed. * @return args with the @ sequences replaced by the text files referenced * by the @ sequences, split around the line breaks. * @throws CmdLineException */ private String[] expandAtFiles(String args[]) throws CmdLineException { List<String> result = new ArrayList<String>(); for (String arg : args) { if (arg.startsWith("@")) { File file = new File(arg.substring(1)); if (!file.exists()) throw new CmdLineException(this,Messages.NO_SUCH_FILE,file.getPath()); try { result.addAll(readAllLines(file)); } catch (IOException ex) { throw new CmdLineException(this, "Failed to parse "+file,ex); } } else { result.add(arg); } } return result.toArray(new String[result.size()]); }
private static void processCli (String[] args) { try { // First get the provided parameters if any cli = new CLI(WellKnowns.TOOL_NAME); cli.parseParameters(args); } catch (CmdLineException ex) { logger.warn("Error in command line: {}", ex.getLocalizedMessage(), ex); logger.warn("Exiting ..."); // Stop the JVM, with failure status (1) Runtime.getRuntime().exit(1); } }
Class clazz = Class.forName(classname); Object bean = clazz.newInstance(); parser = new CmdLineParser(bean); classHasOptions = hasAnnotation(clazz, Option.class); parser.parseArgument(args); System.err.println(e.getMessage()); System.err.print(classname); if (classHasOptions) System.err.print(" [options]"); System.err.println(); if (parser != null) parser.printUsage(System.err); } catch (Exception e) {
public static void main(String[] args) throws IOException, InterruptedException { try { _main(args); } catch (CmdLineException e) { System.err.println(e.getMessage()); System.err.println("java -jar slave.jar [options...] <secret key> <slave name>"); new CmdLineParser(new Main()).printUsage(System.err); } }