|
| 1 | +package com.mapbox.services.cli |
| 2 | + |
| 3 | +import com.google.gson.Gson |
| 4 | +import com.mapbox.services.cli.validator.DirectionsResponseValidator |
| 5 | +import org.apache.commons.cli.CommandLine |
| 6 | +import org.apache.commons.cli.DefaultParser |
| 7 | +import org.apache.commons.cli.HelpFormatter |
| 8 | +import org.apache.commons.cli.Option |
| 9 | +import org.apache.commons.cli.Options |
| 10 | +import org.apache.commons.cli.ParseException |
| 11 | + |
| 12 | +/** |
| 13 | + * Entry point for the command line interface. |
| 14 | + */ |
| 15 | +object MapboxJavaCli { |
| 16 | + |
| 17 | + private const val COMMAND_FILE_INPUT = "f" |
| 18 | + private const val COMMAND_HELP = "h" |
| 19 | + |
| 20 | + @JvmStatic |
| 21 | + fun main(args: Array<String>) { |
| 22 | + val options = Options() |
| 23 | + .addOption(Option.builder(COMMAND_HELP) |
| 24 | + .longOpt("help") |
| 25 | + .desc("Shows this help message") |
| 26 | + .build()) |
| 27 | + .addOption(Option.builder(COMMAND_FILE_INPUT) |
| 28 | + .longOpt("file") |
| 29 | + .hasArg(true) |
| 30 | + .desc("Path to a json file or directory") |
| 31 | + .required() |
| 32 | + .build()) |
| 33 | + |
| 34 | + try { |
| 35 | + val commandLine = DefaultParser().parse(options, args) |
| 36 | + parseCommands(commandLine, options) |
| 37 | + } catch (pe: ParseException) { |
| 38 | + println(pe.message) |
| 39 | + printHelp(options) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private fun parseCommands(commandLine: CommandLine, options: Options) { |
| 44 | + if (commandLine.hasOption(COMMAND_HELP)) { |
| 45 | + printHelp(options) |
| 46 | + } |
| 47 | + |
| 48 | + val fileInput = commandLine.getOptionValue(COMMAND_FILE_INPUT) |
| 49 | + val directionsResponseValidator = DirectionsResponseValidator() |
| 50 | + val results = directionsResponseValidator.parse(fileInput) |
| 51 | + print(Gson().toJson(results)) |
| 52 | + } |
| 53 | + |
| 54 | + private fun printHelp(options: Options) { |
| 55 | + val syntax = "java -jar services-cli/build/libs/services-cli-all.jar" |
| 56 | + HelpFormatter().printHelp(syntax, options) |
| 57 | + } |
| 58 | +} |
0 commit comments