From b64707148cfa4061b98913e4f09c0e51bac0db74 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 24 Sep 2024 08:26:12 +0200 Subject: [PATCH] Refactor CLI parsing --- .../gtfs_transformer/GtfsTransformerMain.java | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/onebusaway-gtfs-transformer-cli/src/main/java/org/onebusaway/gtfs_transformer/GtfsTransformerMain.java b/onebusaway-gtfs-transformer-cli/src/main/java/org/onebusaway/gtfs_transformer/GtfsTransformerMain.java index 0ea998428..be41ff048 100644 --- a/onebusaway-gtfs-transformer-cli/src/main/java/org/onebusaway/gtfs_transformer/GtfsTransformerMain.java +++ b/onebusaway-gtfs-transformer-cli/src/main/java/org/onebusaway/gtfs_transformer/GtfsTransformerMain.java @@ -20,8 +20,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.PrintWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -45,7 +45,7 @@ public class GtfsTransformerMain { - private static Logger _log = LoggerFactory.getLogger(GtfsTransformerMain.class); + private static final Logger LOG = LoggerFactory.getLogger(GtfsTransformerMain.class); /**** * Generic Arguments @@ -177,11 +177,11 @@ protected void buildOptions(Options options) { "overwrite duplicate elements"); } - protected void printHelp(PrintWriter out, Options options) throws IOException { + private void printHelp() throws IOException { InputStream is = getClass().getResourceAsStream("usage.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); - String line = null; + String line; while ((line = reader.readLine()) != null) { System.err.println(line); @@ -193,22 +193,23 @@ protected void printHelp(PrintWriter out, Options options) throws IOException { protected void runApplication(CommandLine cli, String[] originalArgs) throws Exception { - String[] args = cli.getArgs(); + var args = Arrays.stream(cli.getArgs()).toList(); - if (args.length < 2) { + if (args.size() < 2) { printHelp(); System.exit(-1); } - List paths = new ArrayList(); - for (int i = 0; i < args.length - 1; ++i) { - paths.add(new File(args[i])); - _log.info("input path: " + args[i]); - } + List inputPaths = args.stream().limit(args.size() - 1).map(File::new + ).toList(); + LOG.info("input paths: {}", inputPaths); + GtfsTransformer transformer = new GtfsTransformer(); - transformer.setGtfsInputDirectories(paths); - transformer.setOutputDirectory(new File(args[args.length - 1])); - _log.info("output path: " + args[args.length - 1]); + transformer.setGtfsInputDirectories(inputPaths); + + var outputPath = new File(args.getLast()); + transformer.setOutputDirectory(outputPath); + LOG.info("output path: {}", outputPath); Option[] options = getOptionsInCommandLineOrder(cli, originalArgs); @@ -371,14 +372,6 @@ private void configureVerifyRoutesFile(GtfsTransformer updater, String file) { updater.addParameter("verifyRoutesFile", file); } - /***************************************************************************** - * Protected Methods - ****************************************************************************/ - - protected void printHelp() throws IOException { - printHelp(new PrintWriter(System.err, true), _options); - } - private boolean needsHelp(String[] args) { for (String arg : args) { if (arg.equals("-h") || arg.equals("--help") || arg.equals("-help"))