Skip to content

Commit 6310f10

Browse files
committed
refactoring
1 parent 2341e4a commit 6310f10

13 files changed

Lines changed: 49 additions & 52 deletions

File tree

compiler/src/main/java/net/jbock/context/ParseOrExitMethod.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ public class ParseOrExitMethod {
4242
MethodSpec define() {
4343

4444
ParameterSpec args = builder(STRING_ARRAY, "args").build();
45-
ParameterSpec notSuccess = builder(generatedTypes.parseResultType(), "notSuccess").build();
45+
ParameterSpec notSuccess = builder(generatedTypes.parseResultType(), "failure").build();
4646
ParameterSpec err = builder(AtFileError.class, "err").build();
4747

4848
CodeBlock.Builder code = CodeBlock.builder();
4949
if (allMappings.stream().anyMatch(Mapping::isRequired)) {
5050
code.beginControlFlow("if ($1N.length == 0 || $2S.equals($1N[0]))", args, "--help")
51-
.addStatement("$T.builder().build().printHelp($N())",
52-
StandardErrorHandler.class, createModelMethod.get())
51+
.add("$T.builder().build()\n", StandardErrorHandler.class).indent()
52+
.add(".printUsageDocumentation($N());\n", createModelMethod.get()).unindent()
5353
.addStatement("$T.exit(0)", System.class)
5454
.endControlFlow();
5555
} else {
5656
code.beginControlFlow("if ($1N.length > 0 && $2S.equals($1N[0]))", args, "--help")
57-
.addStatement("$T.builder().build().printHelp($N())",
58-
StandardErrorHandler.class, createModelMethod.get())
57+
.add("$T.builder().build()\n", StandardErrorHandler.class).indent()
58+
.add(".printUsageDocumentation($N());\n", createModelMethod.get()).unindent()
5959
.addStatement("$T.exit(0)", System.class)
6060
.endControlFlow();
6161
}
@@ -64,7 +64,7 @@ MethodSpec define() {
6464
.add(".mapLeft($1N -> $1N.addModel($2N()))\n", err, createModelMethod.get())
6565
.add(".flatMap(this::$N)\n", parseMethod.get())
6666
.add(".orElseThrow($N -> {\n", notSuccess).indent()
67-
.addStatement("$T.builder().build().handle($N)",
67+
.addStatement("$T.builder().build().printErrorMessage($N)",
6868
StandardErrorHandler.class, notSuccess)
6969
.addStatement("$T.exit(1)", System.class)
7070
.addStatement("return new $T()", RuntimeException.class).unindent()

core/src/main/java/net/jbock/contrib/StandardErrorHandler.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.jbock.contrib;
22

33
import net.jbock.model.CommandModel;
4-
import net.jbock.util.HasMessage;
54
import net.jbock.util.ParsingFailed;
65

76
import java.io.PrintStream;
@@ -10,8 +9,8 @@
109
import java.util.Map;
1110

1211
/**
13-
* This class is responsible for standard error handling,
14-
* like printing messages and shutting down the JVM.
12+
* A convenience class that performs standard error handling,
13+
* like printing error messages and printing the usage documentation.
1514
*/
1615
public final class StandardErrorHandler {
1716

@@ -87,10 +86,9 @@ public Builder withMessages(Map<String, String> map) {
8786
}
8887

8988
/**
90-
* Set the value of the ansi attribute.
89+
* Sets the value of the ansi attribute.
9190
*
9291
* @param ansi if ansi codes should be used
93-
* when printing the usage documentation
9492
* @return the builder instance
9593
*/
9694
public Builder withAnsi(boolean ansi) {
@@ -99,7 +97,7 @@ public Builder withAnsi(boolean ansi) {
9997
}
10098

10199
/**
102-
* Create the error handler.
100+
* Creates the error handler.
103101
*
104102
* @return an error handler
105103
*/
@@ -117,26 +115,31 @@ public static Builder builder() {
117115
return new Builder();
118116
}
119117

120-
public void printHelp(CommandModel model) {
118+
/**
119+
* Prints the usage documentation.
120+
*
121+
* @param model command model
122+
*/
123+
public void printUsageDocumentation(CommandModel model) {
121124
UsageDocumentation.builder(model)
122125
.withOutputStream(out)
123126
.withAnsi(ansi)
124127
.withMessages(messages)
125128
.withTerminalWidth(terminalWidth)
126-
.build().printUsageDocumentation();
129+
.build()
130+
.printUsageDocumentation();
127131
out.flush();
128132
}
129133

130134
/**
131-
* This method does standard error handling like printing
132-
* error messages, or printing usage documentation.
135+
* Prints an error message.
133136
*
134-
* @param parsingFailed an object describing the error condition
137+
* @param failure an object describing the error condition
135138
*/
136-
public void handle(ParsingFailed parsingFailed) {
137-
CommandModel model = parsingFailed.commandModel();
139+
public void printErrorMessage(ParsingFailed failure) {
140+
CommandModel model = failure.commandModel();
138141
AnsiStyle ansiStyle = AnsiStyle.create(ansi);
139-
out.println(ansiStyle.red("ERROR:") + ' ' + ((HasMessage) parsingFailed).message());
142+
out.println(ansiStyle.red("ERROR:") + ' ' + failure.message());
140143
List<String> synopsis = Synopsis.create(model)
141144
.createSynopsis("Usage:");
142145
out.println(String.join(" ", synopsis));

core/src/main/java/net/jbock/util/ErrAtFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Non-exceptional failure object, indicates that reading options
99
* from the {@code @file} was unsuccessful.
1010
*/
11-
public final class ErrAtFile extends ParsingFailed implements HasMessage {
11+
public final class ErrAtFile extends ParsingFailed {
1212

1313
private final String message;
1414
private final Path atFile;

core/src/main/java/net/jbock/util/ErrConvert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Non-exceptional failure object that represents
88
* any runtime error during a converter invocation.
99
*/
10-
public final class ErrConvert extends ParsingFailed implements HasMessage {
10+
public final class ErrConvert extends ParsingFailed {
1111

1212
private final ConverterFailure converterFailure;
1313
private final Item item;

core/src/main/java/net/jbock/util/ErrMissingItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @see net.jbock.model.Multiplicity#REQUIRED
1111
*/
12-
public final class ErrMissingItem extends ParsingFailed implements HasMessage {
12+
public final class ErrMissingItem extends ParsingFailed {
1313

1414
private final Item item;
1515

core/src/main/java/net/jbock/util/ErrToken.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* @see net.jbock.Command
1010
*/
11-
public final class ErrToken extends ParsingFailed implements HasMessage {
11+
public final class ErrToken extends ParsingFailed {
1212

1313
private final ErrTokenType errorType;
1414
private final String token;

core/src/main/java/net/jbock/util/ExConvert.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package net.jbock.util;
22

3-
import net.jbock.Option;
4-
import net.jbock.Parameter;
5-
import net.jbock.Parameters;
63
import net.jbock.model.CommandModel;
74

85
/**
96
* A checked exception to signal that an error has occurred
107
* within a particular converter.
118
*
12-
* @see Option#converter()
13-
* @see Parameter#converter()
14-
* @see Parameters#converter()
9+
* <p>This class is internal API and should not be used
10+
* in client code. It may be removed without warning in future
11+
* releases.
1512
*/
1613
public final class ExConvert extends ExNotSuccess {
1714

core/src/main/java/net/jbock/util/ExMissingItem.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* A checked exception to signal that a required item was
77
* missing in the command line input.
88
*
9-
* @see net.jbock.model.Multiplicity#REQUIRED
9+
* <p>This class is internal API and should not be used
10+
* in client code. It may be removed without warning in future
11+
* releases.
1012
*/
1113
public final class ExMissingItem extends ExNotSuccess {
1214

core/src/main/java/net/jbock/util/ExNotSuccess.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* in the generated code. These are checked exceptions, to
88
* ensure none of them are thrown from the generated
99
* parse method.
10+
*
11+
* <p>This class is internal API and should not be used
12+
* in client code. It may be removed without warning in future
13+
* releases.
1014
*/
1115
public abstract class ExNotSuccess extends Exception {
1216

core/src/main/java/net/jbock/util/ExToken.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package net.jbock.util;
22

33
import net.jbock.model.CommandModel;
4-
import net.jbock.model.Item;
54

65
/**
76
* A checked exception to signal that a parsing error has occurred
87
* that is not associated with a particular item.
98
* Internal exception that may be thrown and caught
109
* in the generated code.
1110
*
12-
* @see Item
11+
* <p>This class is internal API and should not be used
12+
* in client code. It may be removed without warning in future
13+
* releases.
1314
*/
1415
public final class ExToken extends ExNotSuccess {
1516

0 commit comments

Comments
 (0)