summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/cz/crcs/ectester/common')
-rw-r--r--src/cz/crcs/ectester/common/cli/ParserOptions.java13
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeCommandLine.java27
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeParser.java32
3 files changed, 64 insertions, 8 deletions
diff --git a/src/cz/crcs/ectester/common/cli/ParserOptions.java b/src/cz/crcs/ectester/common/cli/ParserOptions.java
index 4216ce3..ee2097e 100644
--- a/src/cz/crcs/ectester/common/cli/ParserOptions.java
+++ b/src/cz/crcs/ectester/common/cli/ParserOptions.java
@@ -3,18 +3,27 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
+import java.util.Collections;
+import java.util.List;
+
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public class ParserOptions {
private CommandLineParser parser;
private Options options;
+ private List<Argument> arguments;
public ParserOptions(CommandLineParser parser, Options options) {
this.parser = parser;
this.options = options;
}
+ public ParserOptions(CommandLineParser parser, Options options, List<Argument> arguments) {
+ this(parser, options);
+ this.arguments = arguments;
+ }
+
public CommandLineParser getParser() {
return parser;
}
@@ -22,4 +31,8 @@ public class ParserOptions {
public Options getOptions() {
return options;
}
+
+ public List<Argument> getArguments() {
+ return Collections.unmodifiableList(arguments);
+ }
}
diff --git a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
index 82d1e15..39607dc 100644
--- a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
+++ b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
@@ -3,6 +3,7 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
+import sun.reflect.generics.tree.Tree;
import java.util.Iterator;
import java.util.List;
@@ -55,11 +56,21 @@ public class TreeCommandLine extends CommandLine {
return cli;
}
+ public int getDepth() {
+ if (next == null) {
+ return 0;
+ }
+ return next.getDepth() + 1;
+ }
+
private <T> T getOption(String opt, BiFunction<CommandLine, String, T> getter, T defaultValue) {
if (opt.contains(".")) {
String[] parts = opt.split("\\.", 2);
if (next != null && parts[0].equals(next.getName())) {
- return getter.apply(next, parts[1]);
+ T result = getter.apply(next, parts[1]);
+ if (result != null)
+ return result;
+ return defaultValue;
}
return defaultValue;
}
@@ -138,11 +149,21 @@ public class TreeCommandLine extends CommandLine {
return cli.getOptions();
}
+ public boolean hasArg(int index) {
+ if (next != null) {
+ return next.hasArg(index);
+ }
+ return Math.abs(index) < cli.getArgs().length;
+ }
+
public String getArg(int index) {
- if (index < 0 || index >= cli.getArgs().length) {
+ if (next != null) {
+ return next.getArg(index);
+ }
+ if (index >= cli.getArgs().length) {
return null;
}
- return cli.getArgs()[index];
+ return index < 0 ? cli.getArgs()[cli.getArgs().length + index] : cli.getArgs()[index];
}
@Override
diff --git a/src/cz/crcs/ectester/common/cli/TreeParser.java b/src/cz/crcs/ectester/common/cli/TreeParser.java
index 9b197a5..77cce30 100644
--- a/src/cz/crcs/ectester/common/cli/TreeParser.java
+++ b/src/cz/crcs/ectester/common/cli/TreeParser.java
@@ -3,6 +3,7 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.*;
import java.util.*;
+import java.util.stream.Collectors;
/**
* @author Jan Jancar johny@neuromancer.sk
@@ -53,10 +54,10 @@ public class TreeParser implements CommandLineParser {
CommandLine cli = thisParser.parse(options, arguments, properties, true);
CommandLine subCli = null;
- String[] args = cli.getArgs();
+ String[] cliArgs = cli.getArgs();
String sub = null;
- if (args.length != 0) {
- sub = args[0];
+ if (cliArgs.length != 0) {
+ sub = cliArgs[0];
List<String> matches = new LinkedList<>();
String finalSub = sub;
@@ -73,8 +74,8 @@ public class TreeParser implements CommandLineParser {
if (matches.size() == 1) {
sub = matches.get(0);
ParserOptions subparser = parsers.get(sub);
- String[] remainingArgs = new String[args.length - 1];
- System.arraycopy(args, 1, remainingArgs, 0, args.length - 1);
+ String[] remainingArgs = new String[cliArgs.length - 1];
+ System.arraycopy(cliArgs, 1, remainingArgs, 0, cliArgs.length - 1);
subCli = subparser.getParser().parse(subparser.getOptions(), remainingArgs, true);
} else if (matches.size() > 1) {
throw new AmbiguousOptionException(sub, matches);
@@ -84,14 +85,35 @@ public class TreeParser implements CommandLineParser {
throw new MissingOptionException(new ArrayList(parsers.keySet()));
}
}
+
+ long requiredArgs = args.stream().filter(Argument::isRequired).count();
+ String reqArgs = String.join(" ", args.stream().filter(Argument::isRequired).map(Argument::getName).collect(Collectors.toList()));
+
if (subCli instanceof TreeCommandLine) {
TreeCommandLine subTreeCli = (TreeCommandLine) subCli;
+
+ TreeCommandLine lastCli = subTreeCli;
+ while (lastCli.getNext() != null) {
+ lastCli = lastCli.getNext();
+ }
+
+ if (lastCli.getArgs().length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ }
+
subTreeCli.setName(sub);
return new TreeCommandLine(cli, subTreeCli);
} else if (subCli != null) {
+ if (subCli.getArgs().length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ }
+
TreeCommandLine subTreeCli = new TreeCommandLine(sub, subCli, null);
return new TreeCommandLine(cli, subTreeCli);
} else {
+ if (cliArgs.length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ }
return new TreeCommandLine(cli, null);
}
}