aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/cz/crcs/ectester/common/cli/Argument.java29
-rw-r--r--src/cz/crcs/ectester/common/cli/CLITools.java52
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeCommandLine.java23
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeParser.java20
4 files changed, 103 insertions, 21 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
index 1b73207..77b3794 100644
--- a/src/cz/crcs/ectester/common/cli/CLITools.java
+++ b/src/cz/crcs/ectester/common/cli/CLITools.java
@@ -25,14 +25,20 @@ public class CLITools {
help.printHelp(prog, header, options, footer, usage);
}
- private static void help(HelpFormatter help, PrintWriter pw, CommandLineParser cli, int depth) {
+ 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) -> {
- help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + String.valueOf(depth) + "s" + key + ":", " "));
- help.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, value.getOptions(), HelpFormatter.DEFAULT_LEFT_PAD + depth, HelpFormatter.DEFAULT_DESC_PAD);
pw.println();
- CLITools.help(help, pw, value.getParser(), depth + 1);
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + depth + "s" + key + ":", " "));
+ CLITools.help(help, pw, value.getParser(), value.getOptions(), depth + 1);
});
}
}
@@ -41,22 +47,37 @@ public class CLITools {
StringWriter sw = new StringWriter();
PrintWriter upw = new PrintWriter(sw);
help.printUsage(upw, HelpFormatter.DEFAULT_WIDTH, "", opts);
- upw.print(" ");
if (cli instanceof TreeParser) {
+ upw.print(" ");
TreeParser tp = (TreeParser) cli;
- if (!tp.isRequired()) {
+ String[] keys = tp.getParsers().keySet().toArray(new String[tp.getParsers().size()]);
+ if (keys.length > 0 && !tp.isRequired()) {
upw.print("[ ");
}
- tp.getParsers().forEach((key, value) -> {
- upw.print("( " + key + " ");
+
+ 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 (!tp.isRequired()) {
+ if (i != keys.length - 1) {
+ upw.print(" | ");
+ }
+ }
+ Argument[] args = tp.getArgs().toArray(new Argument[tp.getArgs().size()]);
+ 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));
+
+ if (keys.length > 0 && !tp.isRequired()) {
upw.print(" ]");
}
}
- pw.println(sw.toString().substring(8).replace("\n", ""));
+ pw.println(sw.toString().replaceAll("usage:( )?", "").replace("\n", ""));
}
/**
@@ -67,18 +88,17 @@ public class CLITools {
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(prog + " usage: ");
+ pw.print("usage: " + prog);
help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, uw.toString());
upw.close();
+ pw.println();
}
- help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, header);
- help.printOptions(pw, HelpFormatter.DEFAULT_WIDTH, baseOpts, HelpFormatter.DEFAULT_LEFT_PAD, HelpFormatter.DEFAULT_DESC_PAD);
- pw.println();
- help(help, pw, baseParser, 1);
+ help(help, pw, baseParser, baseOpts, 1);
help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, footer);
System.out.println(sw.toString());
}
diff --git a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
index ef6079e..e0927fa 100644
--- a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
+++ b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java
@@ -5,6 +5,8 @@ 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;
@@ -45,6 +47,10 @@ public class TreeCommandLine extends CommandLine {
return next;
}
+ public boolean isNext(String next) {
+ return Objects.equals(getNextName(), next);
+ }
+
public CommandLine getThis() {
return cli;
}
@@ -131,4 +137,21 @@ public class TreeCommandLine extends CommandLine {
public Option[] getOptions() {
return cli.getOptions();
}
+
+ public String getArg(int index) {
+ if (index < 0 || index >= cli.getArgs().length) {
+ return null;
+ }
+ return cli.getArgs()[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
index 8e7edb4..9b197a5 100644
--- a/src/cz/crcs/ectester/common/cli/TreeParser.java
+++ b/src/cz/crcs/ectester/common/cli/TreeParser.java
@@ -10,12 +10,18 @@ import java.util.*;
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);
}
@@ -24,21 +30,25 @@ public class TreeParser implements CommandLineParser {
return required;
}
+ public List<Argument> getArgs() {
+ return Collections.unmodifiableList(args);
+ }
+
@Override
- public CommandLine parse(Options options, String[] arguments) throws ParseException {
+ public TreeCommandLine parse(Options options, String[] arguments) throws ParseException {
return this.parse(options, arguments, null);
}
- public CommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException {
+ public TreeCommandLine parse(Options options, String[] arguments, Properties properties) throws ParseException {
return this.parse(options, arguments, properties, false);
}
@Override
- public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException {
+ public TreeCommandLine parse(Options options, String[] arguments, boolean stopAtNonOption) throws ParseException {
return this.parse(options, arguments, null, stopAtNonOption);
}
- public CommandLine parse(Options options, String[] arguments, Properties properties, boolean stopAtNonOption) throws ParseException {
+ 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);
@@ -67,7 +77,7 @@ public class TreeParser implements CommandLineParser {
System.arraycopy(args, 1, remainingArgs, 0, args.length - 1);
subCli = subparser.getParser().parse(subparser.getOptions(), remainingArgs, true);
} else if (matches.size() > 1) {
- throw new UnrecognizedOptionException("Ambiguous option: " + sub + ", couldn't match. Partially matches: " + String.join(",", matches.toArray(new String[0])) + ".", sub);
+ throw new AmbiguousOptionException(sub, matches);
}
} else {
if (required) {