From d0facd8b18c06f49b44fd62d541b9b76778158e0 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko <svjatoslav@svjatoslav.eu> Date: Sun, 21 Apr 2024 18:51:11 +0300 Subject: [PATCH] Add FloatOption class for CLI parameter parsing This commit introduces a new FloatOption class to handle float type parameters in the command-line interface. This new class ensures that the input argument is correctly parsed as a float and includes appropriate error handling for invalid inputs. --- .../parameter/FloatOption.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FloatOption.java diff --git a/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FloatOption.java b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FloatOption.java new file mode 100644 index 0000000..f84b334 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/cli_helper/parameter_parser/parameter/FloatOption.java @@ -0,0 +1,35 @@ +package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter; + +import eu.svjatoslav.commons.cli_helper.parameter_parser.Option; +import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount; + +public class FloatOption extends Option<Float, FloatOption> { + + public FloatOption(final String description) { + super(description, ParameterCount.ONE); + } + + @Override + public java.lang.String describeFormat() { + return "Float."; + } + + @Override + public Float getValue() { + if (parameters.size() != 1) + throw new RuntimeException("Parameter " + description + + " shall have exactly 1 argument."); + return Float.parseFloat(parameters.get(0)); + } + + @Override + public boolean isValid(final java.lang.String value) { + try { + java.lang.Float.valueOf(value); + return true; + } catch (final NumberFormatException e) { + return false; + } + } +} + -- 2.20.1