Skip to content

Commit

Permalink
Highlight demo help
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Sep 21, 2019
1 parent a479daf commit 718e9df
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions demo/src/main/java/org/apache/felix/gogo/jline/Posix.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.jline.builtins.Less;
import org.jline.builtins.Nano;
import org.jline.builtins.Options;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.Source;
import org.jline.builtins.Source.PathSource;
import org.jline.builtins.Source.URLSource;
Expand Down Expand Up @@ -151,27 +152,18 @@ public void _main(CommandSession session, String[] argv) {
process.err().println(e.getMessage());
process.error(2);
} catch (HelpException e) {
process.err().println(e.getMessage());
HelpException.highlight(e.getMessage(), HelpException.defaultStyle()).print(Shell.getTerminal(session));
process.error(0);
} catch (Exception e) {
process.err().println(argv[0] + ": " + e.toString());
process.error(1);
}
}

@SuppressWarnings("serial")
protected static class HelpException extends Exception {
public HelpException(String message) {
super(message);
}
}

protected Options parseOptions(CommandSession session, String[] usage, Object[] argv) throws Exception {
Options opt = Options.compile(usage, s -> get(session, s)).parse(argv, true);
if (opt.isSet("help")) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
opt.usage(new PrintStream(baos));
throw new HelpException(baos.toString());
throw new HelpException(opt.usage());
}
return opt;
}
Expand Down Expand Up @@ -254,7 +246,7 @@ protected void date(CommandSession session, Process process, String[] argv) thro
String output = null;
for (int i = 1; i < argv.length; i++) {
if ("-?".equals(argv[i]) || "--help".equals(argv[i])) {
throw new HelpException(String.join("\n", usage));
throw new HelpException(Options.compile(usage).usage());
}
else if ("-r".equals(argv[i])) {
if (i + 1 < argv.length) {
Expand Down

0 comments on commit 718e9df

Please sign in to comment.