Skip to content

Commit 4f6b287

Browse files
committed
refactoring
1 parent cdde1ea commit 4f6b287

8 files changed

Lines changed: 82 additions & 73 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ MethodSpec define() {
5858
.addStatement("$T.exit(0)", System.class)
5959
.endControlFlow();
6060
}
61-
code.add("$1T $2N = $1T.standardBuilder($3N)\n", ParseRequest.class, request, args).indent();
62-
code.addStatement(".build()").unindent();
61+
code.addStatement("$1T $2N = $1T.maybeExpand($3N)", ParseRequest.class, request, args);
6362
code.add("return $N($N)", parseMethod.get(), request)
6463
.add(".orElseThrow($N -> {\n", notSuccess).indent()
6564
.addStatement("$T.builder().build().handle($N)",

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

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,17 @@
88
/**
99
* Input for the generated parser.
1010
*/
11-
public final class ParseRequest {
12-
13-
private final Optional<Path> path; // if empty, no @-file expansion will be performed
14-
private final List<String> args; // command line arguments, excluding path
15-
16-
private ParseRequest(Optional<Path> path, List<String> args) {
17-
this.path = path;
18-
this.args = args;
19-
}
11+
public abstract class ParseRequest {
2012

2113
/**
22-
* A builder for {@link ParseRequest}.
23-
*/
24-
public static final class Builder {
25-
26-
private final Optional<Path> path;
27-
private final List<String> rest;
28-
29-
private Builder(Optional<Path> path, List<String> rest) {
30-
this.path = path;
31-
this.rest = rest;
32-
}
33-
34-
/**
35-
* Creates the parse request.
36-
*
37-
* @return a parse request
38-
*/
39-
public ParseRequest build() {
40-
return new ParseRequest(path, rest);
41-
}
42-
}
43-
44-
/**
45-
* Creates a builder.
46-
*
47-
* <p>{@code @file} expansion will be enabled if the input is not empty,
14+
* Creates a {@code ParseRequest}.
15+
* {@code @-file} expansion will be enabled if the input array is nonempty,
4816
* and the first token starts with an {@code "@"} character.
4917
*
5018
* @param args command line input
51-
* @return the options in the file, or an error report
19+
* @return a parse request
5220
*/
53-
public static Builder standardBuilder(String[] args) {
21+
public static ParseRequest maybeExpand(String[] args) {
5422
if (args.length >= 1
5523
&& args[0].length() >= 2
5624
&& args[0].startsWith("@")) {
@@ -62,44 +30,41 @@ public static Builder standardBuilder(String[] args) {
6230
}
6331

6432
/**
65-
* Creates a builder with {@code @file} expansion set to {@code false}.
33+
* Creates a {@code ParseRequest} that indicates {@code @-file} expansion should
34+
* not be performed.
6635
*
6736
* @param args command line input
68-
* @return a builder
37+
* @return a parse request
6938
*/
70-
public static Builder simple(List<String> args) {
71-
return new Builder(Optional.empty(), args);
39+
public static ParseRequest simple(List<String> args) {
40+
return new ParseRequestSimple(args);
7241
}
7342

7443
/**
75-
* Creates a builder with {@code @file} expansion set to {@code true}.
44+
* Creates a {@code ParseRequest} that indicates {@code @-file} expansion should
45+
* be performed.
7646
*
77-
* @param atFile source for {@code @file} expansion
78-
* @param rest the remaining command line arguments, possibly none
79-
* @return a builder
47+
* @param atFile the {@code @-file}
48+
* @param rest additional command line arguments
49+
* @return a parse request
8050
*/
81-
public static Builder expand(Path atFile, List<String> rest) {
82-
return new Builder(Optional.of(atFile), rest);
51+
public static ParseRequest expand(Path atFile, List<String> rest) {
52+
return new ParseRequestExpand(atFile, rest);
8353
}
8454

8555
/**
86-
* Returns the {@code path} to be used as input for {@code @file} expansion.
87-
* If it is empty, {@code @file} expansion should not be performed.
56+
* Returns the path of the {@code @-file}, or an empty
57+
* {@code Optional} if {@code @-file} expansion is
58+
* not requested.
8859
*
89-
* @return input for {@code @file} expansion
60+
* @return path of the {@code @-file}, or {@code empty}
9061
*/
91-
public Optional<Path> path() {
92-
return path;
93-
}
62+
public abstract Optional<Path> path();
9463

9564
/**
96-
* If {@link #path} is empty, contains the command line arguments.
97-
* If {@link #path} is nonempty, contains any remaining argument after
98-
* the {@code @file} parameter.
65+
* Returns the command line arguments, excluding the {@code @-file}.
9966
*
10067
* @return command line arguments
10168
*/
102-
public List<String> args() {
103-
return args;
104-
}
69+
public abstract List<String> args();
10570
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package net.jbock.util;
2+
3+
import java.nio.file.Path;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
final class ParseRequestExpand extends ParseRequest {
8+
9+
private final Path path;
10+
private final List<String> args;
11+
12+
ParseRequestExpand(Path path, List<String> args) {
13+
this.path = path;
14+
this.args = args;
15+
}
16+
17+
@Override
18+
public Optional<Path> path() {
19+
return Optional.of(path);
20+
}
21+
22+
@Override
23+
public List<String> args() {
24+
return args;
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package net.jbock.util;
2+
3+
import java.nio.file.Path;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
final class ParseRequestSimple extends ParseRequest {
8+
9+
private final List<String> args;
10+
11+
ParseRequestSimple(List<String> args) {
12+
this.args = args;
13+
}
14+
15+
@Override
16+
public Optional<Path> path() {
17+
return Optional.empty();
18+
}
19+
20+
@Override
21+
public List<String> args() {
22+
return args;
23+
}
24+
}

examples/src/test/java/net/jbock/examples/CpArgumentsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void testAtFileSyntax() throws IOException {
119119
Path path = foo.resolve("hello.txt");
120120
Files.write(path, List.of("-r", "\"a\"", "\"'b'\"", "--backup=\\", "'SIMPLE'", ""), StandardCharsets.UTF_8);
121121
Either<ParsingFailed, CpArguments> result = parser.parse(
122-
ParseRequest.expand(path, List.of()).build());
122+
ParseRequest.expand(path, List.of()));
123123
f.assertThat(result)
124124
.has(CpArguments::source, "a")
125125
.has(CpArguments::dest, "'b'")

examples/src/test/java/net/jbock/examples/HelplessArgumentsTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class HelplessArgumentsTest {
1818
@Test
1919
void testHelpIsAcceptedAsNormalOption() {
2020
Either<ParsingFailed, HelplessArguments> result =
21-
parser.parse(ParseRequest.simple(List.of("--help", "x"))
22-
.build());
21+
parser.parse(ParseRequest.simple(List.of("--help", "x")));
2322
f.assertThat(result)
2423
.has(HelplessArguments::required, "x")
2524
.has(HelplessArguments::help, true);
@@ -28,17 +27,15 @@ void testHelpIsAcceptedAsNormalOption() {
2827
@Test
2928
void errorNoArguments() {
3029
Either<ParsingFailed, HelplessArguments> result =
31-
parser.parse(ParseRequest.simple(List.of(/* empty */))
32-
.build());
30+
parser.parse(ParseRequest.simple(List.of(/* empty */)));
3331
f.assertThat(result)
3432
.fails("Missing required parameter REQUIRED");
3533
}
3634

3735
@Test
3836
void errorHelpDisabled() {
3937
Either<ParsingFailed, HelplessArguments> result =
40-
parser.parse(ParseRequest.simple(List.of("--help"))
41-
.build());
38+
parser.parse(ParseRequest.simple(List.of("--help")));
4239
f.assertThat(result)
4340
.fails("Missing required parameter REQUIRED");
4441
}

examples/src/test/java/net/jbock/examples/HelplessSuperArgumentsTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class HelplessSuperArgumentsTest {
1919
@Test
2020
void testHelp() {
2121
Either<ParsingFailed, SuperResult<HelplessSuperArguments>> result =
22-
parser.parse(ParseRequest.simple(List.of("--help"))
23-
.build());
22+
parser.parse(ParseRequest.simple(List.of("--help")));
2423
f.assertThat(result).fails("Invalid option: --help");
2524
}
2625
}

examples/src/test/java/net/jbock/examples/fixture/ParserTestFixture.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static <E> ParserTestFixture<E> create(Function<ParseRequest, Either<Pars
4545
}
4646

4747
public AssertionBuilder<E> assertThat(String... args) {
48-
ParseRequest request = ParseRequest.simple(List.of(args)).build();
48+
ParseRequest request = ParseRequest.simple(List.of(args));
4949
Either<ParsingFailed, E> instance = parser.apply(request);
5050
return new AssertionBuilder<>(instance);
5151
}
@@ -69,8 +69,7 @@ public void assertPrintsHelp(
6969
}
7070

7171
public E parse(String... args) {
72-
ParseRequest request = ParseRequest.simple(List.of(args))
73-
.build();
72+
ParseRequest request = ParseRequest.simple(List.of(args));
7473
return parser.apply(request)
7574
.orElseThrow(l -> new RuntimeException("expecting success but found " + l.getClass()));
7675
}

0 commit comments

Comments
 (0)