summaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/cli
diff options
context:
space:
mode:
Diffstat (limited to 'src/cz/crcs/ectester/common/cli')
-rw-r--r--src/cz/crcs/ectester/common/cli/Argument.java29
-rw-r--r--src/cz/crcs/ectester/common/cli/CLITools.java140
-rw-r--r--src/cz/crcs/ectester/common/cli/ParserOptions.java38
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeCommandLine.java178
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeParser.java128
5 files changed, 513 insertions, 0 deletions
diff --git a/src/cz/crcs/ectester/common/cli/Argument.java b/src/cz/crcs/ectester/common/cli/Argument.java
new file mode 100644
index 0000000..e9b6688
--- /dev/null
+++ b/src/cz/crcs/ectester/common/cli/Argument.java
@@ -0,0 +1,29 @@
+package cz.crcs.ectester.common.cli;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class Argument {
+ private String name;
+ private String desc;
+ private boolean required;
+
+ public Argument(String name, String desc, boolean isRequired) {
+ this.name = name;
+ this.desc = desc;
+ this.required = isRequired;
+ }
+
+
+ public String getName() {
+ return name;
+ }
+
+ public String getDesc() {
+ return desc;
+ }
+
+ public boolean isRequired() {
+ return required;
+ }
+}
diff --git a/src/cz/crcs/ectester/common/cli/CLITools.java b/src/cz/crcs/ectester/common/cli/CLITools.java
new file mode 100644
index 0000000..91f121f
--- /dev/null
+++ b/src/cz/crcs/ectester/common/cli/CLITools.java
@@ -0,0 +1,140 @@
+package cz.crcs.ectester.common.cli;
+
+import cz.crcs.ectester.common.ec.EC_Category;
+import cz.crcs.ectester.common.ec.EC_Data;
+import cz.crcs.ectester.data.EC_Store;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Map;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class CLITools {
+
+ /**
+ * Print help.
+ */
+ public static void help(String prog, String header, Options options, String footer, boolean usage) {
+ HelpFormatter help = new HelpFormatter();
+ help.setOptionComparator(null);
+ help.printHelp(prog, header, options, footer, usage);
+ }
+
+ private static void help(HelpFormatter help, PrintWriter pw, CommandLineParser cli, Options opts, int depth) {
+ if (opts.getOptions().size() > 0) {
+ help.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, opts, HelpFormatter.DEFAULT_LEFT_PAD + depth, HelpFormatter.DEFAULT_DESC_PAD);
+ }
+ if (cli instanceof TreeParser) {
+ TreeParser tp = (TreeParser) cli;
+ for (Argument arg : tp.getArgs()) {
+ String argname = arg.isRequired() ? "<" + arg.getName() + ">" : "[" + arg.getName() + "]";
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + (depth + 1) + "s" + argname + " " + arg.getDesc(), " "));
+ }
+ tp.getParsers().forEach((key, value) -> {
+ pw.println();
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + depth + "s" + key + ":", " "));
+ CLITools.help(help, pw, value.getParser(), value.getOptions(), depth + 1);
+ });
+ }
+ }
+
+ private static void usage(HelpFormatter help, PrintWriter pw, CommandLineParser cli, Options opts) {
+ StringWriter sw = new StringWriter();
+ PrintWriter upw = new PrintWriter(sw);
+ help.printUsage(upw, HelpFormatter.DEFAULT_WIDTH, "", opts);
+ if (cli instanceof TreeParser) {
+ upw.print(" ");
+ TreeParser tp = (TreeParser) cli;
+ String[] keys = tp.getParsers().keySet().toArray(new String[tp.getParsers().size()]);
+ if (keys.length > 0 && !tp.isRequired()) {
+ upw.print("[ ");
+ }
+
+ for (int i = 0; i < keys.length; ++i) {
+ String key = keys[i];
+ ParserOptions value = tp.getParsers().get(key);
+ upw.print("(" + key);
+ usage(help, upw, value.getParser(), value.getOptions());
+ upw.print(")");
+ if (i != keys.length - 1) {
+ upw.print(" | ");
+ }
+ }
+
+ if (keys.length > 0 && !tp.isRequired()) {
+ upw.print(" ]");
+ }
+
+ Argument[] args = tp.getArgs().toArray(new Argument[tp.getArgs().size()]);
+ if (args.length > 0) {
+ String[] argss = new String[tp.getArgs().size()];
+ for (int i = 0; i < args.length; ++i) {
+ Argument arg = args[i];
+ argss[i] = arg.isRequired() ? "<" + arg.getName() + ">" : "[" + arg.getName() + "]";
+ }
+ upw.print(" " + String.join(" ", argss));
+ }
+ }
+ pw.println(sw.toString().replaceAll("usage:( )?", "").replace("\n", ""));
+ }
+
+ /**
+ * Print tree help.
+ */
+ public static void help(String prog, String header, Options baseOpts, TreeParser baseParser, String footer, boolean printUsage) {
+ HelpFormatter help = new HelpFormatter();
+ help.setOptionComparator(null);
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, header);
+ if (printUsage) {
+ StringWriter uw = new StringWriter();
+ PrintWriter upw = new PrintWriter(uw);
+ usage(help, upw, baseParser, baseOpts);
+ pw.print("usage: " + prog);
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, uw.toString());
+ upw.close();
+ pw.println();
+ }
+ help(help, pw, baseParser, baseOpts, 1);
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, footer);
+ System.out.println(sw.toString());
+ }
+
+ /**
+ * Print version info.
+ */
+ public static void version(String description, String license) {
+ System.out.println(description);
+ System.out.println(license);
+ }
+
+ /**
+ * List categories and named curves.
+ */
+ public static void listNamed(EC_Store dataStore, String named) {
+ Map<String, EC_Category> categories = dataStore.getCategories();
+ if (named == null) {
+ // print all categories, briefly
+ for (EC_Category cat : categories.values()) {
+ System.out.println(cat);
+ }
+ } else if (categories.containsKey(named)) {
+ // print given category
+ System.out.println(categories.get(named));
+ } else {
+ // print given object
+ EC_Data object = dataStore.getObject(EC_Data.class, named);
+ if (object != null) {
+ System.out.println(object);
+ } else {
+ System.err.println("Named object " + named + " not found!");
+ }
+ }
+ }
+}
diff --git a/src/cz/crcs/ectester/common/cli/ParserOptions.java b/src/cz/crcs/ectester/common/cli/ParserOptions.java
new file mode 100644
index 0000000..ee2097e
--- /dev/null
+++ b/src/cz/crcs/ectester/common/cli/ParserOptions.java
@@ -0,0 +1,38 @@
+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;
+ }
+
+ 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
new file mode 100644
index 0000000..6a044d2
--- /dev/null
+++ b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
@@ -0,0 +1,178 @@
+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 java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.function.BiFunction;
+
+/**
+ * @author Jan Jancar johny@neuromancer.sk
+ */
+public class TreeCommandLine extends CommandLine {
+ private String name = "";
+ private TreeCommandLine next;
+ private CommandLine cli;
+
+ public TreeCommandLine(CommandLine cli, TreeCommandLine next) {
+ this.cli = cli;
+ this.next = next;
+ }
+
+ public TreeCommandLine(String name, CommandLine cli, TreeCommandLine next) {
+ this(cli, next);
+ this.name = name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getNextName() {
+ if (next != null) {
+ return next.getName();
+ }
+ return null;
+ }
+
+ public TreeCommandLine getNext() {
+ return next;
+ }
+
+ public boolean isNext(String next) {
+ return Objects.equals(getNextName(), next);
+ }
+
+ public CommandLine getThis() {
+ 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())) {
+ T result = getter.apply(next, parts[1]);
+ if (result != null)
+ return result;
+ return defaultValue;
+ }
+ return defaultValue;
+ }
+ return getter.apply(cli, opt);
+ }
+
+ @Override
+ public boolean hasOption(String opt) {
+ return getOption(opt, CommandLine::hasOption, false);
+ }
+
+ @Override
+ public boolean hasOption(char opt) {
+ return cli.hasOption(opt);
+ }
+
+ @Override
+ public Object getParsedOptionValue(String opt) throws ParseException {
+ if (opt.contains(".")) {
+ String[] parts = opt.split("\\.", 2);
+ if (next != null && parts[0].equals(next.getName())) {
+ return next.getParsedOptionValue(parts[1]);
+ }
+ return null;
+ }
+ return cli.getParsedOptionValue(opt);
+ }
+
+ @Override
+ public Object getOptionObject(char opt) {
+ return cli.getOptionObject(opt);
+ }
+
+ @Override
+ public String getOptionValue(String opt) {
+ return getOption(opt, CommandLine::getOptionValue, null);
+ }
+
+ @Override
+ public String getOptionValue(char opt) {
+ return cli.getOptionValue(opt);
+ }
+
+ @Override
+ public String[] getOptionValues(String opt) {
+ return getOption(opt, CommandLine::getOptionValues, null);
+ }
+
+ @Override
+ public String[] getOptionValues(char opt) {
+ return cli.getOptionValues(opt);
+ }
+
+ @Override
+ public String getOptionValue(String opt, String defaultValue) {
+ return getOption(opt, CommandLine::getOptionValue, defaultValue);
+ }
+
+ @Override
+ public String getOptionValue(char opt, String defaultValue) {
+ return cli.getOptionValue(opt, defaultValue);
+ }
+
+ @Override
+ public Properties getOptionProperties(String opt) {
+ return getOption(opt, CommandLine::getOptionProperties, new Properties());
+ }
+
+ @Override
+ public Iterator<Option> iterator() {
+ return cli.iterator();
+ }
+
+ @Override
+ public Option[] getOptions() {
+ return cli.getOptions();
+ }
+
+ public boolean hasArg(int index) {
+ return getArg(index) != null;
+ }
+
+ public String getArg(int index) {
+ if (next != null) {
+ return next.getArg(index);
+ }
+ String[] args = cli.getArgs();
+ if (index >= args.length) {
+ return null;
+ }
+ if (index < 0 && -index > args.length) {
+ return null;
+ }
+ return index < 0 ? args[args.length + index] : args[index];
+ }
+
+ @Override
+ public String[] getArgs() {
+ return cli.getArgs();
+ }
+
+ @Override
+ public List<String> getArgList() {
+ return cli.getArgList();
+ }
+}
diff --git a/src/cz/crcs/ectester/common/cli/TreeParser.java b/src/cz/crcs/ectester/common/cli/TreeParser.java
new file mode 100644
index 0000000..f1a1980
--- /dev/null
+++ b/src/cz/crcs/ectester/common/cli/TreeParser.java
@@ -0,0 +1,128 @@
+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
+ */
+public class TreeParser implements CommandLineParser {
+ private Map<String, ParserOptions> parsers;
+ private boolean required;
+ private List<Argument> args = Collections.emptyList();
+
+ public TreeParser(Map<String, ParserOptions> parsers, boolean required) {
+ this.parsers = parsers;
+ this.required = required;
+ }
+
+ public TreeParser(Map<String, ParserOptions> parsers, boolean required, List<Argument> args) {
+ this(parsers, required);
+ this.args = args;
+ }
+
+ public Map<String, ParserOptions> getParsers() {
+ return Collections.unmodifiableMap(parsers);
+ }
+
+ public boolean isRequired() {
+ return required;
+ }
+
+ public List<Argument> getArgs() {
+ return Collections.unmodifiableList(args);
+ }
+
+ @Override
+ public TreeCommandLine parse(Options options, String[] arguments) throws ParseException {
+ return this.parse(options, arguments, null);
+ }
+
+ public TreeCommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException {
+ return this.parse(options, arguments, properties, false);
+ }
+
+ @Override
+ public TreeCommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException {
+ return this.parse(options, arguments, null, stopAtNonOption);
+ }
+
+ public TreeCommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException {
+ DefaultParser thisParser = new DefaultParser();
+ CommandLine cli = thisParser.parse(options, arguments, properties, true);
+
+ CommandLine subCli = null;
+ String[] cliArgs = cli.getArgs();
+ String sub = null;
+ if (cliArgs.length != 0) {
+ sub = cliArgs[0];
+
+ List<String> matches = new LinkedList<>();
+ String finalSub = sub;
+ for (Map.Entry<String, ParserOptions> entry : parsers.entrySet()) {
+ if (entry.getKey().equalsIgnoreCase(finalSub)) {
+ matches.clear();
+ matches.add(finalSub);
+ break;
+ } else if (entry.getKey().startsWith(finalSub)) {
+ matches.add(entry.getKey());
+ }
+ }
+
+ if (matches.size() == 1) {
+ sub = matches.get(0);
+ ParserOptions subparser = parsers.get(sub);
+ 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);
+ }
+ } else {
+ if (required) {
+ throw new MissingOptionException(new ArrayList(parsers.keySet()));
+ }
+ }
+
+ int maxArgs = args.size();
+ 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);
+ } else if (lastCli.getArgs().length > maxArgs) {
+ throw new MissingArgumentException("Too many arguments.");
+ }
+
+ subTreeCli.setName(sub);
+ return new TreeCommandLine(cli, subTreeCli);
+ } else if (subCli != null) {
+ if (subCli.getArgs().length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ } else if (subCli.getArgs().length > maxArgs) {
+ throw new MissingArgumentException("Too many arguments.");
+ }
+
+ TreeCommandLine subTreeCli = new TreeCommandLine(sub, subCli, null);
+ return new TreeCommandLine(cli, subTreeCli);
+ } else {
+ if (cliArgs.length < requiredArgs) {
+ throw new MissingArgumentException("Not enough arguments: " + reqArgs);
+ } else if (cliArgs.length > maxArgs) {
+ throw new MissingArgumentException("Too many arguments.");
+ }
+
+ return new TreeCommandLine(cli, null);
+ }
+ }
+}