Skip to content

Commit f0ec068

Browse files
committed
Updates for better help command
1 parent faaa178 commit f0ec068

File tree

7 files changed

+62
-19
lines changed

7 files changed

+62
-19
lines changed

convex-cli/src/main/java/convex/cli/ACommand.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ public abstract class ACommand implements Runnable {
2323

2424
public void showUsage() {
2525
CommandLine cl=new CommandLine(this);
26+
showUsage(cl);
27+
}
28+
29+
protected void showUsage(CommandLine cl) {
2630
cl.setUsageHelpAutoWidth(true);
2731
cl.setUsageHelpWidth(100);
2832
cl.setUsageHelpLongOptionsMaxWidth(40);
29-
cl.usage(cli().commandLine().getOut(),Help.defaultColorScheme(Ansi.ON));
33+
Ansi ansi=cli().isColoured()?Ansi.ON:Ansi.OFF;
34+
cl.usage(cli().commandLine().getOut(),Help.defaultColorScheme(ansi));
3035
}
3136

3237
public CommandLine commandLine() {

convex-cli/src/main/java/convex/cli/ATopCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import picocli.CommandLine.ParentCommand;
44

5+
/**
6+
* Abstract base class for top level subcommands, i.e. convex xxxx
7+
*/
58
public abstract class ATopCommand extends ACommand {
69

710
@ParentCommand
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package convex.cli;
2+
3+
import java.io.PrintWriter;
4+
5+
import picocli.CommandLine;
6+
import picocli.CommandLine.Command;
7+
import picocli.CommandLine.Help.ColorScheme;
8+
import picocli.CommandLine.IHelpCommandInitializable2;
9+
import picocli.CommandLine.ParentCommand;
10+
11+
@Command(
12+
name="help",
13+
description="Show usage help",
14+
helpCommand = true)
15+
public class Help extends ACommand implements IHelpCommandInitializable2 {
16+
17+
@ParentCommand
18+
protected ACommand parent;
19+
protected CommandLine commandLine=null;
20+
21+
@Override
22+
public void init(CommandLine helpCommandLine, ColorScheme colorScheme, PrintWriter outWriter,
23+
PrintWriter errWriter) {
24+
this.commandLine=helpCommandLine;
25+
}
26+
27+
@Override
28+
public Main cli() {
29+
return parent.cli();
30+
}
31+
32+
@Override
33+
protected void execute() throws InterruptedException {
34+
parent.showUsage();
35+
}
36+
}

convex-cli/src/main/java/convex/cli/Main.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
*/
3737
@Command(name = "convex",
3838
subcommands = { Account.class, Key.class, Local.class, Peer.class, Query.class, Status.class, Desktop.class,
39-
Etch.class, Transact.class,
40-
CommandLine.HelpCommand.class },
39+
Etch.class, Transact.class, Help.class },
4140
usageHelpAutoWidth = true,
4241
sortOptions = true,
4342
mixinStandardHelpOptions = true,
@@ -52,8 +51,12 @@
5251
public class Main extends ACommand {
5352
private static Logger log = LoggerFactory.getLogger(Main.class);
5453

55-
public CommandLine commandLine = new CommandLine(this);
56-
54+
public CommandLine commandLine;
55+
56+
public Main() {
57+
commandLine= new CommandLine(this);
58+
commandLine.setExecutionExceptionHandler(new Main.ExceptionHandler());
59+
}
5760

5861
@Option(names = { "-S","--strict-security" },
5962
defaultValue = "false",
@@ -78,10 +81,6 @@ public class Main extends ACommand {
7881
description = "Specify verbosity level. Use -v0 to suppress user output, -v5 for all log output. Default: ${DEFAULT-VALUE}")
7982
private Integer verbose;
8083

81-
public Main() {
82-
commandLine = commandLine.setExecutionExceptionHandler(new Main.ExceptionHandler());
83-
}
84-
8584
@Override
8685
public void execute() {
8786
String art=Helpers.getConvexArt();
@@ -170,7 +169,7 @@ public static final class VersionProvider implements IVersionProvider {
170169
@Override
171170
public String[] getVersion() throws Exception {
172171
String s = Utils.getVersion();
173-
return new String[] { "Convex version: " + s };
172+
return new String[] { s };
174173
}
175174
}
176175

convex-cli/src/main/java/convex/cli/peer/Peer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import convex.cli.ATopCommand;
44
import convex.cli.Constants;
5-
import picocli.CommandLine;
5+
import convex.cli.Help;
66
import picocli.CommandLine.Command;
77
import picocli.CommandLine.Option;
88
import picocli.CommandLine.ScopeType;
@@ -21,7 +21,7 @@
2121
PeerStart.class,
2222
PeerList.class,
2323
PeerGenesis.class,
24-
CommandLine.HelpCommand.class
24+
Help.class
2525
},
2626
mixinStandardHelpOptions=true,
2727
description="Operate a Convex Peer")

convex-cli/src/test/java/convex/cli/CLIHelpTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public class CLIHelpTest {
88

99
@Test
1010
public void testHelp() {
11-
assertExecuteCommandLineResult(0, "^Usage: convex ", "--help");
12-
assertExecuteCommandLineResult(0, "^Usage: convex ", "-h");
13-
assertExecuteCommandLineResult(0, "^Usage: convex ", "help");
11+
assertExecuteCommandLineResult(0, "^Usage: convex ", "--help", "--no-color");
12+
assertExecuteCommandLineResult(0, "^Usage: convex ", "-h", "--no-color");
1413
assertExecuteCommandLineResult(0, "^Usage: convex account ", "account", "help");
1514
assertExecuteCommandLineResult(0, "^Usage: convex account balance ", "account", "balance", "--help");
1615
assertExecuteCommandLineResult(0, "^Usage: convex account create ", "account", "create", "--help");
@@ -19,14 +18,15 @@ public void testHelp() {
1918
assertExecuteCommandLineResult(0, "^Usage: convex key ", "key", "help");
2019
assertExecuteCommandLineResult(0, "^Usage: convex key generate ", "key", "generate", "--help");
2120
assertExecuteCommandLineResult(0, "^Usage: convex key list ", "key", "list", "--help");
22-
assertExecuteCommandLineResult(0, "^Usage: convex peer ", "peer", "help");
23-
assertExecuteCommandLineResult(0, "^Usage: convex peer start ", "peer", "start", "--help");
21+
assertExecuteCommandLineResult(0, "^Usage: convex peer ", "peer", "-h", "--no-color");
22+
assertExecuteCommandLineResult(0, "^Usage: convex peer start ", "peer", "start", "--help", "--no-color");
2423
assertExecuteCommandLineResult(0, "^Usage: convex local ", "local", "--help");
2524
assertExecuteCommandLineResult(0, "^Usage: convex local start ", "local", "start", "--help");
2625
assertExecuteCommandLineResult(0, "^Usage: convex local gui ", "local", "gui", "--help");
2726
assertExecuteCommandLineResult(0, "^Usage: convex query ", "query", "--help");
2827
assertExecuteCommandLineResult(0, "^Usage: convex status ", "status", "--help");
2928
assertExecuteCommandLineResult(0, "^Usage: convex transact ", "transact", "--help");
29+
// assertExecuteCommandLineResult(0, "^Usage: convex \\[", "help", "--help", "--no-color");
3030
}
3131

3232
}

convex-cli/src/test/java/convex/cli/HelperTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public void testSplitArray() {
1818
assertEquals(List.of("a"),Helpers.splitArrayParameter(" a "));
1919
}
2020

21-
public static void assertExecuteCommandLineResult(int returnCode, String patternText, String ... args) {
21+
public static void assertExecuteCommandLineResult(int exitCode, String patternText, String ... args) {
2222
CLTester tester = CLTester.run(args);
23-
assertEquals(returnCode, tester.getResult());
23+
tester.assertExitCode(exitCode);;
2424

2525
String output=tester.getOutput();
2626
Pattern regex = Pattern.compile(patternText, Pattern.MULTILINE + Pattern.DOTALL);

0 commit comments

Comments
 (0)