Skip to content

Commit

Permalink
#20 supporting already prefixed options
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Aug 28, 2014
1 parent b4e036b commit 6b3d396
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
* @version $Revision$ $Date$
*/
public class CmdMethod implements Cmd {
private static final Join.NameCallback<String> STRING_NAME_CALLBACK = new Join.NameCallback<String>() {
@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;
Expand Down Expand Up @@ -331,15 +343,7 @@ private <T> List<Object> convert(final Arguments args) {
}

if (args.options.size() > 0) {
throw new IllegalArgumentException("Unknown arguments: " + Join.join(", ", new Join.NameCallback<String>() {
@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;
Expand Down Expand Up @@ -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<String, String> defaults,
final Map<String, String> supplied,
Expand All @@ -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-")) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -684,12 +696,7 @@ private void interpret(final Map<String, String> map) {

private void checkInvalid(final List<String> invalid) {
if (invalid.size() > 0) {
throw new IllegalArgumentException("Unknown options: " + Join.join(", ", new Join.NameCallback<String>() {
@Override
public String getName(final String object) {
return object;
}
}, invalid));
throw new IllegalArgumentException("Unknown options: " + Join.join(", ", STRING_NAME_CALLBACK, invalid));
}
}

Expand All @@ -710,16 +717,7 @@ private void checkRequired(final Map<String, String> supplied) {
}

if (required.size() > 0) {
throw new IllegalArgumentException("Required: " + Join.join(", ", new Join.NameCallback<String>() {
@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));
}
}

Expand Down Expand Up @@ -759,14 +757,18 @@ private Collection<String> 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);
}
}
}
Expand All @@ -778,10 +780,11 @@ private Collection<String> findMatchingAliasOptions(String prefix, boolean isInc
final List<String> result = new ArrayList<String>();
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -126,13 +125,6 @@ public void add(final CmdMethod cmd) {

@Override
public Collection<String> complete(String buffer, int cursorPosition) {

final Set<String> candidates = new HashSet<String>();

for (CmdMethod cmdMethod : methods) {
candidates.addAll(cmdMethod.complete(buffer, cursorPosition));
}

return candidates;
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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=<String> " +
" -value=<String>",
new String(out.toByteArray()).replace(System.getProperty("line.separator"), "").trim());
}

public void test() throws Exception {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b3d396

Please sign in to comment.