diff options
Diffstat (limited to 'src/cz/crcs/ectester/common/cli')
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/Argument.java | 29 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/CLITools.java | 161 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/Colors.java | 97 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/ParserOptions.java | 35 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/TreeCommandLine.java | 179 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/TreeParser.java | 130 |
6 files changed, 0 insertions, 631 deletions
diff --git a/src/cz/crcs/ectester/common/cli/Argument.java b/src/cz/crcs/ectester/common/cli/Argument.java deleted file mode 100644 index e9b6688..0000000 --- a/src/cz/crcs/ectester/common/cli/Argument.java +++ /dev/null @@ -1,29 +0,0 @@ -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 deleted file mode 100644 index 82ab530..0000000 --- a/src/cz/crcs/ectester/common/cli/CLITools.java +++ /dev/null @@ -1,161 +0,0 @@ -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(Colors.bold(prog), header, options, footer, usage); - } - - private static void help(HelpFormatter help, PrintWriter pw, String cmd, ParserOptions parser, int depth) { - String description = parser.getDescription() == null ? "" : " | " + parser.getDescription() + " |"; - help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + depth + "s" + cmd + ":" + description, " ")); - CLITools.help(help, pw, parser.getParser(), parser.getOptions(), depth + 1); - } - - 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(help, pw, key, value, depth); - }); - } - } - - 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: " + Colors.bold(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()); - } - - public static void help(String header, TreeParser baseParser, String footer, String command) { - ParserOptions opts = baseParser.getParsers().get(command); - if (opts == null) { - System.err.println("Command not found: " + command); - return; - } - HelpFormatter help = new HelpFormatter(); - help.setOptionComparator(null); - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, header); - help(help, pw, command, opts, 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/Colors.java b/src/cz/crcs/ectester/common/cli/Colors.java deleted file mode 100644 index 7601088..0000000 --- a/src/cz/crcs/ectester/common/cli/Colors.java +++ /dev/null @@ -1,97 +0,0 @@ -package cz.crcs.ectester.common.cli; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -/** - * @author Diogo Nunes - * @author Jan Jancar johny@neuromancer.sk - * Adapted from https://github.com/dialex/JCDP/ - */ -public class Colors { - public static boolean enabled = false; - - public interface ANSIParam { - } - - public enum Foreground implements ANSIParam { - BLACK("30"), RED("31"), GREEN("32"), YELLOW("33"), BLUE("34"), MAGENTA("35"), CYAN("36"), WHITE("37"), NONE(""); - private final String code; - - Foreground(String code) { - this.code = code; - } - - @Override - public String toString() { - return code; - } - } - - public enum Background implements ANSIParam { - BLACK("40"), RED("41"), GREEN("42"), YELLOW("43"), BLUE("44"), MAGENTA("45"), CYAN("46"), WHITE("47"), NONE(""); - private final String code; - - Background(String code) { - this.code = code; - } - - @Override - public String toString() { - return code; - } - } - - public enum Attribute implements ANSIParam { - CLEAR("0"), BOLD("1"), LIGHT("1"), DARK("2"), UNDERLINE("4"), REVERSE("7"), HIDDEN("8"), NONE(""); - private final String code; - - Attribute(String code) { - this.code = code; - } - - @Override - public String toString() { - return code; - } - } - - private static final String PREFIX = "\033["; - private static final String SEPARATOR = ";"; - private static final String POSTFIX = "m"; - - public static String colored(String text, ANSIParam... params) { - if (!enabled) { - return text; - } - Optional<Foreground> fg = Arrays.stream(params).filter(Foreground.class::isInstance).map(Foreground.class::cast).findFirst(); - Optional<Background> bg = Arrays.stream(params).filter(Background.class::isInstance).map(Background.class::cast).findFirst(); - List<Attribute> attr = Arrays.stream(params).filter(Attribute.class::isInstance).distinct().map(Attribute.class::cast).collect(Collectors.toList()); - - List<ANSIParam> apply = new LinkedList<>(); - apply.addAll(attr); - fg.ifPresent(apply::add); - bg.ifPresent(apply::add); - List<String> codes = apply.stream().map(Object::toString).collect(Collectors.toList()); - return PREFIX + String.join(SEPARATOR, codes) + POSTFIX + text + PREFIX + Attribute.CLEAR + POSTFIX; - } - - public static String error(String text) { - return colored(text, Foreground.RED, Attribute.BOLD); - } - - public static String ok(String text) { - return colored(text, Foreground.GREEN, Attribute.BOLD); - } - - public static String bold(String text) { - return colored(text, Attribute.BOLD); - } - - public static String underline(String text) { - return colored(text, Attribute.UNDERLINE); - } -}
\ No newline at end of file diff --git a/src/cz/crcs/ectester/common/cli/ParserOptions.java b/src/cz/crcs/ectester/common/cli/ParserOptions.java deleted file mode 100644 index 7300cbb..0000000 --- a/src/cz/crcs/ectester/common/cli/ParserOptions.java +++ /dev/null @@ -1,35 +0,0 @@ -package cz.crcs.ectester.common.cli; - -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.Options; - -/** - * @author Jan Jancar johny@neuromancer.sk - */ -public class ParserOptions { - private CommandLineParser parser; - private Options options; - private String description; - - public ParserOptions(CommandLineParser parser, Options options) { - this.parser = parser; - this.options = options; - } - - public ParserOptions(CommandLineParser parser, Options options, String description) { - this(parser, options); - this.description = description; - } - - public CommandLineParser getParser() { - return parser; - } - - public Options getOptions() { - return options; - } - - public String getDescription() { - return description; - } -} diff --git a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java b/src/cz/crcs/ectester/common/cli/TreeCommandLine.java deleted file mode 100644 index d758b78..0000000 --- a/src/cz/crcs/ectester/common/cli/TreeCommandLine.java +++ /dev/null @@ -1,179 +0,0 @@ -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 - */ -@SuppressWarnings("serial") -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 deleted file mode 100644 index 657318d..0000000 --- a/src/cz/crcs/ectester/common/cli/TreeParser.java +++ /dev/null @@ -1,130 +0,0 @@ -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); - } - } -} |
