forked from bmatthias/config-builder
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more flexible command line value description (#50)
Signed-off-by: Daniel Kraschewski <daniel.kraschewski@tngtech.com>
- Loading branch information
krasched
committed
Dec 27, 2020
1 parent
c5d1220
commit 972ae9d
Showing
9 changed files
with
154 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
.../java/com/tngtech/configbuilder/annotation/valueextractor/CommandLineValueDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.tngtech.configbuilder.annotation.valueextractor; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* This annotation is used to mark a method as description text supplier for command line options. | ||
* The annotated method must be static and accept a single String parameter, which is the longOpt name of a command line option. | ||
* There may be at most one such method per class.<br> | ||
* If a field is annotated with {@link com.tngtech.configbuilder.annotation.valueextractor.CommandLineValue} but has no description, | ||
* then this method is called to generate the description text.<br> | ||
* <b>Usage:</b> <code>@CommandLineValueDescriptor</code> | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface CommandLineValueDescriptor {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/com/tngtech/configbuilder/exception/InvalidDescriptionMethodException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.tngtech.configbuilder.exception; | ||
|
||
public class InvalidDescriptionMethodException extends RuntimeException {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/test/java/com/tngtech/configbuilder/util/CommandLineHelperExceptionHandlingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.tngtech.configbuilder.util; | ||
|
||
import com.tngtech.configbuilder.ConfigBuilder; | ||
import com.tngtech.configbuilder.annotation.valueextractor.CommandLineValue; | ||
import com.tngtech.configbuilder.annotation.valueextractor.CommandLineValueDescriptor; | ||
import com.tngtech.configbuilder.exception.ConfigBuilderException; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class CommandLineHelperExceptionHandlingTest { | ||
|
||
private static class TestConfig { | ||
|
||
@CommandLineValue(shortOpt = "u", longOpt = "user", required = true) | ||
public String user; | ||
} | ||
|
||
private static class TestConfigWithInvalidCommandLineValueDescriptor { | ||
|
||
@CommandLineValue(shortOpt = "u", longOpt = "user") | ||
public String user; | ||
|
||
@CommandLineValueDescriptor | ||
private static void description() { } | ||
} | ||
|
||
private static class TestConfigWithMultipleCommandLineValueDescriptors { | ||
|
||
@CommandLineValue(shortOpt = "u", longOpt = "user") | ||
public String user; | ||
|
||
@CommandLineValueDescriptor | ||
private static String description1() { | ||
return ""; | ||
} | ||
|
||
@CommandLineValueDescriptor | ||
private static String description2() { | ||
return ""; | ||
} | ||
} | ||
|
||
@Test | ||
public void testUndefinedCommandLineOption() { | ||
String[] args = new String[]{"nd", "notDefined"}; | ||
assertThatThrownBy(() -> ConfigBuilder.on(TestConfig.class).withCommandLineArgs(args).build()) | ||
.isInstanceOf(ConfigBuilderException.class) | ||
.hasMessageContaining("unable to parse command line arguments"); | ||
} | ||
|
||
@Test | ||
public void testInvalidCommandLineValueDescriptor() { | ||
assertThatThrownBy(() -> ConfigBuilder.on(TestConfigWithInvalidCommandLineValueDescriptor.class).printCommandLineHelp()) | ||
.isInstanceOf(ConfigBuilderException.class) | ||
.hasMessageContaining("invalid or multiple use of the @CommandLineValueDescriptor annotation"); | ||
|
||
assertThatThrownBy(() -> ConfigBuilder.on(TestConfigWithInvalidCommandLineValueDescriptor.class).build()) | ||
.isInstanceOf(ConfigBuilderException.class) | ||
.hasMessageContaining("invalid or multiple use of the @CommandLineValueDescriptor annotation"); | ||
} | ||
|
||
@Test | ||
public void testMultipleCommandLineValueDescriptors() { | ||
assertThatThrownBy(() -> ConfigBuilder.on(TestConfigWithMultipleCommandLineValueDescriptors.class).printCommandLineHelp()) | ||
.isInstanceOf(ConfigBuilderException.class) | ||
.hasMessageContaining("invalid or multiple use of the @CommandLineValueDescriptor annotation"); | ||
|
||
assertThatThrownBy(() -> ConfigBuilder.on(TestConfigWithMultipleCommandLineValueDescriptors.class).build()) | ||
.isInstanceOf(ConfigBuilderException.class) | ||
.hasMessageContaining("invalid or multiple use of the @CommandLineValueDescriptor annotation"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters