From 08476ed9942dd8d4ca8591f6e656d9401c1b20fc Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Mon, 3 Oct 2022 00:05:03 +0200 Subject: [PATCH] [DEV] correct some argument parsing and update remove scenarium --- .classpath | 2 +- src/module-info.java | 2 +- src/org/atriasoft/death/ArgumentManager.java | 51 ++++++++++--- .../atriasoft/death/PropertiesInterface.java | 72 ++++++++++++++----- src/org/atriasoft/death/internal/Log.java | 4 +- 5 files changed, 100 insertions(+), 31 deletions(-) diff --git a/.classpath b/.classpath index 314c64e..bb8e2b8 100644 --- a/.classpath +++ b/.classpath @@ -15,7 +15,7 @@ - + diff --git a/src/module-info.java b/src/module-info.java index 9143ab7..8cabdc7 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -6,6 +6,6 @@ open module org.atriasoft.death { exports org.atriasoft.death; exports org.atriasoft.death.annotation; - requires transitive io.scenarium.logger; + requires transitive org.atriasoft.reggol; requires java.base; } diff --git a/src/org/atriasoft/death/ArgumentManager.java b/src/org/atriasoft/death/ArgumentManager.java index 5a8c938..b98a8fc 100644 --- a/src/org/atriasoft/death/ArgumentManager.java +++ b/src/org/atriasoft/death/ArgumentManager.java @@ -50,59 +50,70 @@ public class ArgumentManager { final String parameter = elements[0]; String newValue = null; if (!this.properties.existParameter(parameter)) { - Log.critical("Parameter Does not exist for '{}'", value); + Log.error("Parameter Does not exist for '{}'", value); + showHelpAndExitError(); } if (!this.properties.haveParameter(parameter)) { // Boolean value if (elements.length != 1) { - Log.critical("Boolean parameter can not have parameters for '{}", value); + Log.error("Boolean parameter can not have parameters for '{}", value); + showHelpAndExitError(); } } else if (elements.length == 1) { if (iii == this.arguments.size() - 1) { - Log.critical("Missing parameters for '{}' (no more arguments)", value); + Log.error("Missing parameters for '{}' (no more arguments)", value); + showHelpAndExitError(); } newValue = this.arguments.get(iii + 1); if (newValue.startsWith("-")) { - Log.critical("Missing parameters for '{}' (next argument is a parameter (start with '-'))", value); + Log.error("Missing parameters for '{}' (next argument is a parameter (start with '-'))", value); + showHelpAndExitError(); } iii++; } else if (elements.length == 2) { newValue = elements[1]; } else { - Log.critical("too much parameters for '{}' (only 1 = is authorized)", value); + Log.error("too much parameters for '{}' (only 1 = is authorized)", value); + showHelpAndExitError(); } this.properties.setParameter(tmp, parameter, newValue); } else if (value.startsWith("-")) { final String[] elements = value.split("="); elements[0] = elements[0].substring(1); - Log.warning("find element (-) : {}", Arrays.toString(elements)); + Log.verbose("find element (-) : {}", Arrays.toString(elements)); if (elements[0].length() != 1) { - Log.critical("Can not parse alias argument with a size != 1 for '{}'", value); + Log.error("Can not parse alias argument with a size != 1 for '{}'", value); + showHelpAndExitError(); } final char parameter = elements[0].charAt(0); String newValue = null; if (!this.properties.existParameterAlias(parameter)) { - Log.critical("Parameter Does not exist for '{}'", value); + Log.error("Parameter Does not exist for '{}'", value); + showHelpAndExitError(); } if (!this.properties.haveParameterAlias(parameter)) { // Boolean value if (elements.length != 1) { Log.critical("Boolean parameter can not have parameters for '{}'", value); + showHelpAndExitError(); } } else if (elements.length == 1) { if (iii == this.arguments.size() - 1) { - Log.critical("Missing parameters for '{}' (no more arguments)", value); + Log.error("Missing parameters for '{}' (no more arguments)", value); + showHelpAndExitError(); } newValue = this.arguments.get(iii + 1); if (newValue.startsWith("-")) { - Log.critical("Missing parameters for '{}' (next argument is a parameter (start with '-'))", value); + Log.error("Missing parameters for '{}' (next argument is a parameter (start with '-'))", value); + showHelpAndExitError(); } iii++; } else if (elements.length == 2) { newValue = elements[1]; } else { - Log.critical("too much parameters for '{}' (only 1 = is authorized)", value); + Log.error("too much parameters for '{}' (only 1 = is authorized)", value); + showHelpAndExitError(); } this.properties.setParameterAlias(tmp, parameter, newValue); } else { @@ -270,7 +281,25 @@ public class ArgumentManager { return this.actionDetected != null; } + /** + * Display help of the global elements + */ public void showHelp() { this.properties.displayHelp(); } + + /** + * Display help of the global elements and the help of each sub-actions + */ + public void showHelpAndExitError() { + showHelp(); + System.exit(-2); + } + + /** + * Display help of the global elements and the help of each sub-actions + */ + public void showHelpFull() { + this.properties.displayHelp(true); + } } diff --git a/src/org/atriasoft/death/PropertiesInterface.java b/src/org/atriasoft/death/PropertiesInterface.java index a444bbc..962278e 100644 --- a/src/org/atriasoft/death/PropertiesInterface.java +++ b/src/org/atriasoft/death/PropertiesInterface.java @@ -3,6 +3,7 @@ package org.atriasoft.death; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.nio.CharBuffer; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -119,8 +120,23 @@ public class PropertiesInterface { } public void displayHelp() { - Log.print("Usage:"); - final StringBuilder applicationLine = new StringBuilder(" "); + displayHelp(false); + } + + public void displayHelp(final boolean withActions) { + + displayHelp(withActions, 0); + } + + public void displayHelp(final boolean withActions, int baseIndent) { + if (baseIndent == 0) { + Log.print("Usage:"); + } + final StringBuilder applicationLine = new StringBuilder(spaces(4 + baseIndent)); + if (baseIndent != 0) { + baseIndent += 4; + Log.print("{}-----------------------------", spaces(baseIndent)); + } if (this.classCommand != null) { applicationLine.append(this.classCommand).append(" "); } else { @@ -130,49 +146,64 @@ public class PropertiesInterface { applicationLine.append("[options] "); } if (this.actions != null && this.actions.getActions().size() != 0) { - applicationLine.append("[actions] ..."); + applicationLine.append(" [options-action]"); } Log.print(applicationLine.toString()); if (this.classDescription != null) { - Log.print("{}", this.classDescription); + Log.print("{}{}", spaces(baseIndent), this.classDescription); } if (this.properties.size() != 0) { - Log.print(" [options]"); + Log.print("{} [options]", spaces(baseIndent)); for (final FieldProperty prop : this.properties) { final String parameterExpect = haveParameter(prop) ? " [PARAMETER]" : ""; if (prop.alias != null && prop.name != null) { - Log.print(" -{} / --{}{}", prop.alias, prop.name, parameterExpect); + Log.print("{} -{} / --{}{}", spaces(baseIndent), prop.alias, prop.name, parameterExpect); } else if (prop.alias != null) { - Log.print(" -{}{}", prop.alias, parameterExpect); + Log.print("{} -{}{}", spaces(baseIndent), prop.alias, parameterExpect); } else if (prop.name != null) { - Log.print(" --{}{}", prop.name, parameterExpect); + Log.print("{} --{}{}", spaces(baseIndent), prop.name, parameterExpect); } if (prop.description != null) { - Log.print(" {}", prop.description); + Log.print("{} {}", spaces(baseIndent), prop.description); } if (prop.choice != null) { for (final ChoiceElement elem : prop.getChoiceList()) { if (elem.destination() == null) { - Log.print(" - {}", elem.origin()); + Log.print("{} - {}", spaces(baseIndent), elem.origin()); } else { - Log.print(" - {} ==> {}", elem.origin(), elem.destination()); + Log.print("{} - {} ==> {}", spaces(baseIndent), elem.origin(), elem.destination()); } } } } } if (this.actions != null && this.actions.getActions().size() != 0) { - Log.print(" [actions]"); + Log.print("{} ", spaces(baseIndent)); final Set actions = this.actions.getActions(); + + int sizeOfActions = 0; + for (final String action : actions) { + sizeOfActions = Math.max(sizeOfActions, action.length()); + } for (final String action : actions) { final Class clazz = this.actions.getInterface(action); - final String description = PropertiesInterface.getDescription(clazz); - if (description == null || description.isEmpty()) { - Log.print(" {}", action); + if (!withActions) { + final String description = PropertiesInterface.getDescription(clazz); + if (description == null || description.isEmpty()) { + Log.print("{} {}", spaces(baseIndent), action); + } else { + final String emptyString = spaces(sizeOfActions - action.length()); + Log.print("{} {}:{} {}", spaces(baseIndent), action, emptyString, description); + } } else { - Log.print(" {}: {}", action, description); + final PropertiesInterface tmpInterface = new PropertiesInterface(clazz); + tmpInterface.displayHelp(false, baseIndent + 4); } } + if (!withActions) { + Log.print(" [options-action]"); + Log.print(" Action have theire specifi help. (add -h or --help after action)"); + } } } @@ -274,4 +305,13 @@ public class PropertiesInterface { final FieldProperty property = findFieldAlias(parameter); setParameter(tmp, property.name, newValue); } + + /** + * Creates a string of spaces that is 'spaces' spaces long. + * + * @param spaces The number of spaces to add to the string. + */ + private String spaces(final int spaces) { + return CharBuffer.allocate(spaces).toString().replace('\0', ' '); + } } diff --git a/src/org/atriasoft/death/internal/Log.java b/src/org/atriasoft/death/internal/Log.java index 1754b83..dcfafa0 100644 --- a/src/org/atriasoft/death/internal/Log.java +++ b/src/org/atriasoft/death/internal/Log.java @@ -1,7 +1,7 @@ package org.atriasoft.death.internal; -import io.scenarium.logger.LogLevel; -import io.scenarium.logger.Logger; +import org.atriasoft.reggol.LogLevel; +import org.atriasoft.reggol.Logger; public class Log { private static final String LIB_NAME = "death";