From 89df440859f0180145fcee7af2c57d2af5d8188c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Thu, 7 Mar 2024 10:10:53 -0300 Subject: [PATCH] add validation at MainOptions --- .../clagomess/pirilampo/cli/MainOptions.java | 17 +++++++++++++---- .../pirilampo/cli/MainOptionsTest.java | 6 ++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java b/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java index ec2dff0..84ff224 100644 --- a/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java +++ b/cli/src/main/java/com/github/clagomess/pirilampo/cli/MainOptions.java @@ -5,11 +5,14 @@ import com.github.clagomess.pirilampo.core.enums.CompilationTypeEnum; import com.github.clagomess.pirilampo.core.enums.HtmlPanelToggleEnum; import com.github.clagomess.pirilampo.core.enums.LayoutPdfEnum; +import com.github.clagomess.pirilampo.core.exception.ParametersException; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.cli.*; import java.io.File; import java.util.Arrays; +@Slf4j public class MainOptions { private final ParametersDto defaultParameters = new ParametersDto(); @@ -177,12 +180,18 @@ public ParametersDto getArgs(String[] argv){ parameters.setProjectTarget(new File(cmd.getOptionValue(projectTarget))); } + parameters.validate(); + return parameters; - } catch (ParseException e) { - System.out.println(e.getMessage()); + } catch (ParseException | ParametersException e ) { + log.error(e.getMessage()); + } catch (Throwable e) { + log.error(log.getName(), e); + } finally { new HelpFormatter().printHelp("Pirilampo", options); - System.exit(1); - return null; } + + System.exit(1); + return null; } } diff --git a/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainOptionsTest.java b/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainOptionsTest.java index c5d80aa..88838f1 100644 --- a/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainOptionsTest.java +++ b/cli/src/test/java/com/github/clagomess/pirilampo/cli/MainOptionsTest.java @@ -13,4 +13,10 @@ public class MainOptionsTest { public void getArgs(){ mainOptions.getArgs(new String[]{"-projectLogo", "aaa"}); } + + @Test + @ExpectSystemExitWithStatus(1) + public void getArgs_trigger_validate(){ + mainOptions.getArgs(new String[]{"-projectSource", "aaa"}); + } }