public static String getStringOption(CommandLine cmd, Option option) { String opt = option.getOpt() == null ? option.getLongOpt() : option.getOpt(); if (cmd.hasOption(opt)) { return cmd.getOptionValue(opt); } else { throw new CliException("Missing value for option " + opt); } }
protected void start(String[] args) { int result = 1; try { System.out.println(); result = runBase(args); } catch (CliException e) { System.err.println(); System.err.println(e.getMessage()); System.exit(e.getExitCode()); } catch (Throwable t) { reportThrowable(t); } finally { try { cleanup(); } catch (Throwable t) { System.err.println("Error during cleanup:"); t.printStackTrace(); } } System.out.println(); if (result != 0) { System.exit(result); } }
public static long getLongOption(CommandLine cmd, Option option, long defaultValue) { String opt = option.getOpt() == null ? option.getLongOpt() : option.getOpt(); if (cmd.hasOption(opt)) { try { return Long.parseLong(cmd.getOptionValue(opt)); } catch (NumberFormatException e) { throw new CliException("Invalid value for option " + option.getLongOpt() + ": " + cmd.getOptionValue(opt)); } } return defaultValue; }
public static int getIntOption(CommandLine cmd, Option option, int defaultValue) { String opt = option.getOpt() == null ? option.getLongOpt() : option.getOpt(); if (cmd.hasOption(opt)) { try { return Integer.parseInt(cmd.getOptionValue(opt)); } catch (NumberFormatException e) { throw new CliException("Invalid value for option " + option.getLongOpt() + ": " + cmd.getOptionValue(opt)); } } return defaultValue; }
public static <T extends Enum<T>> T getEnum(CommandLine cmd, Option option, T defaultValue, Class<T> enumClass) { String value = getStringOption(cmd, option, null); if (value == null) { return defaultValue; } try { return Enum.valueOf(enumClass, value.toUpperCase()); } catch (IllegalArgumentException e) { throw new CliException("Invalid value for option " + option.getLongOpt() + ": " + value); } }
public static <T extends Enum<T>> T getEnum(CommandLine cmd, Option option, Class<T> enumClass) { String value = getStringOption(cmd, option); try { return Enum.valueOf(enumClass, value.toUpperCase()); } catch (IllegalArgumentException e) { throw new CliException("Invalid value for option " + option.getLongOpt() + ": " + value); } } }