|
1 | | -/* |
2 | | - * Copyright 2021 The Context Mapper Project Team |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | 1 | package org.contextmapper.cli; |
17 | 2 |
|
18 | 3 | import org.contextmapper.cli.commands.GenerateCommand; |
19 | 4 | import org.contextmapper.cli.commands.ValidateCommand; |
20 | 5 | import picocli.CommandLine; |
21 | 6 | import picocli.CommandLine.Command; |
| 7 | +import java.util.function.Supplier; |
22 | 8 |
|
23 | | -@Command(name = "cm", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class, |
24 | | - description = "Context Mapper CLI", |
25 | | - subcommands = { |
26 | | - ValidateCommand.class, |
27 | | - GenerateCommand.class |
28 | | - }) |
| 9 | +@Command( |
| 10 | + name = "cm", |
| 11 | + versionProvider = VersionProvider.class, |
| 12 | + description = "Context Mapper CLI", |
| 13 | + subcommands = { |
| 14 | + ValidateCommand.class, |
| 15 | + GenerateCommand.class |
| 16 | + }, |
| 17 | + mixinStandardHelpOptions = true, |
| 18 | + usageHelpAutoWidth = true) |
29 | 19 | public class ContextMapperCLI implements Runnable { |
30 | 20 |
|
31 | 21 | private static final int REQUIRED_JAVA_VERSION = 11; |
32 | | - |
33 | | - public static void main(String[] args) { |
34 | | - if (Runtime.version().feature() < REQUIRED_JAVA_VERSION) { |
35 | | - System.err.printf("Invalid Java version '%s' (>=%s is required).%n", Runtime.version().feature(), REQUIRED_JAVA_VERSION); |
36 | | - System.exit(1); |
37 | | - } |
38 | | - int exitCode = new CommandLine(new ContextMapperCLI()).execute(args); |
39 | | - System.exit(exitCode); |
40 | | - } |
| 22 | + static Supplier<Integer> javaVersionSupplier = () -> Runtime.version().feature(); |
41 | 23 |
|
42 | 24 | @Override |
43 | 25 | public void run() { |
44 | | - // This is executed if no subcommand is specified. |
45 | | - // Picocli will show the help message by default if mixinStandardHelpOptions = true and no subcommand is given. |
46 | | - // We can add a custom message here if needed, or rely on Picocli's default behavior. |
47 | 26 | System.out.println("Context Mapper CLI. Use 'cm --help' for usage information."); |
48 | 27 | } |
49 | 28 |
|
50 | | -} |
| 29 | + static void checkJavaVersion(Runnable exiter) { |
| 30 | + int currentVersion = javaVersionSupplier.get(); |
| 31 | + if (currentVersion < REQUIRED_JAVA_VERSION) { |
| 32 | + System.err.printf("Invalid Java version '%s' (>=%s is required).%n", currentVersion, REQUIRED_JAVA_VERSION); |
| 33 | + exiter.run(); |
| 34 | + } |
| 35 | + } |
51 | 36 |
|
52 | | -class VersionProvider implements CommandLine.IVersionProvider { |
53 | | - @Override |
54 | | - public String[] getVersion() throws Exception { |
55 | | - String implVersion = ContextMapperCLI.class.getPackage().getImplementationVersion(); |
56 | | - return new String[]{"Context Mapper CLI " + (implVersion != null ? "v" + implVersion : "DEVELOPMENT VERSION")}; |
| 37 | + private static void checkJavaVersion() { |
| 38 | + checkJavaVersion(() -> System.exit(1)); |
| 39 | + } |
| 40 | + |
| 41 | + static int runCLI(String[] args) { |
| 42 | + return new CommandLine(new ContextMapperCLI()).execute(args); |
| 43 | + } |
| 44 | + |
| 45 | + public static void main(String[] args) { |
| 46 | + checkJavaVersion(); |
| 47 | + int exitCode = runCLI(args); |
| 48 | + System.exit(exitCode); |
57 | 49 | } |
58 | 50 | } |
0 commit comments