Skip to content

Commit e355c37

Browse files
♻️ update CLI syntax & files layout
1 parent 1bff470 commit e355c37

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed

src/main/java/com/mindee/CommandLineInterface.java

Lines changed: 101 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import com.mindee.parsing.common.Inference;
1212
import com.mindee.parsing.common.PredictResponse;
1313
import com.mindee.product.custom.CustomV1;
14+
import com.mindee.product.generated.GeneratedV1;
1415
import java.io.File;
1516
import java.io.IOException;
1617
import java.util.ArrayList;
1718
import java.util.List;
19+
import java.util.Objects;
1820
import picocli.CommandLine;
1921
import picocli.CommandLine.Command;
2022
import picocli.CommandLine.Model.CommandSpec;
@@ -28,12 +30,15 @@
2830
*/
2931
@Command(
3032
name = "CLI",
31-
scope = ScopeType.INHERIT,
33+
description = "Invoke Off The Shelf API for invoice, receipt, and passports",
3234
subcommands = {CommandLine.HelpCommand.class},
33-
description = "Invoke Off The Shelf API for invoice, receipt, and passports"
35+
mixinStandardHelpOptions = true
3436
)
3537
public class CommandLineInterface implements ProductProcessor {
3638

39+
@CommandLine.Mixin
40+
private final CommandLineInterfaceProducts productCommands = new CommandLineInterfaceProducts(this);
41+
3742
@Spec
3843
CommandSpec spec;
3944

@@ -86,21 +91,12 @@ private enum OutputChoices { summary, full, raw }
8691
public static void main(String[] args) {
8792
CommandLineInterface cli = new CommandLineInterface();
8893
CommandLine commandLine = new CommandLine(cli);
89-
cli.setupCommands(commandLine);
9094

91-
int exitCode = new CommandLine(new CommandLineInterface()).execute(args);
95+
int exitCode = commandLine.execute(args);
9296
System.exit(exitCode);
9397
}
9498

95-
/**
96-
* @param commandLine Product to add to the CLI.
97-
*/
98-
public void setupCommands(CommandLine commandLine) {
99-
CommandLineInterfaceProducts productCommands = new CommandLineInterfaceProducts(this);
100-
commandLine.addSubcommand(productCommands);
101-
}
102-
103-
@Command(name = "custom", description = "Invokes a Custom API")
99+
@Command(name = "custom", description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)")
104100
void customMethod(
105101
@Option(
106102
names = {"-a", "--account"},
@@ -111,11 +107,11 @@ void customMethod(
111107
)
112108
String accountName,
113109
@Option(
114-
names = {"-e", "--endpointName"},
115-
scope = ScopeType.LOCAL,
116-
required = true,
117-
paramLabel = "endpointName",
118-
description = "The name of the endpoint"
110+
names = {"-e", "--endpointName"},
111+
scope = ScopeType.LOCAL,
112+
required = true,
113+
paramLabel = "endpointName",
114+
description = "The name of the endpoint"
119115
)
120116
String endpointName,
121117
@Parameters(index = "0", scope = ScopeType.LOCAL, paramLabel = "<path>")
@@ -129,17 +125,99 @@ void customMethod(
129125

130126
if (cutDoc) {
131127
document = mindeeClient.parse(
132-
new LocalInputSource(file),
133-
endpoint,
134-
getDefaultPageOptions());
128+
new LocalInputSource(file),
129+
endpoint,
130+
getDefaultPageOptions());
135131
} else {
136132
document = mindeeClient.parse(
137-
new LocalInputSource(file),
138-
endpoint);
133+
new LocalInputSource(file),
134+
endpoint);
139135
}
140136
System.out.println(document.toString());
141137
}
142138

139+
@Command(name = "generated", description = "Invokes a Generated API")
140+
void generatedMethod(
141+
@Option(
142+
names = {"-a", "--account"},
143+
scope = ScopeType.LOCAL,
144+
required = true,
145+
paramLabel = "accountName",
146+
description = "The name of the account"
147+
)
148+
String accountName,
149+
@Option(
150+
names = {"-e", "--endpointName"},
151+
scope = ScopeType.LOCAL,
152+
required = true,
153+
paramLabel = "endpointName",
154+
description = "The name of the endpoint"
155+
)
156+
String endpointName,
157+
@Option(
158+
names = {"-v", "--productVersion"},
159+
scope = ScopeType.LOCAL,
160+
paramLabel = "productVersion",
161+
description = "The version of the endpoint",
162+
defaultValue = "1"
163+
)
164+
String productVersion,
165+
@Option(
166+
names = {"-m", "--parsingMode"},
167+
description = "Whether to parse the document in synchronous mode or polling.",
168+
scope = ScopeType.LOCAL,
169+
defaultValue = "async"
170+
)
171+
String parsingMode,
172+
@Parameters(index = "0", scope = ScopeType.LOCAL, paramLabel = "<path>")
173+
File file
174+
) throws IOException, InterruptedException {
175+
176+
MindeeClient mindeeClient = new MindeeClient(apiKey);
177+
178+
Endpoint endpoint = new Endpoint(endpointName, accountName, productVersion);
179+
180+
if (Objects.equals(parsingMode, "sync")) {
181+
if (cutDoc) {
182+
PredictResponse<GeneratedV1> document = mindeeClient.parse(
183+
GeneratedV1.class,
184+
endpoint,
185+
new LocalInputSource(file),
186+
getDefaultPageOptions()
187+
);
188+
System.out.println(document.toString());
189+
} else {
190+
PredictResponse<GeneratedV1> document = mindeeClient.parse(
191+
GeneratedV1.class,
192+
endpoint,
193+
new LocalInputSource(file)
194+
);
195+
System.out.println(document.toString());
196+
}
197+
} else if (Objects.equals(parsingMode, "async")) {
198+
if (cutDoc) {
199+
AsyncPredictResponse<GeneratedV1> document = mindeeClient.enqueueAndParse(
200+
GeneratedV1.class,
201+
endpoint,
202+
new LocalInputSource(file),
203+
PredictOptions.builder().build(),
204+
getDefaultPageOptions(),
205+
AsyncPollingOptions.builder().build()
206+
);
207+
System.out.println(document.toString());
208+
} else {
209+
AsyncPredictResponse<GeneratedV1> document = mindeeClient.enqueueAndParse(
210+
GeneratedV1.class,
211+
endpoint,
212+
new LocalInputSource(file)
213+
);
214+
System.out.println(document.toString());
215+
}
216+
} else {
217+
throw new MindeeException("Invalid parsing mode: " + parsingMode);
218+
}
219+
}
220+
143221
protected PageOptions getDefaultPageOptions() {
144222

145223
List<Integer> pageNumbers = new ArrayList<>();

src/main/java/com/mindee/cli/CommandLineInterfaceProducts.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818

1919
@CommandLine.Command(
2020
name = "CLI",
21-
scope = CommandLine.ScopeType.INHERIT,
22-
subcommands = {CommandLine.HelpCommand.class},
23-
description = "Invoke Off The Shelf API for invoice, receipt, and passports"
21+
scope = CommandLine.ScopeType.INHERIT
2422
)
2523
public class CommandLineInterfaceProducts {
2624
private final ProductProcessor processor;

0 commit comments

Comments
 (0)