Skip to content

Commit cc816dc

Browse files
committed
Make sure cli outputs usage in case of an error
For example when no command is provided. Fixes #110
1 parent 4e43c07 commit cc816dc

File tree

5 files changed

+94
-29
lines changed

5 files changed

+94
-29
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>org.utplsql</groupId>
66
<artifactId>cli</artifactId>
7-
<version>3.1.2-SNAPSHOT</version>
7+
<version>3.1.3-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>cli</name>

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

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import com.beust.jcommander.JCommander;
44
import com.beust.jcommander.Parameter;
55
import com.beust.jcommander.ParameterException;
6-
import org.utplsql.api.exception.DatabaseNotCompatibleException;
7-
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
8-
import org.utplsql.cli.exception.DatabaseConnectionFailed;
96

107
public class Cli {
118

@@ -26,36 +23,31 @@ static int runWithExitCode( String[] args ) {
2623
JCommander jc = new JCommander();
2724
jc.setProgramName("utplsql");
2825

29-
CommandProvider cmdProvider = new CommandProvider();
26+
CommandProvider cmdProvider = new CommandProvider(jc);
3027

3128
cmdProvider.commands().forEach(cmd -> jc.addCommand(cmd.getCommand(), cmd));
3229

3330
int exitCode = DEFAULT_ERROR_CODE;
3431

35-
try {
36-
jc.parse(args);
32+
if ( args.length >= 1 && args[0].equals("-h") ) // Help?
33+
{
34+
exitCode = 0;
35+
jc.usage();
36+
}
37+
else {
38+
try {
39+
jc.parse(args);
3740

38-
exitCode = cmdProvider.getCommand(jc.getParsedCommand()).run();
41+
exitCode = cmdProvider.getCommand(jc.getParsedCommand()).run();
3942

40-
} catch (ParameterException e) {
41-
if (jc.getParsedCommand() != null) {
42-
System.err.println(e.getMessage());
43-
jc.usage(jc.getParsedCommand());
44-
} else {
45-
jc.usage();
43+
} catch (ParameterException e) {
44+
exitCode = new HelpCommand(jc, e.getMessage()).run();
45+
} catch (Exception e) {
46+
e.printStackTrace();
4647
}
47-
} catch (Exception e) {
48-
e.printStackTrace();
4948
}
5049

5150
return exitCode;
5251
}
5352

54-
private static class HelpCommand {
55-
56-
@Parameter(names = {HELP_CMD, "--help"}, help = true)
57-
public boolean callHelp;
58-
59-
}
60-
6153
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package org.utplsql.cli;
22

3+
import com.beust.jcommander.JCommander;
4+
35
import java.util.HashMap;
46
import java.util.Map;
57
import java.util.stream.Stream;
68

79
public class CommandProvider {
810

911
private Map<String, ICommand> commands;
12+
private JCommander jCommander;
1013

11-
public CommandProvider() {
14+
public CommandProvider( JCommander jCommander ) {
15+
this.jCommander = jCommander;
1216
init();
1317
}
1418

@@ -18,6 +22,7 @@ private void init() {
1822
addCommand(new RunCommand());
1923
addCommand(new VersionInfoCommand());
2024
addCommand(new ReportersCommand());
25+
addCommand(new HelpCommand(jCommander));
2126
}
2227

2328
private void addCommand( ICommand command ) {
@@ -28,7 +33,7 @@ public ICommand getCommand( String key ) {
2833
if ( commands.containsKey(key))
2934
return commands.get(key.toLowerCase());
3035
else
31-
return new HelpCommand("Unknown command: '" + key + "'");
36+
return new HelpCommand(jCommander, "Unknown command: '" + key + "'");
3237
}
3338

3439
public Stream<ICommand> commands() {

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
package org.utplsql.cli;
22

3+
import com.beust.jcommander.JCommander;
4+
5+
/** Simple Help-Command which outputs an (optional) error message and the command line usage
6+
* @author pesse
7+
*/
38
public class HelpCommand implements ICommand {
49

510
private String errorMessage;
11+
private JCommander jCommander;
12+
13+
public HelpCommand(JCommander jCommander) {
14+
this.jCommander = jCommander;
15+
}
616

7-
public HelpCommand( String errorMessage ) {
17+
public HelpCommand( JCommander jCommander, String errorMessage ) {
18+
this.jCommander = jCommander;
819
this.errorMessage = errorMessage;
920
}
1021

1122
@Override
1223
public int run() {
13-
if ( errorMessage != null )
24+
if ( errorMessage != null ) {
1425
System.out.println(errorMessage);
15-
16-
return 1;
26+
if (jCommander.getParsedCommand() != null)
27+
jCommander.usage(jCommander.getParsedCommand());
28+
else
29+
jCommander.usage();
30+
return 1;
31+
}
32+
else {
33+
jCommander.usage();
34+
return 0;
35+
}
1736
}
1837

1938
@Override
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.utplsql.cli;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.utplsql.cli.util.SystemOutCapturer;
7+
8+
import java.io.IOException;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
public class HelpCommandTest {
14+
15+
16+
private SystemOutCapturer capturer;
17+
18+
@BeforeEach
19+
public void setupCaptureSystemOut() {
20+
capturer = new SystemOutCapturer();
21+
}
22+
23+
@Test
24+
public void callHelp() {
25+
26+
capturer.start();
27+
int result = TestHelper.runApp("-h");
28+
String output = capturer.stop();
29+
30+
assertEquals(0, result);
31+
assertTrue(output.contains("Usage:"));
32+
}
33+
34+
@Test
35+
public void callWithNoArgs() {
36+
37+
capturer.start();
38+
int result = TestHelper.runApp();
39+
String output = capturer.stop();
40+
41+
assertEquals(1, result);
42+
assertTrue(output.contains("Usage:"));
43+
}
44+
45+
@AfterEach
46+
public void cleanupCaptureSystemOut() throws IOException {
47+
capturer.stop();
48+
}
49+
}

0 commit comments

Comments
 (0)