|
15 | 15 | */ |
16 | 16 | package org.contextmapper.cli; |
17 | 17 |
|
18 | | -import org.contextmapper.cli.commands.CliCommand; |
19 | 18 | import org.contextmapper.cli.commands.GenerateCommand; |
20 | 19 | import org.contextmapper.cli.commands.ValidateCommand; |
| 20 | +import picocli.CommandLine; |
| 21 | +import picocli.CommandLine.Command; |
21 | 22 |
|
22 | | -import java.util.Arrays; |
23 | | -import java.util.Collections; |
24 | | -import java.util.List; |
25 | | - |
26 | | -public class ContextMapperCLI { |
| 23 | +@Command(name = "cm", mixinStandardHelpOptions = true, versionProvider = VersionProvider.class, |
| 24 | + description = "Context Mapper CLI", |
| 25 | + subcommands = { |
| 26 | + ValidateCommand.class, |
| 27 | + GenerateCommand.class |
| 28 | + }) |
| 29 | +public class ContextMapperCLI implements Runnable { |
27 | 30 |
|
28 | 31 | private static final int REQUIRED_JAVA_VERSION = 11; |
29 | | - private static final String VALIDATE_COMMAND = "validate"; |
30 | | - private static final String GENERATE_COMMAND = "generate"; |
31 | | - |
32 | | - private CliCommand generateCommand; |
33 | | - private CliCommand validateCommand; |
34 | | - |
35 | | - public ContextMapperCLI() { |
36 | | - this.generateCommand = new GenerateCommand(); |
37 | | - this.validateCommand = new ValidateCommand(); |
38 | | - } |
39 | 32 |
|
40 | 33 | public static void main(String[] args) { |
41 | | - int javaVersion = Runtime.version().feature(); |
42 | | - |
43 | | - if (Runtime.version().feature() >= REQUIRED_JAVA_VERSION) { |
44 | | - new ContextMapperCLI().run(args); |
45 | | - } else { |
46 | | - System.out.printf("Invalid Java version '%s' (>=%s is required).", javaVersion, REQUIRED_JAVA_VERSION); |
| 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); |
47 | 36 | System.exit(1); |
48 | 37 | } |
| 38 | + int exitCode = new CommandLine(new ContextMapperCLI()).execute(args); |
| 39 | + System.exit(exitCode); |
49 | 40 | } |
50 | 41 |
|
51 | | - protected void run(String[] args) { |
52 | | - System.out.println("Context Mapper CLI " + getVersion()); |
53 | | - |
54 | | - if (args == null || args.length == 0) { |
55 | | - printUsages(); |
56 | | - } else if (VALIDATE_COMMAND.equalsIgnoreCase(args[0])) { |
57 | | - validateCommand.run(Arrays.copyOfRange(args, 1, args.length)); |
58 | | - } else if (GENERATE_COMMAND.equalsIgnoreCase(args[0])) { |
59 | | - generateCommand.run(Arrays.copyOfRange(args, 1, args.length)); |
60 | | - } else { |
61 | | - System.out.println("Invalid input"); |
62 | | - System.exit(127); |
63 | | - } |
| 42 | + @Override |
| 43 | + 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 | + System.out.println("Context Mapper CLI. Use 'cm --help' for usage information."); |
64 | 48 | } |
65 | 49 |
|
66 | | - private void printUsages() { |
67 | | - System.out.println("Usage: cm " + VALIDATE_COMMAND + "|" + GENERATE_COMMAND + " [options]"); |
68 | | - } |
| 50 | +} |
69 | 51 |
|
70 | | - private String getVersion() { |
| 52 | +class VersionProvider implements CommandLine.IVersionProvider { |
| 53 | + @Override |
| 54 | + public String[] getVersion() throws Exception { |
71 | 55 | String implVersion = ContextMapperCLI.class.getPackage().getImplementationVersion(); |
72 | | - return implVersion != null ? "v" + implVersion : "DEVELOPMENT VERSION"; |
| 56 | + return new String[]{"Context Mapper CLI " + (implVersion != null ? "v" + implVersion : "DEVELOPMENT VERSION")}; |
73 | 57 | } |
74 | | - |
75 | 58 | } |
0 commit comments