/** * Reinvoke the createDescriptions of the sub-commanders as they will otherwise not have their * descriptions taken from the bundle. * * @param jc * @param bundle */ protected static void fixResourceBundleBug(final JCommander jc, final ResourceBundle bundle) { for (final Entry<String, JCommander> entry : jc.getCommands().entrySet()) { final JCommander subJc = entry.getValue(); subJc.setDescriptionsBundle(bundle); subJc.setAcceptUnknownOptions(false); subJc.setColumnSize(jc.getColumnSize()); try { final Method method = subJc.getClass().getDeclaredMethod("createDescriptions"); method.setAccessible(true); method.invoke(subJc); } catch (final NoSuchMethodException e) { } catch (final SecurityException e) { } catch (final IllegalAccessException e) { } catch (final IllegalArgumentException e) { } catch (final InvocationTargetException e) { } } }
public static void main(final String[] args) { final ResourceBundle bundle = ResourceBundle.getBundle("help"); final JCommander commander = createCommander(bundle); final HelpCommand help = addCommands(bundle, commander); try { commander.parse(args); } catch (final ParameterException e) { help.error(e); } if (commander.getParsedCommand() != null) { final JCommander parsedCommander = commander.getCommands().get(commander.getParsedCommand()); final Runnable cmd = (Runnable) parsedCommander.getObjects().get(0); cmd.run(); } else { help.error("help.missingCommand"); } }
@Override public int run(String[] args) throws Exception { if (getConf() != null) { DefaultConfiguration.set(getConf()); } try { jc.parse(args); } catch (MissingCommandException e) { console.error(e.getMessage()); return 1; } catch (ParameterException e) { console.error(e.getMessage()); return 1; } // configure log4j if (debug) { org.apache.log4j.Logger console = org.apache.log4j.Logger.getLogger(Main.class); console.setLevel(Level.DEBUG); } String parsed = jc.getParsedCommand(); if (parsed == null) { console.error("Unable to parse command."); return 1; } Command command = (Command) jc.getCommands().get(parsed).getObjects().get(0); if (command == null) { console.error("Unable to find command."); return 1; } try { if (command instanceof Configurable) { ((Configurable) command).setConf(getConf()); } return command.run(); } catch (IllegalArgumentException e) { if (debug) { console.error("Argument error", e); } else { console.error("Argument error: {}", e.getMessage()); } return 1; } catch (IllegalStateException e) { if (debug) { console.error("State error", e); } else { console.error("State error: {}", e.getMessage()); } return 1; } catch (ValidationException e) { if (debug) { console.error("Validation error", e); } else { console.error("Validation error: {}", e.getMessage()); } return 1; } catch (DatasetNotFoundException e) { if (debug) { console.error("Cannot find dataset", e); } else { // the error message already contains "No such dataset: <name>" console.error(e.getMessage()); } return 1; } catch (DatasetIOException e) { if (debug) { console.error("IO error", e); } else { console.error("IO error: {}", e.getMessage()); } return 1; } catch (Exception e) { if (debug) { console.error("Unknown error", e); } else { console.error("Unknown error: {}", e.getMessage()); } return 1; } }