Skip to content

Commit a19317d

Browse files
committed
update: use blank option names in OptCfg as indent in help
1 parent 9a1cc72 commit a19317d

File tree

5 files changed

+98
-10
lines changed

5 files changed

+98
-10
lines changed

src/main/java/com/github/sttk/cliargs/OptCfg.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.github.sttk.cliargs;
77

88
import static com.github.sttk.cliargs.Util.isEmpty;
9+
import static com.github.sttk.cliargs.Util.isBlank;
910
import static java.util.Collections.unmodifiableList;
1011
import static java.util.Collections.emptyList;
1112

@@ -210,17 +211,18 @@ public <T> OptCfg(NamedParam<T> ...params) {
210211
}
211212

212213
private void fillmissing(Init<?> init) {
214+
List<String> nonBlankNames = (init.names == null) ? null :
215+
init.names.stream().filter(s -> !isBlank(s)).toList();
216+
213217
if (isEmpty(init.storeKey)) {
214-
if (! isEmpty(init.names)) {
215-
init.storeKey = init.names.get(0);
218+
if (! isEmpty(nonBlankNames)) {
219+
init.storeKey = nonBlankNames.get(0);
216220
}
217221
} else {
218-
if (isEmpty(init.names)) {
219-
init.names = new ArrayList<String>();
222+
if (isEmpty(nonBlankNames)) {
223+
init.names = (init.names == null) ? new ArrayList<String>() :
224+
new ArrayList<String>(init.names);
220225
init.names.add(init.storeKey);
221-
} else if (isEmpty(init.names.get(0))) {
222-
init.names = new ArrayList<String>(init.names);
223-
init.names.set(0, init.storeKey);
224226
}
225227
}
226228

src/main/java/com/github/sttk/cliargs/Util.java

+4
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ static boolean isEmpty(String[] array) {
2020
static boolean isEmpty(List<?> list) {
2121
return (list == null || list.isEmpty());
2222
}
23+
24+
static boolean isBlank(String value) {
25+
return (value == null || value.isBlank());
26+
}
2327
}

src/test/java/com/github/sttk/cliargs/OptCfgTest.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,29 @@ void testConstructor_withNamedParam_storeKeyIsEmpty() {
183183
}
184184

185185
@Test
186-
void testConstructor_withNamedParam_firstElemOfNamesIsEmpty() {
186+
void testConstructor_withNamedParam_acceptBlankNames() {
187187
@SuppressWarnings("unchecked")
188-
var optCfg = new OptCfg(storeKey("FooBar"), names("", "f"));
188+
var optCfg = new OptCfg(storeKey("FooBar"), names("", " ", "f"));
189189

190190
assertThat(optCfg.storeKey).isEqualTo("FooBar");
191-
assertThat(optCfg.names).containsExactly("FooBar", "f");
191+
assertThat(optCfg.names).containsExactly("", " ", "f");
192+
assertThat(optCfg.hasArg).isFalse();
193+
assertThat(optCfg.isArray).isFalse();
194+
assertThat(optCfg.type).isNull();
195+
assertThat(optCfg.defaults).isNull();
196+
assertThat(optCfg.desc).isNull();
197+
assertThat(optCfg.argInHelp).isNull();
198+
assertThat(optCfg.converter).isNull();
199+
assertThat(optCfg.postparser).isNull();
200+
}
201+
202+
@Test
203+
void testConstructor_withNamedParam_notSetBlankNameToStoreKey() {
204+
@SuppressWarnings("unchecked")
205+
var optCfg = new OptCfg(names(" ", "foo-bar", "f"));
206+
207+
assertThat(optCfg.storeKey).isEqualTo("foo-bar");
208+
assertThat(optCfg.names).containsExactly(" ", "foo-bar", "f");
192209
assertThat(optCfg.hasArg).isFalse();
193210
assertThat(optCfg.isArray).isFalse();
194211
assertThat(optCfg.type).isNull();

src/test/java/com/github/sttk/cliargs/ParseWithTest.java

+54
Original file line numberDiff line numberDiff line change
@@ -2121,4 +2121,58 @@ record Reason() {}
21212121
assertThat((List<?>)cmd.getOptArgs("foo")).isEmpty();
21222122
assertThat(cmd.getArgs()).isEmpty();
21232123
}
2124+
2125+
@Test
2126+
void testParseWith_notToSetBlankNameInNamesToStoreKey() {
2127+
@SuppressWarnings("unchecked")
2128+
var cfgs = new OptCfg[]{
2129+
new OptCfg(
2130+
names(" ", "foo", "f"),
2131+
hasArg(true),
2132+
isArray(true)
2133+
)
2134+
};
2135+
2136+
var args = new String[] {"--foo", "A", "-f", "B"};
2137+
var cliargs = new CliArgs("app", args);
2138+
var result = cliargs.parseWith(cfgs);
2139+
2140+
assertThat(result.optCfgs()).isEqualTo(cfgs);
2141+
assertThat(result.exception()).isNull();
2142+
2143+
var cmd = result.cmd();
2144+
assertThat(cmd.getName()).isEqualTo("app");
2145+
assertThat(cmd.hasOpt("foo")).isTrue();
2146+
assertThat((String)cmd.getOptArg("foo")).isEqualTo("A");
2147+
List<String> foo = cmd.getOptArgs("foo");
2148+
assertThat(foo).containsExactly("A", "B");
2149+
assertThat(cmd.getArgs()).isEmpty();
2150+
}
2151+
2152+
@Test
2153+
void testParseWith_addStoreKeyToNamesIsSpecifiedNamesAreAllBlank() {
2154+
@SuppressWarnings("unchecked")
2155+
var cfgs = new OptCfg[]{
2156+
new OptCfg(
2157+
storeKey("foo"),
2158+
names(" ", " ", " "),
2159+
hasArg(true)
2160+
)
2161+
};
2162+
2163+
var args = new String[] {"--foo", "A"};
2164+
var cliargs = new CliArgs("app", args);
2165+
var result = cliargs.parseWith(cfgs);
2166+
2167+
assertThat(result.optCfgs()).isEqualTo(cfgs);
2168+
assertThat(result.exception()).isNull();
2169+
2170+
var cmd = result.cmd();
2171+
assertThat(cmd.getName()).isEqualTo("app");
2172+
assertThat(cmd.hasOpt("foo")).isTrue();
2173+
assertThat((String)cmd.getOptArg("foo")).isEqualTo("A");
2174+
List<String> foo = cmd.getOptArgs("foo");
2175+
assertThat(foo).containsExactly("A");
2176+
assertThat(cmd.getArgs()).isEmpty();
2177+
}
21242178
}

src/test/java/com/github/sttk/cliargs/UtilTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.sttk.cliargs;
22

33
import static com.github.sttk.cliargs.Util.isEmpty;
4+
import static com.github.sttk.cliargs.Util.isBlank;
45
import static org.assertj.core.api.Assertions.assertThat;
56
import org.junit.jupiter.api.Test;
67
import java.util.List;
@@ -31,4 +32,14 @@ void testIsEmpty_list() {
3132
assertThat(isEmpty(new ArrayList<String>())).isTrue();
3233
assertThat(isEmpty((List<Integer>) null)).isTrue();
3334
}
35+
36+
@Test
37+
void testIsBlank_string() {
38+
assertThat(isBlank("abc")).isFalse();
39+
assertThat(isBlank("")).isTrue();
40+
assertThat(isBlank((String)null)).isTrue();
41+
assertThat(isBlank(" ")).isTrue();
42+
assertThat(isBlank(" ")).isTrue();
43+
assertThat(isBlank("\t")).isTrue();
44+
}
3445
}

0 commit comments

Comments
 (0)