[DEV] correct some argument parsing and update remove scenarium

This commit is contained in:
Edouard DUPIN 2022-10-03 00:05:03 +02:00
parent 9128ed2dcf
commit 08476ed994
5 changed files with 100 additions and 31 deletions

View File

@ -15,7 +15,7 @@
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/scenarium-logger"> <classpathentry combineaccessrules="false" kind="src" path="/atriasoft-reggol">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
</attributes> </attributes>

View File

@ -6,6 +6,6 @@ open module org.atriasoft.death {
exports org.atriasoft.death; exports org.atriasoft.death;
exports org.atriasoft.death.annotation; exports org.atriasoft.death.annotation;
requires transitive io.scenarium.logger; requires transitive org.atriasoft.reggol;
requires java.base; requires java.base;
} }

View File

@ -50,59 +50,70 @@ public class ArgumentManager {
final String parameter = elements[0]; final String parameter = elements[0];
String newValue = null; String newValue = null;
if (!this.properties.existParameter(parameter)) { 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)) { if (!this.properties.haveParameter(parameter)) {
// Boolean value // Boolean value
if (elements.length != 1) { 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) { } else if (elements.length == 1) {
if (iii == this.arguments.size() - 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); newValue = this.arguments.get(iii + 1);
if (newValue.startsWith("-")) { 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++; iii++;
} else if (elements.length == 2) { } else if (elements.length == 2) {
newValue = elements[1]; newValue = elements[1];
} else { } 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); this.properties.setParameter(tmp, parameter, newValue);
} else if (value.startsWith("-")) { } else if (value.startsWith("-")) {
final String[] elements = value.split("="); final String[] elements = value.split("=");
elements[0] = elements[0].substring(1); 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) { 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); final char parameter = elements[0].charAt(0);
String newValue = null; String newValue = null;
if (!this.properties.existParameterAlias(parameter)) { 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)) { if (!this.properties.haveParameterAlias(parameter)) {
// Boolean value // Boolean value
if (elements.length != 1) { if (elements.length != 1) {
Log.critical("Boolean parameter can not have parameters for '{}'", value); Log.critical("Boolean parameter can not have parameters for '{}'", value);
showHelpAndExitError();
} }
} else if (elements.length == 1) { } else if (elements.length == 1) {
if (iii == this.arguments.size() - 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); newValue = this.arguments.get(iii + 1);
if (newValue.startsWith("-")) { 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++; iii++;
} else if (elements.length == 2) { } else if (elements.length == 2) {
newValue = elements[1]; newValue = elements[1];
} else { } 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); this.properties.setParameterAlias(tmp, parameter, newValue);
} else { } else {
@ -270,7 +281,25 @@ public class ArgumentManager {
return this.actionDetected != null; return this.actionDetected != null;
} }
/**
* Display help of the global elements
*/
public void showHelp() { public void showHelp() {
this.properties.displayHelp(); 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);
}
} }

View File

@ -3,6 +3,7 @@ package org.atriasoft.death;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.nio.CharBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -119,8 +120,23 @@ public class PropertiesInterface {
} }
public void displayHelp() { public void displayHelp() {
Log.print("Usage:"); displayHelp(false);
final StringBuilder applicationLine = new StringBuilder(" "); }
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) { if (this.classCommand != null) {
applicationLine.append(this.classCommand).append(" "); applicationLine.append(this.classCommand).append(" ");
} else { } else {
@ -130,49 +146,64 @@ public class PropertiesInterface {
applicationLine.append("[options] "); applicationLine.append("[options] ");
} }
if (this.actions != null && this.actions.getActions().size() != 0) { if (this.actions != null && this.actions.getActions().size() != 0) {
applicationLine.append("[actions] ..."); applicationLine.append("<actions> [options-action]");
} }
Log.print(applicationLine.toString()); Log.print(applicationLine.toString());
if (this.classDescription != null) { if (this.classDescription != null) {
Log.print("{}", this.classDescription); Log.print("{}{}", spaces(baseIndent), this.classDescription);
} }
if (this.properties.size() != 0) { if (this.properties.size() != 0) {
Log.print(" [options]"); Log.print("{} [options]", spaces(baseIndent));
for (final FieldProperty prop : this.properties) { for (final FieldProperty prop : this.properties) {
final String parameterExpect = haveParameter(prop) ? " [PARAMETER]" : ""; final String parameterExpect = haveParameter(prop) ? " [PARAMETER]" : "";
if (prop.alias != null && prop.name != null) { 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) { } else if (prop.alias != null) {
Log.print(" -{}{}", prop.alias, parameterExpect); Log.print("{} -{}{}", spaces(baseIndent), prop.alias, parameterExpect);
} else if (prop.name != null) { } else if (prop.name != null) {
Log.print(" --{}{}", prop.name, parameterExpect); Log.print("{} --{}{}", spaces(baseIndent), prop.name, parameterExpect);
} }
if (prop.description != null) { if (prop.description != null) {
Log.print(" {}", prop.description); Log.print("{} {}", spaces(baseIndent), prop.description);
} }
if (prop.choice != null) { if (prop.choice != null) {
for (final ChoiceElement elem : prop.getChoiceList()) { for (final ChoiceElement elem : prop.getChoiceList()) {
if (elem.destination() == null) { if (elem.destination() == null) {
Log.print(" - {}", elem.origin()); Log.print("{} - {}", spaces(baseIndent), elem.origin());
} else { } else {
Log.print(" - {} ==> {}", elem.origin(), elem.destination()); Log.print("{} - {} ==> {}", spaces(baseIndent), elem.origin(), elem.destination());
} }
} }
} }
} }
} }
if (this.actions != null && this.actions.getActions().size() != 0) { if (this.actions != null && this.actions.getActions().size() != 0) {
Log.print(" [actions]"); Log.print("{} <actions>", spaces(baseIndent));
final Set<String> actions = this.actions.getActions(); final Set<String> actions = this.actions.getActions();
int sizeOfActions = 0;
for (final String action : actions) {
sizeOfActions = Math.max(sizeOfActions, action.length());
}
for (final String action : actions) { for (final String action : actions) {
final Class<?> clazz = this.actions.getInterface(action); final Class<?> clazz = this.actions.getInterface(action);
final String description = PropertiesInterface.getDescription(clazz); if (!withActions) {
if (description == null || description.isEmpty()) { final String description = PropertiesInterface.getDescription(clazz);
Log.print(" {}", action); 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 { } 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); final FieldProperty property = findFieldAlias(parameter);
setParameter(tmp, property.name, newValue); 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', ' ');
}
} }

View File

@ -1,7 +1,7 @@
package org.atriasoft.death.internal; package org.atriasoft.death.internal;
import io.scenarium.logger.LogLevel; import org.atriasoft.reggol.LogLevel;
import io.scenarium.logger.Logger; import org.atriasoft.reggol.Logger;
public class Log { public class Log {
private static final String LIB_NAME = "death"; private static final String LIB_NAME = "death";