Skip to content

Commit ded39d3

Browse files
committed
Implemented new Picocli help
1 parent 8eec2cb commit ded39d3

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

src/main/java/org/utplsql/cli/Cli.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Cli {
1414

1515
public static void main(String[] args) {
1616

17-
int exitCode = runWithExitCode(args);
17+
int exitCode = runPicocliWithExitCode(args);
1818

1919
System.exit(exitCode);
2020
}
@@ -67,15 +67,33 @@ static int runPicocliWithExitCode( String[] args ) {
6767

6868
List<CommandLine> parsedLines = commandLine.parse(args);
6969

70+
boolean commandWasRun = false;
7071
for ( CommandLine parsedLine : parsedLines ) {
72+
if (parsedLine.isUsageHelpRequested()) {
73+
parsedLine.usage(System.out);
74+
return 0;
75+
}
76+
else if (parsedLine.isVersionHelpRequested()) {
77+
parsedLine.printVersionHelp(System.out);
78+
return 0;
79+
}
80+
7181
Object command = parsedLine.getCommand();
7282
if ( command instanceof ICommand ) {
7383
exitCode = ((ICommand)command).run();
84+
commandWasRun = true;
7485
break;
7586
}
7687
}
77-
} catch (ParameterException e) {
78-
e.printStackTrace();
88+
89+
if ( !commandWasRun ) {
90+
commandLine.usage(System.out);
91+
}
92+
} catch (CommandLine.ParameterException e) {
93+
System.err.println(e.getMessage());
94+
if (!CommandLine.UnmatchedArgumentException.printSuggestions(e, System.err)) {
95+
e.getCommandLine().usage(System.err);
96+
}
7997
} catch (Exception e) {
8098
e.printStackTrace();
8199
}

src/main/java/org/utplsql/cli/RunPicocliCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ FileMapperConfig toFileMapperConfig() {
169169
}
170170
}
171171

172+
@Option(names = "-h", usageHelp = true, description = "display this help and exit")
173+
boolean help;
174+
172175
private RunAction runAction;
173176

174177
private String[] splitOrEmpty(String value) {

src/main/java/org/utplsql/cli/UtplsqlPicocliCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@
1212
})
1313
public class UtplsqlPicocliCommand {
1414

15+
@CommandLine.Option(names = "-h", usageHelp = true, description = "display this help and exit")
16+
boolean help;
17+
1518
}

src/test/java/org/utplsql/cli/HelpCommandTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ void callHelp() {
3131
assertTrue(output.contains("Usage:"));
3232
}
3333

34+
@Test
35+
void callRunHelp() {
36+
37+
capturer.start();
38+
int result = TestHelper.runApp("run", "-h");
39+
String output = capturer.stop();
40+
41+
assertEquals(0, result);
42+
assertTrue(output.contains("Usage:"));
43+
}
44+
3445
@Test
3546
void callWithNoArgs() {
3647

0 commit comments

Comments
 (0)