@Test public void testInvalidArgs() { String[] args = {"foo"}; HelpStub help = new HelpStub(); try { help.parseArgs("program", args); } catch (RuntimeException e) { assertEquals("1", e.getMessage()); } }
public void parseArgs(String programName, String[] args, Object... others) { JCommander commander = new JCommander(); commander.addObject(this); for (Object other : others) commander.addObject(other); commander.setProgramName(programName); try { commander.parse(args); } catch (ParameterException ex) { commander.usage(); exitWithError(ex.getMessage(), 1); } if (help) { commander.usage(); exit(0); } }
Help { public void parseArgs(String programName, String[] args, Object... others) { JCommander commander = new JCommander(); commander.addObject(this); for (Object other : others) commander.addObject(other); commander.setProgramName(programName); try { commander.parse(args); } catch (ParameterException ex) { commander.usage(); exitWithError(ex.getMessage(), 1); } if (help) { commander.usage(); exit(0); } } }
Help { public void parseArgs(String programName, String[] args, Object... others) { JCommander commander = new JCommander(); commander.addObject(this); for (Object other : others) commander.addObject(other); commander.setProgramName(programName); try { commander.parse(args); } catch (ParameterException ex) { commander.usage(); exitWithError(ex.getMessage(), 1); } if (help) { commander.usage(); exit(0); } }  }
Help { public void parseArgs(String programName, String[] args, Object... others) { JCommander commander = new JCommander(); commander.addObject(this); for (Object other : others) commander.addObject(other); commander.setProgramName(programName); try { commander.parse(args); } catch (ParameterException ex) { commander.usage(); exitWithError(ex.getMessage(), 1); } if (help) { commander.usage(); exit(0); } }  void parseArgs(String programName, String[] args, Object... others); void exit(int status); void exitWithError(String message, int status); }
Help { public void parseArgs(String programName, String[] args, Object... others) { JCommander commander = new JCommander(); commander.addObject(this); for (Object other : others) commander.addObject(other); commander.setProgramName(programName); try { commander.parse(args); } catch (ParameterException ex) { commander.usage(); exitWithError(ex.getMessage(), 1); } if (help) { commander.usage(); exit(0); } }  void parseArgs(String programName, String[] args, Object... others); void exit(int status); void exitWithError(String message, int status); @Parameter(names = {"-h", "-?", "--help", "-help"}, help = true)
  public boolean help; }