Skip to content

Commit ef7873b

Browse files
committed
fix: No longer showing stack traces by default
Added -v/--verbsoe flags that will show stack traces when enabled. Fixes #96
1 parent 3fa31c9 commit ef7873b

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/main/java/org/codejive/jpm/Main.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@
6161
})
6262
public class Main {
6363

64+
static boolean verbose = false;
65+
66+
@Mixin VerboseMixin verboseMixin;
67+
6468
@Command(
6569
name = "copy",
6670
aliases = {"c"},
@@ -69,6 +73,7 @@ public class Main {
6973
+ "By default jpm will try to create symbolic links to conserve space.\n\n"
7074
+ "Example:\n jpm copy org.apache.httpcomponents:httpclient:4.5.14\n")
7175
static class Copy implements Callable<Integer> {
76+
@Mixin VerboseMixin verboseMixin;
7277
@Mixin QuietMixin quietMixin;
7378
@Mixin ArtifactsMixin artifactsMixin;
7479

@@ -107,6 +112,7 @@ public Integer call() throws Exception {
107112
+ "match the given (partial) name.\n\n"
108113
+ "Example:\n jpm search httpclient\n")
109114
static class Search implements Callable<Integer> {
115+
@Mixin VerboseMixin verboseMixin;
110116
@Mixin QuietMixin quietMixin;
111117
@Mixin DepsMixin depsMixin;
112118

@@ -290,6 +296,7 @@ private static String getSelectedId(
290296
+ "If no artifacts are passed the app.yml file will be left untouched and only the existing dependencies in the file will be copied.\n\n"
291297
+ "Example:\n jpm install org.apache.httpcomponents:httpclient:4.5.14\n")
292298
static class Install implements Callable<Integer> {
299+
@Mixin VerboseMixin verboseMixin;
293300
@Mixin QuietMixin quietMixin;
294301
@Mixin OptionalArtifactsMixin optionalArtifactsMixin;
295302

@@ -318,6 +325,7 @@ public Integer call() throws Exception {
318325
+ "If no artifacts are passed the classpath for the dependencies defined in the app.yml file will be printed instead.\n\n"
319326
+ "Example:\n jpm path org.apache.httpcomponents:httpclient:4.5.14\n")
320327
static class PrintPath implements Callable<Integer> {
328+
@Mixin VerboseMixin verboseMixin;
321329
@Mixin OptionalArtifactsMixin optionalArtifactsMixin;
322330

323331
@Override
@@ -367,6 +375,7 @@ public Integer call() throws Exception {
367375
+ " jpm exec javac -cp {{deps}} -d out/classes --source-path src/main/java App.java\n"
368376
+ " jpm exec @kotlinc -cp {{deps}} -d out/classes src/main/kotlin/App.kt\n")
369377
static class Exec implements Callable<Integer> {
378+
@Mixin VerboseMixin verboseMixin;
370379
@Mixin DepsMixin depsMixin;
371380
@Mixin QuietMixin quietMixin;
372381

@@ -403,6 +412,7 @@ public Integer call() throws Exception {
403412
+ " jpm do test --arg verbose\n"
404413
+ " jpm do build -a --fresh test -a verbose\n")
405414
static class Do implements Callable<Integer> {
415+
@Mixin VerboseMixin verboseMixin;
406416
@Mixin DepsMixin depsMixin;
407417
@Mixin QuietMixin quietMixin;
408418

@@ -496,6 +506,7 @@ public Integer call() throws Exception {
496506
}
497507

498508
abstract static class DoAlias implements Callable<Integer> {
509+
@Mixin VerboseMixin verboseMixin;
499510
@Mixin DepsMixin depsMixin;
500511

501512
@Unmatched List<String> args = new ArrayList<>();
@@ -628,6 +639,15 @@ static class OptionalArtifactsMixin extends BaseArtifactsMixin {
628639
private String[] artifactNames = {};
629640
}
630641

642+
static class VerboseMixin {
643+
@Option(
644+
names = {"-v", "--verbose"},
645+
description = "Enable verbose output for debugging")
646+
public void setVerbose(boolean verbose) {
647+
Main.verbose = verbose;
648+
}
649+
}
650+
631651
static class QuietMixin {
632652
@Option(
633653
names = {"-q", "--quiet"},
@@ -642,11 +662,24 @@ private static void printStats(SyncResult stats) {
642662
(Integer) stats.copied, (Integer) stats.updated, (Integer) stats.deleted);
643663
}
644664

665+
static CommandLine.IExecutionExceptionHandler errorHandler =
666+
(ex, commandLine, parseResult) -> {
667+
System.err.println("Error: " + ex.getMessage());
668+
if (verbose) {
669+
ex.printStackTrace();
670+
} else {
671+
System.err.println(
672+
"(Run with --verbose for more details. If you believe you found a bug in jpm, open an issue at https://github.com/codejive/java-jpm/issues)");
673+
}
674+
return commandLine.getCommandSpec().exitCodeOnExecutionException();
675+
};
676+
645677
public static CommandLine getCommandLine() {
646678
return new CommandLine(new Main())
647679
.setStopAtPositional(true)
648680
.setAllowOptionsAsOptionParameters(true)
649-
.setAllowSubcommandsAsOptionParameters(true);
681+
.setAllowSubcommandsAsOptionParameters(true)
682+
.setExecutionExceptionHandler(errorHandler);
650683
}
651684

652685
/**

src/test/java/org/codejive/jpm/MainTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void testMainWithNoArgs() {
242242
int exitCode = cmd.execute();
243243
assertThat(exitCode >= 0).isTrue(); // Should not be negative (internal error)
244244
assertThat(capture.getErr()).contains("Missing required subcommand");
245-
assertThat(capture.getErr()).contains("Usage: jpm [-hV] [COMMAND]");
245+
assertThat(capture.getErr()).contains("Usage: jpm [-hvV] [COMMAND]");
246246
assertThat(capture.getErr())
247247
.contains("Simple command line tool for managing Maven artifacts");
248248
}
@@ -290,4 +290,16 @@ private void createAppYmlWithoutBuildAction() throws IOException {
290290
+ " hello: \"echo Hello World\"\n";
291291
Files.writeString(tempDir.resolve("app.yml"), yamlContent);
292292
}
293+
294+
@Test
295+
void testVerboseFlag() {
296+
try (TestOutputCapture capture = captureOutput()) {
297+
CommandLine cmd = Main.getCommandLine();
298+
int exitCode = cmd.execute("--help");
299+
assertThat(exitCode).isEqualTo(0);
300+
String output = capture.getOut();
301+
assertThat(output).contains("-v, --verbose");
302+
assertThat(output).contains("Enable verbose output for debugging");
303+
}
304+
}
293305
}

0 commit comments

Comments
 (0)