private static String generateOptionsCases(List<OptionSpec> argOptionFields, String indent, String currWord) { StringBuilder buff = new StringBuilder(1024); for (OptionSpec option : argOptionFields) { if (option.completionCandidates() != null) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -u|--timeUnit)\n" buff.append(format("%s COMPREPLY=( $( compgen -W \"${%s_OPTION_ARGS}\" -- %s ) )\n", indent, bashify(option.paramLabel()), currWord)); buff.append(format("%s return $?\n", indent)); buff.append(format("%s ;;\n", indent)); } else if (option.type().equals(File.class) || "java.nio.file.Path".equals(option.type().getName())) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -f|--file)\n" buff.append(format("%s compopt -o filenames\n", indent)); buff.append(format("%s COMPREPLY=( $( compgen -f -- %s ) ) # files\n", indent, currWord)); buff.append(format("%s return $?\n", indent)); buff.append(format("%s ;;\n", indent)); } else if (option.type().equals(InetAddress.class)) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -h|--host)\n" buff.append(format("%s compopt -o filenames\n", indent)); buff.append(format("%s COMPREPLY=( $( compgen -A hostname -- %s ) )\n", indent, currWord)); buff.append(format("%s return $?\n", indent)); buff.append(format("%s ;;\n", indent)); } else { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // no completions available buff.append(format("%s return\n", indent)); buff.append(format("%s ;;\n", indent)); } } return buff.toString(); }
private static void generateCompletionCandidates(StringBuilder buff, OptionSpec f) { buff.append(format(" %s_OPTION_ARGS=\"%s\" # %s values\n", bashify(f.paramLabel()), concat(" ", extract(f.completionCandidates())).trim(), f.longestName())); } private static List<String> extract(Iterable<String> generator) {
if (f.completionCandidates() != null) { generateCompletionCandidates(buff, f);
if (f.completionCandidates() != null) { generateCompletionCandidates(buff, f);
private static String generateOptionsCases(List<OptionSpec> argOptionFields, String indent, String currWord) { StringBuilder buff = new StringBuilder(1024); for (OptionSpec option : argOptionFields) { if (option.completionCandidates() != null) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -u|--timeUnit)\n" buff.append(format("%s COMPREPLY=( $( compgen -W \"${%s_OPTION_ARGS}\" -- %s ) )\n", indent, bashify(option.paramLabel()), currWord)); buff.append(format("%s return $?\n", indent)); buff.append(format("%s ;;\n", indent)); } else if (option.type().equals(File.class) || "java.nio.file.Path".equals(option.type().getName())) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -f|--file)\n" buff.append(format("%s compopt -o filenames\n", indent)); buff.append(format("%s COMPREPLY=( $( compgen -f -- %s ) ) # files\n", indent, currWord)); buff.append(format("%s return $?\n", indent)); buff.append(format("%s ;;\n", indent)); } else if (option.type().equals(InetAddress.class)) { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -h|--host)\n" buff.append(format("%s compopt -o filenames\n", indent)); buff.append(format("%s COMPREPLY=( $( compgen -A hostname -- %s ) )\n", indent, currWord)); buff.append(format("%s return $?\n", indent)); buff.append(format("%s ;;\n", indent)); } else { buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // no completions available buff.append(format("%s return\n", indent)); buff.append(format("%s ;;\n", indent)); } } return buff.toString(); }
@Test public void testCompletionCandidatesValues_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) String x; } CommandLine cmd = new CommandLine(new App()); assertEquals(Arrays.asList("A", "B", "C", "D"), extract(cmd.getCommandSpec().findOption("x").completionCandidates())); } @Test
@Test public void testCompletionCandidatesEnumValues_forOption() { class App { @Option(names = "-x") MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("E", "F", "G"), cmd.getCommandSpec().findOption("x").completionCandidates()); } @Test
private static void generateCompletionCandidates(StringBuilder buff, OptionSpec f) { buff.append(format(" %s_OPTION_ARGS=\"%s\" # %s values\n", bashify(f.paramLabel()), concat(" ", extract(f.completionCandidates())).trim(), f.longestName())); } private static List<String> extract(Iterable<String> generator) {
@Test public void testDefaultCompletionCandidatesNull() { class App { @Option(names = "-x") int x; } CommandLine cmd = new CommandLine(new App()); Iterable<String> candidates = cmd.getCommandSpec().findOption('x').completionCandidates(); assertNull(candidates); }
@Test public void testCompletionCandidatesPriority_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("A", "B", "C", "D"), cmd.getCommandSpec().findOption("x").completionCandidates()); } @Test
@Test public void testCompletionCandidatesValues_forOptionSpec() { CommandSpec spec = CommandSpec.create(); spec.add(OptionSpec.builder("-x").completionCandidates(Arrays.asList("x", "y", "z")).build()); assertEquals(Arrays.asList("x", "y", "z"), extract(spec.findOption("x").completionCandidates())); } @Test
private static void addCandidatesForArgsFollowing(OptionSpec optionSpec, List<CharSequence> candidates) { if (optionSpec != null) { addCompletionCandidates(optionSpec.completionCandidates(), candidates); } } private static void addCandidatesForArgsFollowing(PositionalParamSpec positionalSpec, List<CharSequence> candidates) {
@Test public void testCompletionCandidatesValues_forOptionSpec() { CommandSpec spec = CommandSpec.create(); spec.add(OptionSpec.builder("-x").completionCandidates(Arrays.asList("x", "y", "z")).build()); assertEquals(Arrays.asList("x", "y", "z"), extract(spec.findOption("x").completionCandidates())); } @Test
@Test public void testCompletionCandidatesEnumValues_forOption() { class App { @Option(names = "-x") MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("E", "F", "G"), cmd.getCommandSpec().findOption("x").completionCandidates()); } @Test
private static void addCandidatesForArgsFollowing(OptionSpec optionSpec, List<CharSequence> candidates) { if (optionSpec != null) { addCompletionCandidates(optionSpec.completionCandidates(), candidates); } } private static void addCandidatesForArgsFollowing(PositionalParamSpec positionalSpec, List<CharSequence> candidates) {
@Test public void testCompletionCandidatesPriority_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) MyEfgEnum x; } App app = new App(); CommandLine cmd = new CommandLine(app); assertEquals(Arrays.asList("A", "B", "C", "D"), cmd.getCommandSpec().findOption("x").completionCandidates()); } @Test
@Test public void testCompletionCandidatesValues_forOption() { class App { @Option(names = "-x", completionCandidates = MyAbcdCandidates.class) String x; } CommandLine cmd = new CommandLine(new App()); assertEquals(Arrays.asList("A", "B", "C", "D"), extract(cmd.getCommandSpec().findOption("x").completionCandidates())); } @Test
@Test public void testDefaultCompletionCandidatesNull() { class App { @Option(names = "-x") int x; } CommandLine cmd = new CommandLine(new App()); Iterable<String> candidates = cmd.getCommandSpec().findOption('x').completionCandidates(); assertNull(candidates); }