@Test public void expand() { JCommanderWrapper<Supplier<String>> commander = new JCommanderWrapper<>("PG", new Exp()); Optional<String> result = commander.parse("-abc").map(Supplier::get); assertThat(result, is(Optional.of("abc"))); }
private String[] expand(String[] args) { JCommander commander = findCommander(args); Params params = collectParams(commander); if (params.names.stream() .filter(JCommanderWrapper::isShortNameOption) .anyMatch(it -> it.length() > 2)) { return args; } List<String> dynamics = params.dynamicNames.stream() .filter(JCommanderWrapper::isShortNameOption) .collect(Collectors.toList()); boolean sawExpand = false; boolean sawEscape = false; List<String> results = new ArrayList<>(); for (String arg : args) { if (sawEscape) { results.add(arg); } else { if (isShortNameOption(arg) && arg.length() > 2 && dynamics.stream().anyMatch(arg::startsWith) == false) { LOG.debug("expand option: {}", arg); for (int i = 1, n = arg.length(); i < n; i++) { sawExpand = true; results.add("-" + arg.charAt(i)); } } else { sawEscape |= arg.equals("--"); results.add(arg); } } } if (sawExpand) { LOG.debug("expanded command line: {}", results); return results.toArray(new String[results.size()]); } return args; }
JCommanderWrapper implements CommandBuilder<T> { private String[] expand(String[] args) { JCommander commander = findCommander(args); Params params = collectParams(commander); if (params.names.stream() .filter(JCommanderWrapper::isShortNameOption) .anyMatch(it -> it.length() > 2)) { return args; } List<String> dynamics = params.dynamicNames.stream() .filter(JCommanderWrapper::isShortNameOption) .collect(Collectors.toList()); boolean sawExpand = false; boolean sawEscape = false; List<String> results = new ArrayList<>(); for (String arg : args) { if (sawEscape) { results.add(arg); } else { if (isShortNameOption(arg) && arg.length() > 2 && dynamics.stream().anyMatch(arg::startsWith) == false) { LOG.debug("expand option: {}", arg); for (int i = 1, n = arg.length(); i < n; i++) { sawExpand = true; results.add("-" + arg.charAt(i)); } } else { sawEscape |= arg.equals("--"); results.add(arg); } } } if (sawExpand) { LOG.debug("expanded command line: {}", results); return results.toArray(new String[results.size()]); } return args; } }
JCommanderWrapper implements CommandBuilder<T> { private String[] expand(String[] args) { JCommander commander = findCommander(args); Params params = collectParams(commander); if (params.names.stream() .filter(JCommanderWrapper::isShortNameOption) .anyMatch(it -> it.length() > 2)) { return args; } List<String> dynamics = params.dynamicNames.stream() .filter(JCommanderWrapper::isShortNameOption) .collect(Collectors.toList()); boolean sawExpand = false; boolean sawEscape = false; List<String> results = new ArrayList<>(); for (String arg : args) { if (sawEscape) { results.add(arg); } else { if (isShortNameOption(arg) && arg.length() > 2 && dynamics.stream().anyMatch(arg::startsWith) == false) { LOG.debug("expand option: {}", arg); for (int i = 1, n = arg.length(); i < n; i++) { sawExpand = true; results.add("-" + arg.charAt(i)); } } else { sawEscape |= arg.equals("--"); results.add(arg); } } } if (sawExpand) { LOG.debug("expanded command line: {}", results); return results.toArray(new String[results.size()]); } return args; } JCommanderWrapper(String programName, Object command); }
JCommanderWrapper implements CommandBuilder<T> { private String[] expand(String[] args) { JCommander commander = findCommander(args); Params params = collectParams(commander); if (params.names.stream() .filter(JCommanderWrapper::isShortNameOption) .anyMatch(it -> it.length() > 2)) { return args; } List<String> dynamics = params.dynamicNames.stream() .filter(JCommanderWrapper::isShortNameOption) .collect(Collectors.toList()); boolean sawExpand = false; boolean sawEscape = false; List<String> results = new ArrayList<>(); for (String arg : args) { if (sawEscape) { results.add(arg); } else { if (isShortNameOption(arg) && arg.length() > 2 && dynamics.stream().anyMatch(arg::startsWith) == false) { LOG.debug("expand option: {}", arg); for (int i = 1, n = arg.length(); i < n; i++) { sawExpand = true; results.add("-" + arg.charAt(i)); } } else { sawEscape |= arg.equals("--"); results.add(arg); } } } if (sawExpand) { LOG.debug("expanded command line: {}", results); return results.toArray(new String[results.size()]); } return args; } JCommanderWrapper(String programName, Object command); @Override JCommanderWrapper<T> configure(Consumer<? super CommandBuilder<T>> configurator); @Override JCommanderWrapper<T> addCommand(T command); @Override JCommanderWrapper<T> addCommand(String name, T command); @Override JCommanderWrapper<T> addGroup(T command, Consumer<? super CommandBuilder<T>> configurator); @Override JCommanderWrapper<T> addGroup(String name, T command, Consumer<? super CommandBuilder<T>> configurator); Optional<T> parse(String... args); static void handle(ParameterException exception, Consumer<? super String> sink); }
JCommanderWrapper implements CommandBuilder<T> { private String[] expand(String[] args) { JCommander commander = findCommander(args); Params params = collectParams(commander); if (params.names.stream() .filter(JCommanderWrapper::isShortNameOption) .anyMatch(it -> it.length() > 2)) { return args; } List<String> dynamics = params.dynamicNames.stream() .filter(JCommanderWrapper::isShortNameOption) .collect(Collectors.toList()); boolean sawExpand = false; boolean sawEscape = false; List<String> results = new ArrayList<>(); for (String arg : args) { if (sawEscape) { results.add(arg); } else { if (isShortNameOption(arg) && arg.length() > 2 && dynamics.stream().anyMatch(arg::startsWith) == false) { LOG.debug("expand option: {}", arg); for (int i = 1, n = arg.length(); i < n; i++) { sawExpand = true; results.add("-" + arg.charAt(i)); } } else { sawEscape |= arg.equals("--"); results.add(arg); } } } if (sawExpand) { LOG.debug("expanded command line: {}", results); return results.toArray(new String[results.size()]); } return args; } JCommanderWrapper(String programName, Object command); @Override JCommanderWrapper<T> configure(Consumer<? super CommandBuilder<T>> configurator); @Override JCommanderWrapper<T> addCommand(T command); @Override JCommanderWrapper<T> addCommand(String name, T command); @Override JCommanderWrapper<T> addGroup(T command, Consumer<? super CommandBuilder<T>> configurator); @Override JCommanderWrapper<T> addGroup(String name, T command, Consumer<? super CommandBuilder<T>> configurator); Optional<T> parse(String... args); static void handle(ParameterException exception, Consumer<? super String> sink);  }