From 6b3d396061c8f5336ad77e8482b454d68dcd4ffb Mon Sep 17 00:00:00 2001 From: Romain Manni-Bucau Date: Thu, 28 Aug 2014 07:56:35 +0200 Subject: [PATCH] #20 supporting already prefixed options --- .../org/tomitribe/crest/cmds/CmdMethod.java | 69 ++++++++++--------- .../crest/cmds/OverloadedCmdMethod.java | 10 +-- .../tomitribe/crest/cmds/processors/Help.java | 11 +-- .../org/tomitribe/crest/CmdMethodTest.java | 27 ++++++++ 4 files changed, 71 insertions(+), 46 deletions(-) diff --git a/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/CmdMethod.java b/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/CmdMethod.java index 241720c1..00a7aa93 100644 --- a/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/CmdMethod.java +++ b/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/CmdMethod.java @@ -66,6 +66,18 @@ * @version $Revision$ $Date$ */ public class CmdMethod implements Cmd { + private static final Join.NameCallback STRING_NAME_CALLBACK = new Join.NameCallback() { + @Override + public String getName(final String object) { + if (object.startsWith("-")) { + return object; + } + if (object.length() > 1) { + return "--" + object; + } + return "-" + object; + } + }; private final Target target; private final Method method; @@ -331,15 +343,7 @@ private List convert(final Arguments args) { } if (args.options.size() > 0) { - throw new IllegalArgumentException("Unknown arguments: " + Join.join(", ", new Join.NameCallback() { - @Override - public String getName(final String object) { - if (object.length() > 1) { - return "--" + object; - } - return "-" + object; - } - }, args.options.keySet())); + throw new IllegalArgumentException("Unknown arguments: " + Join.join(", ", STRING_NAME_CALLBACK, args.options.keySet())); } return converted; @@ -575,7 +579,7 @@ private Arguments(final String[] rawArgs) { } - private void getCommand(final String prefix, + private void getCommand(final String defaultPrefix, final String arg, final Map defaults, final Map supplied, @@ -584,9 +588,14 @@ private void getCommand(final String prefix, { String name; String value; + String prefix = defaultPrefix; if (arg.indexOf("=") > 0) { name = arg.substring(arg.indexOf(prefix) + prefix.length(), arg.indexOf("=")); + if (!defaults.containsKey(name) && !spec.aliases.containsKey(name)) { + name = arg.substring(0, arg.indexOf("=")); + prefix = ""; + } value = arg.substring(arg.indexOf("=") + 1); } else { if (arg.startsWith("--no-")) { @@ -625,6 +634,9 @@ private void getCommand(final String prefix, processOption(prefix, name, value, defaults, supplied, invalid, repeated); } + if (prefix.isEmpty()) { + processOption(prefix, name, value, defaults, supplied, invalid, repeated); + } } private void processOption(final String prefix, @@ -684,12 +696,7 @@ private void interpret(final Map map) { private void checkInvalid(final List invalid) { if (invalid.size() > 0) { - throw new IllegalArgumentException("Unknown options: " + Join.join(", ", new Join.NameCallback() { - @Override - public String getName(final String object) { - return object; - } - }, invalid)); + throw new IllegalArgumentException("Unknown options: " + Join.join(", ", STRING_NAME_CALLBACK, invalid)); } } @@ -710,16 +717,7 @@ private void checkRequired(final Map supplied) { } if (required.size() > 0) { - throw new IllegalArgumentException("Required: " + Join.join(", ", new Join.NameCallback() { - @Override - public String getName(final String object) { - if (object.length() > 1) { - return "--" + object; - } else { - return "-" + object; - } - } - }, required)); + throw new IllegalArgumentException("Required: " + Join.join(", ", STRING_NAME_CALLBACK, required)); } } @@ -759,14 +757,18 @@ private Collection findMatcingParametersOptions(String prefix, boolean i if (param instanceof OptionParam) { final OptionParam optionParam = (OptionParam) param; - if (optionParam.getName().startsWith(prefix)) { - if (optionParam.getName().length() > 1) { - result.add("--" + optionParam.getName()); + final String optionParamName = optionParam.getName(); + if (optionParamName.startsWith(prefix)) { + if (optionParamName.startsWith("-")) { + result.add(optionParamName); + continue; + } + if (optionParamName.length() > 1) { + result.add("--" + optionParamName); continue; } - if (isIncludeAliasChar) { - result.add("-" + optionParam.getName()); + result.add("-" + optionParamName); } } } @@ -778,10 +780,11 @@ private Collection findMatchingAliasOptions(String prefix, boolean isInc final List result = new ArrayList(); for (String alias : spec.aliases.keySet()) { if (alias.startsWith(prefix)) { - if (alias.length() > 1) { + if (alias.startsWith("-")) { + result.add(alias); + } else if (alias.length() > 1) { result.add("--" + alias); } - if (isIncludeAliasChar && alias.length() == 1) { result.add("-" + alias); } diff --git a/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/OverloadedCmdMethod.java b/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/OverloadedCmdMethod.java index 19514f9f..b572975c 100644 --- a/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/OverloadedCmdMethod.java +++ b/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/OverloadedCmdMethod.java @@ -23,7 +23,6 @@ import java.io.PrintStream; import java.util.Collection; import java.util.Comparator; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -126,13 +125,6 @@ public void add(final CmdMethod cmd) { @Override public Collection complete(String buffer, int cursorPosition) { - - final Set candidates = new HashSet(); - - for (CmdMethod cmdMethod : methods) { - candidates.addAll(cmdMethod.complete(buffer, cursorPosition)); - } - - return candidates; + throw new UnsupportedOperationException(); } } diff --git a/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/processors/Help.java b/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/processors/Help.java index cee39cd2..2c91a1a8 100644 --- a/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/processors/Help.java +++ b/tomitribe-crest/src/main/java/org/tomitribe/crest/cmds/processors/Help.java @@ -143,19 +143,22 @@ private Item(final OptionParam p, final String description) { String defaultValue = p.getDefaultValue(); + final String name = p.getName(); if (boolean.class.equals(type) || (Boolean.class.equals(type) && defaultValue != null)) { if ("true".equals(defaultValue)) { - this.flag = hasAlias ? Join.join(", ", "--no-" + p.getName(), getAlias(alias, false, true)) : "--no-" + p.getName(); + this.flag = hasAlias ? Join.join(", ", "--no-" + name, getAlias(alias, false, true)) : "--no-" + name; } else { - this.flag = hasAlias ? Join.join(", ", prefix + p.getName(), getAlias(alias, true, false)) : prefix + p.getName(); + final String optName = name.startsWith("-")? name : prefix + name; + this.flag = hasAlias ? Join.join(", ", optName, getAlias(alias, true, false)) : optName; } defaultValue = null; } else { - this.flag = hasAlias ? String.format("%s%s, %s=<%s>", prefix, p.getName(), getAlias(alias, true, false), p.getDisplayType()) - : String.format("%s%s=<%s>", prefix, p.getName(), p.getDisplayType()); + final String optName = name.startsWith("-")? name : prefix + name; + this.flag = hasAlias ? String.format("%s, %s=<%s>", optName, getAlias(alias, true, false), p.getDisplayType()) + : String.format("%s=<%s>", optName, p.getDisplayType()); } if (defaultValue != null) { diff --git a/tomitribe-crest/src/test/java/org/tomitribe/crest/CmdMethodTest.java b/tomitribe-crest/src/test/java/org/tomitribe/crest/CmdMethodTest.java index 09d7a991..4539686a 100644 --- a/tomitribe-crest/src/test/java/org/tomitribe/crest/CmdMethodTest.java +++ b/tomitribe-crest/src/test/java/org/tomitribe/crest/CmdMethodTest.java @@ -23,12 +23,15 @@ import org.tomitribe.crest.api.Required; import org.tomitribe.crest.api.StreamingOutput; import org.tomitribe.crest.cmds.Cmd; +import org.tomitribe.crest.cmds.processors.Help; import org.tomitribe.util.Files; import org.tomitribe.util.IO; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.io.PrintStream; import java.util.Map; /** @@ -43,6 +46,22 @@ public void testGetUsage() { assertEquals("ls [options] File", commands.get("ls").getUsage()); assertEquals("tail [options] File int", commands.get("tail").getUsage()); assertEquals("set [options]", commands.get("set").getUsage()); + assertEquals("prefixed [options]", commands.get("prefixed").getUsage()); + } + + public void testSupportUserDashPrefixing() { + Commands.prefixed = false; + final Cmd cmd = commands.get("prefixed"); + cmd.exec("-value=1", "----value=4"); + assertTrue(Commands.prefixed); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + cmd.help(new PrintStream(out)); + assertEquals( + "Usage: prefixed [options]" + + "Options: " + + " ----value= " + + " -value=", + new String(out.toByteArray()).replace(System.getProperty("line.separator"), "").trim()); } public void test() throws Exception { @@ -116,6 +135,14 @@ public void testFileParameter() { } public static class Commands { + private static boolean prefixed; + + @Command + public static void prefixed(@Option("-value") final String v, @Option("----value") final String four) { + assertEquals("1", v); + assertEquals("4", four); + prefixed = true; + } @Command public static void touch(final File file) {