CommandLineUtil { public static <T extends Enum<T>> T getOptionValue(CommandLine line, String option, Class<T> clazz, T defaultValue) { Objects.requireNonNull(line); Objects.requireNonNull(option); Objects.requireNonNull(clazz); if (line.hasOption(option)) { return Enum.valueOf(clazz, line.getOptionValue(option)); } else { return defaultValue; } } private  CommandLineUtil(); static T getOptionValue(CommandLine line, String option, Class<T> clazz, T defaultValue);  }
@Test public void test() throws ParseException { CommandLineParser parser = new DefaultParser(); Options options = new Options(); options.addOption(Option.builder().longOpt("value") .hasArg() .build()); String[] args = {"--value", "VALUE2"}; CommandLine line = parser.parse(options, args); assertEquals(TestEnum.VALUE2, CommandLineUtil.getOptionValue(line, "value", TestEnum.class, TestEnum.VALUE1)); args = new String[0]; line = parser.parse(options, args); assertEquals(TestEnum.VALUE1, CommandLineUtil.getOptionValue(line, "value", TestEnum.class, TestEnum.VALUE1)); }