|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Splatgames.de Software and Contributors |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package de.splatgames.aether.datafixers.cli; |
| 24 | + |
| 25 | +import de.splatgames.aether.datafixers.cli.command.InfoCommand; |
| 26 | +import de.splatgames.aether.datafixers.cli.command.MigrateCommand; |
| 27 | +import de.splatgames.aether.datafixers.cli.command.ValidateCommand; |
| 28 | +import picocli.CommandLine; |
| 29 | +import picocli.CommandLine.Command; |
| 30 | +import picocli.CommandLine.HelpCommand; |
| 31 | + |
| 32 | +import java.util.concurrent.Callable; |
| 33 | + |
| 34 | +/** |
| 35 | + * Main entry point for the Aether Datafixers CLI. |
| 36 | + * |
| 37 | + * <p>The CLI provides commands for migrating, validating, and inspecting |
| 38 | + * data files using Aether Datafixers.</p> |
| 39 | + * |
| 40 | + * <h2>Usage Examples</h2> |
| 41 | + * <pre>{@code |
| 42 | + * # Migrate a single file |
| 43 | + * aether-cli migrate --from 100 --to 200 --type player --bootstrap com.example.MyBootstrap input.json |
| 44 | + * |
| 45 | + * # Migrate with auto-detected version |
| 46 | + * aether-cli migrate --to 200 --type player --version-field dataVersion --bootstrap com.example.MyBootstrap input.json |
| 47 | + * |
| 48 | + * # Validate files |
| 49 | + * aether-cli validate --to 200 --type player --bootstrap com.example.MyBootstrap *.json |
| 50 | + * |
| 51 | + * # Show info |
| 52 | + * aether-cli info --formats |
| 53 | + * aether-cli info --bootstrap com.example.MyBootstrap |
| 54 | + * }</pre> |
| 55 | + * |
| 56 | + * @author Erik Pfoertner |
| 57 | + * @since 0.3.0 |
| 58 | + */ |
| 59 | +@Command( |
| 60 | + name = "aether-cli", |
| 61 | + mixinStandardHelpOptions = true, |
| 62 | + version = "Aether Datafixers CLI 0.3.0", |
| 63 | + description = "Command-line data migration tool using Aether Datafixers.", |
| 64 | + subcommands = { |
| 65 | + MigrateCommand.class, |
| 66 | + ValidateCommand.class, |
| 67 | + InfoCommand.class, |
| 68 | + HelpCommand.class |
| 69 | + }, |
| 70 | + synopsisHeading = "%nUsage:%n%n", |
| 71 | + descriptionHeading = "%nDescription:%n%n", |
| 72 | + parameterListHeading = "%nParameters:%n", |
| 73 | + optionListHeading = "%nOptions:%n", |
| 74 | + commandListHeading = "%nCommands:%n" |
| 75 | +) |
| 76 | +public class AetherCli implements Callable<Integer> { |
| 77 | + |
| 78 | + /** |
| 79 | + * Main entry point for the Aether Datafixers CLI application. |
| 80 | + * |
| 81 | + * <p>This method initializes the picocli {@link CommandLine} parser with the |
| 82 | + * root {@link AetherCli} command and executes it with the provided arguments. |
| 83 | + * The process exit code is set based on the command execution result.</p> |
| 84 | + * |
| 85 | + * <h3>Exit Codes</h3> |
| 86 | + * <ul> |
| 87 | + * <li>{@code 0} - Success (or help displayed)</li> |
| 88 | + * <li>{@code 1} - Error occurred during command execution</li> |
| 89 | + * <li>{@code 2} - Validation found files needing migration (validate command only)</li> |
| 90 | + * </ul> |
| 91 | + * |
| 92 | + * <h3>Configuration</h3> |
| 93 | + * <p>The CommandLine instance is configured with:</p> |
| 94 | + * <ul> |
| 95 | + * <li>Case-insensitive enum value parsing enabled</li> |
| 96 | + * </ul> |
| 97 | + * |
| 98 | + * @param args command-line arguments passed from the shell; may be empty |
| 99 | + * but should not be {@code null} |
| 100 | + * @see CommandLine#execute(String...) |
| 101 | + */ |
| 102 | + public static void main(final String[] args) { |
| 103 | + final int exitCode = new CommandLine(new AetherCli()) |
| 104 | + .setCaseInsensitiveEnumValuesAllowed(true) |
| 105 | + .execute(args); |
| 106 | + System.exit(exitCode); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Executes the root command when invoked without a subcommand. |
| 111 | + * |
| 112 | + * <p>When the CLI is invoked without specifying a subcommand (e.g., just |
| 113 | + * {@code aether-cli}), this method is called. It displays the help message |
| 114 | + * showing available commands and options.</p> |
| 115 | + * |
| 116 | + * <p>This behavior provides a user-friendly experience where running the |
| 117 | + * CLI without arguments shows how to use it, rather than doing nothing |
| 118 | + * or showing an error.</p> |
| 119 | + * |
| 120 | + * @return {@code 0} indicating successful execution (help was displayed) |
| 121 | + * @see CommandLine#usage(Object, java.io.PrintStream) |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public Integer call() { |
| 125 | + // Print help when no subcommand is specified |
| 126 | + CommandLine.usage(this, System.out); |
| 127 | + return 0; |
| 128 | + } |
| 129 | +} |
0 commit comments