Skip to content

Commit cdde1ea

Browse files
committed
remove HelpRequested, add StandardErrorHandler.printHelp instead
1 parent 44c4f13 commit cdde1ea

49 files changed

Lines changed: 150 additions & 242 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/src/main/java/net/jbock/annotated/AnnotatedOption.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ static AnnotatedOption createOption(
4040
.sorted(UNIX_NAMES_FIRST_COMPARATOR)
4141
.collect(toList());
4242
String paramLabel = option.paramLabel().or(() -> names.stream()
43-
.filter(name -> name.startsWith("--"))
44-
.map(name -> name.substring(2))
45-
.map(s -> s.toUpperCase(Locale.US))
46-
.findFirst())
43+
.filter(name -> name.startsWith("--"))
44+
.map(name -> name.substring(2))
45+
.map(s -> s.toUpperCase(Locale.US))
46+
.findFirst())
4747
.orElseGet(() -> SnakeName.create(option.simpleName().toString())
4848
.snake('_')
4949
.toUpperCase(Locale.US));

compiler/src/main/java/net/jbock/annotated/AnnotationUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ final class AnnotationUtil {
2424
private static final String CONVERTER_ATTRIBUTE = "converter";
2525

2626
private static final Set<String> ANNOTATIONS = Stream.of(
27-
Parameter.class,
28-
Parameters.class,
29-
Option.class)
27+
Parameter.class,
28+
Parameters.class,
29+
Option.class)
3030
.map(Class::getCanonicalName).collect(toSet());
3131

3232
// visible for testing

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private List<CodeBlock> tailExpressionOption(Mapping<AnnotatedOption> m, int i)
162162
private List<CodeBlock> tailExpressionParameter(Mapping<AnnotatedParameter> m, int i) {
163163
if (m.multiplicity() == Multiplicity.REQUIRED) {
164164
return List.of(CodeBlock.of(".orElseThrow(() -> new $T($T.$L, $L))",
165-
ExMissingItem.class, ItemType.class, ItemType.PARAMETER, i),
165+
ExMissingItem.class, ItemType.class, ItemType.PARAMETER, i),
166166
orElseThrowConverterError(ItemType.PARAMETER, i));
167167
}
168168
checkArgument(m.multiplicity() == OPTIONAL);

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package net.jbock.context;
22

33
import com.squareup.javapoet.ArrayTypeName;
4-
import com.squareup.javapoet.CodeBlock;
54
import com.squareup.javapoet.FieldSpec;
6-
import com.squareup.javapoet.ParameterSpec;
75
import net.jbock.annotated.AnnotatedOption;
86
import net.jbock.annotated.AnnotatedParameter;
97
import net.jbock.convert.Mapping;
108
import net.jbock.processor.SourceElement;
11-
import net.jbock.util.HelpRequested;
129

1310
import java.util.ArrayList;
1411
import java.util.EnumMap;
@@ -34,7 +31,6 @@ class CommonFields {
3431
private final FieldSpec seen = FieldSpec.builder(BOOLEAN, "seen")
3532
.build();
3633

37-
3834
private final FieldSpec rest = FieldSpec.builder(LIST_OF_STRING, "rest")
3935
.initializer("new $T<>()", ArrayList.class)
4036
.build();
@@ -57,12 +53,6 @@ static CommonFields create(
5753
SourceElement sourceElement,
5854
List<Mapping<AnnotatedParameter>> positionalParameters,
5955
List<Mapping<AnnotatedOption>> namedOptions) {
60-
ParameterSpec result = ParameterSpec.builder(generatedTypes.parseResultType(), "result").build();
61-
CodeBlock.Builder code = CodeBlock.builder();
62-
code.add(CodeBlock.builder()
63-
.add("$N ->\n", result).indent()
64-
.add("$T.exit($N instanceof $T ? 0 : 1)", System.class, result, HelpRequested.class)
65-
.unindent().build());
6656
long mapSize = namedOptions.stream()
6757
.map(Mapping::sourceMethod)
6858
.map(AnnotatedOption::names)

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

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

33
import com.squareup.javapoet.CodeBlock;
44
import com.squareup.javapoet.MethodSpec;
5-
import com.squareup.javapoet.ParameterSpec;
65
import net.jbock.annotated.AnnotatedOption;
76
import net.jbock.annotated.AnnotatedParameter;
87
import net.jbock.annotated.AnnotatedParameters;
@@ -12,15 +11,13 @@
1211
import net.jbock.model.Option;
1312
import net.jbock.model.Parameter;
1413
import net.jbock.processor.SourceElement;
15-
import net.jbock.util.ParseRequest;
1614

1715
import javax.inject.Inject;
1816
import java.util.ArrayList;
1917
import java.util.List;
2018
import java.util.stream.Stream;
2119

2220
import static com.squareup.javapoet.MethodSpec.methodBuilder;
23-
import static javax.lang.model.element.Modifier.PRIVATE;
2421

2522
@ContextScope
2623
public class CreateModelMethod extends CachedMethod {
@@ -51,8 +48,7 @@ public class CreateModelMethod extends CachedMethod {
5148
@Override
5249
MethodSpec define() {
5350
List<CodeBlock> code = new ArrayList<>();
54-
ParameterSpec request = ParameterSpec.builder(ParseRequest.class, "request").build();
55-
code.add(CodeBlock.of("return $T.builder($N)", CommandModel.class, request));
51+
code.add(CodeBlock.of("return $T.builder()", CommandModel.class));
5652
sourceElement.descriptionKey().ifPresent(key ->
5753
code.add(CodeBlock.of(".withDescriptionKey($S)", key)));
5854
for (String descriptionLine : sourceElement.description()) {
@@ -75,8 +71,7 @@ MethodSpec define() {
7571
return methodBuilder("createModel")
7672
.addStatement(contextUtil.joinByNewline(code))
7773
.returns(CommandModel.class)
78-
.addModifiers(PRIVATE)
79-
.addParameter(request)
74+
.addModifiers(sourceElement.accessModifiers())
8075
.build();
8176
}
8277

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.squareup.javapoet.ParameterizedTypeName;
55
import com.squareup.javapoet.TypeName;
66
import net.jbock.processor.SourceElement;
7-
import net.jbock.util.NotSuccess;
7+
import net.jbock.util.ParsingFailed;
88
import net.jbock.util.SuperResult;
99

1010
import javax.inject.Inject;
@@ -65,7 +65,7 @@ ClassName implType() {
6565
TypeName parseResultType() {
6666
return ParameterizedTypeName.get(
6767
EITHER,
68-
ClassName.get(NotSuccess.class),
68+
ClassName.get(ParsingFailed.class),
6969
parseSuccessType());
7070
}
7171
}

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import net.jbock.util.AtFileError;
88
import net.jbock.util.AtFileReader;
99
import net.jbock.util.ExNotSuccess;
10-
import net.jbock.util.HelpRequested;
1110
import net.jbock.util.ParseRequest;
1211

1312
import javax.inject.Inject;
@@ -25,8 +24,6 @@ public class ParseMethod extends CachedMethod {
2524
private final BuildMethod buildMethod;
2625
private final CreateModelMethod createModelMethod;
2726

28-
private final ParameterSpec request = builder(ParseRequest.class, "request").build();
29-
3027
@Inject
3128
ParseMethod(
3229
GeneratedTypes generatedTypes,
@@ -42,19 +39,15 @@ public class ParseMethod extends CachedMethod {
4239
@Override
4340
MethodSpec define() {
4441

42+
ParameterSpec request = builder(ParseRequest.class, "request").build();
4543
ParameterSpec it = builder(STRING_ITERATOR, "it").build();
4644
ParameterSpec err = builder(AtFileError.class, "err").build();
4745

4846
CodeBlock.Builder code = CodeBlock.builder();
4947

50-
code.add("if ($1N.isHelpRequested())\n", request).indent()
51-
.addStatement("return $T.left(new $T($N($N)))", EITHER, HelpRequested.class,
52-
createModelMethod.get(), request)
53-
.unindent();
54-
5548
code.addStatement("return $L", CodeBlock.builder()
5649
.add("new $T().read($N)\n", AtFileReader.class, request).indent()
57-
.add(".mapLeft($1N -> $1N.addModel($2N($3N)))\n", err, createModelMethod.get(), request)
50+
.add(".mapLeft($1N -> $1N.addModel($2N()))\n", err, createModelMethod.get())
5851
.add(".map($T::iterator)\n", List.class)
5952
.add(".flatMap($N -> {\n", it)
6053
.indent().add(coreBlock(it)).unindent()
@@ -76,8 +69,8 @@ private CodeBlock coreBlock(ParameterSpec it) {
7669
.add("try {\n").indent()
7770
.add("return $T.right($N.parse($N).$N());\n", EITHER, state, it, buildMethod.get())
7871
.unindent().add("} catch ($T $N) {\n", ExNotSuccess.class, e).indent()
79-
.add("return $T.left($N.toError($N($N)));\n",
80-
EITHER, e, createModelMethod.get(), request)
72+
.add("return $T.left($N.toError($N()));\n",
73+
EITHER, e, createModelMethod.get())
8174
.unindent().add("}\n")
8275
.build();
8376
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import static com.squareup.javapoet.MethodSpec.methodBuilder;
1515
import static com.squareup.javapoet.ParameterSpec.builder;
16-
import static com.squareup.javapoet.TypeName.INT;
1716
import static net.jbock.common.Constants.STRING_ARRAY;
1817

1918
@ContextScope
@@ -23,39 +22,49 @@ public class ParseOrExitMethod {
2322
private final GeneratedTypes generatedTypes;
2423
private final ParseMethod parseMethod;
2524
private final List<Mapping<?>> allMappings;
25+
private final CreateModelMethod createModelMethod;
2626

2727
@Inject
2828
ParseOrExitMethod(
2929
SourceElement sourceElement,
3030
GeneratedTypes generatedTypes,
3131
ParseMethod parseMethod,
32-
List<Mapping<?>> allMappings) {
32+
List<Mapping<?>> allMappings,
33+
CreateModelMethod createModelMethod) {
3334
this.sourceElement = sourceElement;
3435
this.generatedTypes = generatedTypes;
3536
this.parseMethod = parseMethod;
3637
this.allMappings = allMappings;
38+
this.createModelMethod = createModelMethod;
3739
}
3840

3941
MethodSpec define() {
4042

4143
ParameterSpec args = builder(STRING_ARRAY, "args").build();
4244
ParameterSpec notSuccess = builder(generatedTypes.parseResultType(), "notSuccess").build();
43-
ParameterSpec returnCode = builder(INT, "code").build();
4445
ParameterSpec request = builder(ParseRequest.class, "request").build();
4546

4647
CodeBlock.Builder code = CodeBlock.builder();
47-
code.add("$1T $2N = $1T.standardBuilder($3N)\n", ParseRequest.class, request, args).indent();
4848
if (allMappings.stream().anyMatch(Mapping::isRequired)) {
49-
code.add(".withHelpRequested($1N.length == 0 || $2S.equals($1N[0]))\n", args, "--help");
49+
code.beginControlFlow("if ($1N.length == 0 || $2S.equals($1N[0]))", args, "--help")
50+
.addStatement("$T.builder().build().printHelp($N())",
51+
StandardErrorHandler.class, createModelMethod.get())
52+
.addStatement("$T.exit(0)", System.class)
53+
.endControlFlow();
5054
} else {
51-
code.add(".withHelpRequested($1N.length > 0 && $2S.equals($1N[0]))\n", args, "--help");
55+
code.beginControlFlow("if ($1N.length > 0 && $2S.equals($1N[0]))", args, "--help")
56+
.addStatement("$T.builder().build().printHelp($N())",
57+
StandardErrorHandler.class, createModelMethod.get())
58+
.addStatement("$T.exit(0)", System.class)
59+
.endControlFlow();
5260
}
61+
code.add("$1T $2N = $1T.standardBuilder($3N)\n", ParseRequest.class, request, args).indent();
5362
code.addStatement(".build()").unindent();
5463
code.add("return $N($N)", parseMethod.get(), request)
5564
.add(".orElseThrow($N -> {\n", notSuccess).indent()
56-
.addStatement("$T $N = $T.builder().build().handle($N)", INT, returnCode,
65+
.addStatement("$T.builder().build().handle($N)",
5766
StandardErrorHandler.class, notSuccess)
58-
.addStatement("$T.exit($N)", System.class, returnCode)
67+
.addStatement("$T.exit(1)", System.class)
5968
.addStatement("return new $T()", RuntimeException.class).unindent()
6069
.addStatement("})");
6170
return methodBuilder("parseOrExit").addParameter(args)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private CodeBlock regularCode() {
103103
.addStatement("$T $N = $N.next()", STRING, token, it);
104104

105105
code.beginControlFlow("if (!$N && $S.equals($N))",
106-
endOfOptionParsing, "--", token)
106+
endOfOptionParsing, "--", token)
107107
.addStatement("$N = $L", endOfOptionParsing, true)
108108
.addStatement("continue")
109109
.endControlFlow();
@@ -113,7 +113,7 @@ private CodeBlock regularCode() {
113113
}
114114

115115
code.add("if (!$N && $N.matcher($N).matches())\n",
116-
endOfOptionParsing, commonFields.suspiciousPattern(), token).indent()
116+
endOfOptionParsing, commonFields.suspiciousPattern(), token).indent()
117117
.addStatement(throwInvalidOptionStatement(ErrTokenType.INVALID_OPTION))
118118
.unindent();
119119

@@ -180,7 +180,7 @@ private CodeBlock.Builder initVariables() {
180180

181181
private CodeBlock checkSuspicious() {
182182
return CodeBlock.builder().add("if ($N.matcher($N).matches())\n",
183-
commonFields.suspiciousPattern(), token).indent()
183+
commonFields.suspiciousPattern(), token).indent()
184184
.addStatement(throwInvalidOptionStatement(ErrTokenType.INVALID_OPTION))
185185
.unindent().build();
186186
}

compiler/src/main/java/net/jbock/validate/CommandProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ private Optional<List<ValidationFailure>> checkDuplicateDescriptionKeys(
6060
Set<String> keys = new HashSet<>();
6161
sourceElement.descriptionKey().ifPresent(keys::add);
6262
Stream.of(methods.namedOptions(),
63-
methods.positionalParameters(),
64-
methods.repeatablePositionalParameters())
63+
methods.positionalParameters(),
64+
methods.repeatablePositionalParameters())
6565
.flatMap(List::stream)
6666
.forEach(m -> m.descriptionKey().ifPresent(key -> {
6767
if (!keys.add(key)) {

0 commit comments

Comments
 (0)