From a1e6739d595fc0de12861ba9c15758de2f2f9bfe Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Thu, 4 Jul 2024 09:40:10 -0700 Subject: [PATCH] G2-1607 Parse enums --- .../gearbox/argparse/BasicParameterParser.java | 16 ++++++++++++++++ .../argparse/StandardParameterParserFactory.java | 1 + 2 files changed, 17 insertions(+) diff --git a/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/BasicParameterParser.java b/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/BasicParameterParser.java index 58891d3..15e52f8 100644 --- a/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/BasicParameterParser.java +++ b/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/BasicParameterParser.java @@ -3,6 +3,7 @@ import java.nio.file.Paths; import java.util.ListIterator; +import com.g2forge.alexandria.java.core.enums.HEnum; import com.g2forge.alexandria.java.fluent.optional.IOptional; import com.g2forge.alexandria.java.fluent.optional.NullableOptional; @@ -43,5 +44,20 @@ public IOptional getDefault(IParameterInfo parameter) { public Object parse(IParameterInfo parameter, ListIterator argumentIterator) { return argumentIterator.next(); } + }, + ENUM { + @Override + public IOptional getDefault(IParameterInfo parameter) { + if (parameter.getSubject().bind(Parameter.class).isPresent()) return NullableOptional.of(null); + return NullableOptional.empty(); + } + + @SuppressWarnings("unchecked") + @Override + public Object parse(IParameterInfo parameter, ListIterator argumentIterator) { + @SuppressWarnings("rawtypes") + final Class cast = (Class) parameter.getType(); + return HEnum.valueOfInsensitive(cast, argumentIterator.next()); + } }; } \ No newline at end of file diff --git a/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/StandardParameterParserFactory.java b/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/StandardParameterParserFactory.java index 5c7c4de..3cdb272 100644 --- a/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/StandardParameterParserFactory.java +++ b/gb-argparse/src/main/java/com/g2forge/gearbox/argparse/StandardParameterParserFactory.java @@ -74,6 +74,7 @@ private IParameterParser computeParser(final Class type) { if (Path.class.equals(type)) return BasicParameterParser.PATH; if (String.class.equals(type)) return BasicParameterParser.STRING; if (Boolean.TYPE.equals(type) || Boolean.class.equals(type)) return BasicParameterParser.BOOLEAN; + if (Enum.class.isAssignableFrom(type)) return BasicParameterParser.ENUM; if (type.isArray()) { final Class componentType = type.getComponentType(); final IParameterParser componentParser = computeParser(componentType);