From 78db0e91799fea0dd72d315b6d2b0bbe9c42e374 Mon Sep 17 00:00:00 2001 From: Chuckame Date: Mon, 2 Nov 2020 19:41:17 +0100 Subject: [PATCH] Some fixes --- build.gradle | 2 +- .../configurator/MarlinConfigurator.java | 5 --- .../configurator/command/CommandRunner.java | 37 ++++++++++++++----- .../configurator/util/ConsoleHelper.java | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index 926fc02..5536dde 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,7 @@ dependencies { application { mainClass = 'fr.chuckame.marlinfw.configurator.MarlinConfigurator' - applicationDefaultJvmArgs = ['-Dapp-console-name=marlin-console-configurator.jar'] + applicationDefaultJvmArgs = ['-Dapp-console-name=marlin-console-configurator'] } test { diff --git a/src/main/java/fr/chuckame/marlinfw/configurator/MarlinConfigurator.java b/src/main/java/fr/chuckame/marlinfw/configurator/MarlinConfigurator.java index fa85ce4..9cbb1d9 100644 --- a/src/main/java/fr/chuckame/marlinfw/configurator/MarlinConfigurator.java +++ b/src/main/java/fr/chuckame/marlinfw/configurator/MarlinConfigurator.java @@ -7,10 +7,5 @@ public class MarlinConfigurator { public static void main(final String[] args) { SpringApplication.run(MarlinConfigurator.class, args); - // SpringApplication.run(MarlinConfigurator.class, "help"); - // SpringApplication.run(MarlinConfigurator.class, "apply", "--save", "--profile", "src/test/resources/profile.yaml", "--file", "src/test/resources/file.h"); - //SpringApplication.run(MarlinConfigurator.class, "generate-profile", "src/test/resources/file.h", "--output", "src/test/resources/profile.yaml"); - // SpringApplication.run(MarlinConfigurator.class, "generate-profile", "src/test/resources/file.h", "--output", "console"); - // SpringApplication.run(MarlinConfigurator.class, "diff", "--left", "src/test/resources/file.h", "--right", "src/test/resources/file2.h"); } } diff --git a/src/main/java/fr/chuckame/marlinfw/configurator/command/CommandRunner.java b/src/main/java/fr/chuckame/marlinfw/configurator/command/CommandRunner.java index 3958e56..811eb2a 100644 --- a/src/main/java/fr/chuckame/marlinfw/configurator/command/CommandRunner.java +++ b/src/main/java/fr/chuckame/marlinfw/configurator/command/CommandRunner.java @@ -14,20 +14,37 @@ public class CommandRunner implements CommandLineRunner { private final ConsoleHelper consoleHelper; @Override - public void run(final String... args) throws Exception { + public void run(final String[] args) throws Exception { try { - jCommander.parse(args); - } catch (final ParameterException e) { - consoleHelper.writeErrorLine("Bad argument: " + e.getMessage()); - consoleHelper.writeLine(jCommander.getUsageFormatter()::usage); - System.exit(InvalidUseException.EXIT_CODE); - } - try { - final Command command = (Command) jCommander.findCommandByAlias(jCommander.getParsedAlias()).getObjects().get(0); - command.run().blockOptional(); + findCommandByAlias(parseAlias(args)).run().blockOptional(); } catch (final ManuallyStoppedException e) { consoleHelper.writeErrorLine(e.getMessage()); System.exit(e.getExitCode()); } } + + private String parseAlias(final String[] args) { + String errorMessage = null; + try { + jCommander.parse(args); + } catch (final ParameterException e) { + errorMessage = e.getMessage(); + } + if (errorMessage == null && jCommander.getParsedAlias() != null) { + return jCommander.getParsedAlias(); + } + + if (errorMessage == null) { + consoleHelper.writeErrorLine("No argument passed"); + } else { + consoleHelper.writeErrorLine("Bad argument: " + errorMessage); + } + consoleHelper.writeLine(jCommander.getUsageFormatter()::usage); + System.exit(InvalidUseException.EXIT_CODE); + return null; + } + + private Command findCommandByAlias(final String alias) { + return (Command) jCommander.findCommandByAlias(alias).getObjects().get(0); + } } diff --git a/src/main/java/fr/chuckame/marlinfw/configurator/util/ConsoleHelper.java b/src/main/java/fr/chuckame/marlinfw/configurator/util/ConsoleHelper.java index ed9d649..a7286e4 100644 --- a/src/main/java/fr/chuckame/marlinfw/configurator/util/ConsoleHelper.java +++ b/src/main/java/fr/chuckame/marlinfw/configurator/util/ConsoleHelper.java @@ -35,7 +35,7 @@ public void writeLine(final String line) { public void writeLine(final Consumer lineBuilder) { final var line = new StringBuilder(); lineBuilder.accept(line); - writeLine(lineBuilder.toString()); + writeLine(line.toString()); } public void writeErrorLine(final String line) {