From: Ben Soares Date: Tue, 4 Apr 2023 17:28:16 +0000 (+0100) Subject: JAL-629 Start of Usage statement X-Git-Tag: Release_2_11_4_0~406 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=115334feb610d50d8ce91daf7e754baacbeb511b;p=jalview.git JAL-629 Start of Usage statement --- diff --git a/src/jalview/bin/Jalview.java b/src/jalview/bin/Jalview.java index d0e76f2..951578a 100755 --- a/src/jalview/bin/Jalview.java +++ b/src/jalview/bin/Jalview.java @@ -449,8 +449,12 @@ public class Jalview * @j2sIgnore */ { - if (aparser.contains("help") || aparser.contains("h") - || argparser.getBool(Arg.HELP)) + if (bootstrapArgs.contains(Arg.HELP)) + { + System.out.println(Arg.usage()); + Jalview.exit(null, 0); + } + if (aparser.contains("help") || aparser.contains("h")) { showUsage(); Jalview.exit(null, 0); diff --git a/src/jalview/bin/argparser/Arg.java b/src/jalview/bin/argparser/Arg.java index 7eaac09..65a865b 100644 --- a/src/jalview/bin/argparser/Arg.java +++ b/src/jalview/bin/argparser/Arg.java @@ -1,6 +1,7 @@ package jalview.bin.argparser; import java.util.Arrays; +import java.util.EnumSet; import java.util.List; import java.util.Locale; import java.util.stream.Collectors; @@ -24,9 +25,9 @@ public enum Arg static { - HELP.setOptions(Opt.UNARY); + HELP.setOptions("Display this help message", Opt.UNARY, Opt.BOOTSTRAP); CALCULATION.setOptions(true, Opt.BOOLEAN); // default "true" implies only - // expecting "--nocalculation" + // expecting "--nocalculation" MENUBAR.setOptions(true, Opt.BOOLEAN); STATUS.setOptions(true, Opt.BOOLEAN); SHOWOVERVIEW.setOptions(Opt.UNARY, Opt.LINKED); @@ -103,6 +104,8 @@ public enum Arg private boolean defaultBoolValue = false; + private String description = null; + private Arg() { this(new String[0]); @@ -121,8 +124,21 @@ public enum Arg public String argString() { - return new StringBuilder(ArgParser.DOUBLEDASH).append(getName()) - .toString(); + return argString(false); + } + + public String negateArgString() + { + return argString(true); + } + + private String argString(boolean negate) + { + StringBuilder sb = new StringBuilder(ArgParser.DOUBLEDASH); + if (negate && hasOption(Opt.BOOLEAN)) + sb.append(ArgParser.NEGATESTRING); + sb.append(getName()); + return sb.toString(); } public String toLongString() @@ -175,17 +191,91 @@ public enum Arg protected void setOptions(Opt... options) { - setOptions(false, options); + setOptions("", false, options); + } + + protected void setOptions(String desc, Opt... options) + { + setOptions(desc, false, options); } protected void setOptions(boolean defaultBoolValue, Opt... options) { + setOptions("", defaultBoolValue, options); + } + + protected void setOptions(String desc, boolean defaultBoolValue, + Opt... options) + { + this.description = desc; this.defaultBoolValue = defaultBoolValue; - argOptions = options; + this.argOptions = options; } protected boolean getDefaultBoolValue() { return defaultBoolValue; } -} + + private void setDescription(String d) + { + description = d; + } + + protected String getDescription() + { + return description; + } + + public static final String usage() + { + StringBuilder sb = new StringBuilder(); + + sb.append("Usage: jalview [args]"); + sb.append(System.lineSeparator()); + + int maxArgLength = 0; + for (Arg a : EnumSet.allOf(Arg.class)) + { + StringBuilder argSb = new StringBuilder(); + argSb.append(a.hasOption(Opt.BOOLEAN) ? booleanArgString(a) + : a.argString()); + if (a.hasOption(Opt.STRING)) + argSb.append("=value"); + if (argSb.length() > maxArgLength) + maxArgLength = argSb.length(); + } + + // might want to order these + for (Arg a : EnumSet.allOf(Arg.class)) + { + StringBuilder argSb = new StringBuilder(); + argSb.append(a.hasOption(Opt.BOOLEAN) ? booleanArgString(a) + : a.argString()); + if (a.hasOption(Opt.STRING)) + argSb.append("=value"); + sb.append(String.format("%-" + maxArgLength + "s - %s", + argSb.toString(), a.getDescription())); + if (a.hasOption(Opt.BOOLEAN)) + { + sb.append(" (default "); + sb.append(a.getDefaultBoolValue() ? a.argString() + : a.negateArgString()); + sb.append(')'); + } + sb.append(System.lineSeparator()); + } + return sb.toString(); + } + + public static String booleanArgString(Arg a) + { + StringBuilder sb = new StringBuilder(a.argString()); + if (a.hasOption(Opt.BOOLEAN)) + { + sb.append('/'); + sb.append(a.negateArgString()); + } + return sb.toString(); + } +} \ No newline at end of file