Skip to content

Commit a1c475c

Browse files
authored
update: added toString method to Cmd and OptCfg class (#18)
1 parent 7dc49ac commit a1c475c

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static java.util.Collections.emptyMap;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.StringJoiner;
1516

1617
/**
1718
* Is the class which contains a command name, command arguments, and option
@@ -24,6 +25,20 @@ public class Cmd {
2425
private final List<String> args;
2526
private final Map<String, List<?>> opts;
2627

28+
/**
29+
* Outputs a string of the content of this object.
30+
*
31+
* @return A content string of this object.
32+
*/
33+
@Override
34+
public String toString() {
35+
return new StringJoiner(", ", "Cmd{", "}")
36+
.add("name=" + this.name)
37+
.add("args=" + this.args)
38+
.add("opts=" + this.opts)
39+
.toString();
40+
}
41+
2742
/**
2843
* Is the constructor which takes the fields of this class.
2944
*

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

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.math.BigInteger;
2424
import java.util.List;
2525
import java.util.ArrayList;
26+
import java.util.StringJoiner;
2627
import com.github.sttk.exception.ReasonedException;
2728

2829
/**
@@ -101,6 +102,27 @@ public class OptCfg {
101102
*/
102103
public final Postparser<?> postparser;
103104

105+
/**
106+
* Outputs a string of the content of this object.
107+
*
108+
* @return A content string of this object.
109+
*/
110+
@Override
111+
public String toString() {
112+
return new StringJoiner(", ", "OptCfg{", "}")
113+
.add("storeKey=" + this.storeKey)
114+
.add("names=" + this.names)
115+
.add("hasArg=" + this.hasArg)
116+
.add("isArray=" + this.isArray)
117+
.add("type=" + this.type)
118+
.add("defaults=" + this.defaults)
119+
.add("desc=" + this.desc)
120+
.add("argInHelp=" + this.argInHelp)
121+
.add("converter=" + this.converter)
122+
.add("postparser=" + this.postparser)
123+
.toString();
124+
}
125+
104126
/**
105127
* Is the constructor that takes the all field values as parameters.
106128
* <p>

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

+15
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,19 @@ void testGetArgs_returnedListIsUnmodifiable() {
111111
fail();
112112
} catch (UnsupportedOperationException e) {}
113113
}
114+
115+
@Test
116+
void testToString() {
117+
var args = new ArrayList<>(List.of("a0", "a1", "a2"));
118+
119+
var opts = new HashMap<String, List<?>>();
120+
opts.put("o0", new ArrayList<Integer>(List.of(123, 456)));
121+
opts.put("o1", new ArrayList<String>());
122+
123+
var cmd = new Cmd("foo", args, opts);
124+
125+
assertThat(cmd.toString()).isEqualTo(
126+
"Cmd{name=foo, args=[a0, a1, a2], opts={o0=[123, 456], o1=[]}}"
127+
);
128+
}
114129
}

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

+35
Original file line numberDiff line numberDiff line change
@@ -664,4 +664,39 @@ void testConstructor_withNamedParam_full() {
664664
assertThat(optCfg.converter).isEqualTo(c);
665665
assertThat(optCfg.postparser).isEqualTo(p);
666666
}
667+
668+
@Test
669+
void testToString() {
670+
var c = new IntConverter();
671+
Postparser<Integer> p = i -> {};
672+
673+
@SuppressWarnings("unchecked")
674+
var optCfg = new OptCfg(
675+
desc("The option description"),
676+
isArray(true),
677+
names("foo-bar", "f"),
678+
storeKey("FooBar"),
679+
converter(c),
680+
postparser(p),
681+
hasArg(true),
682+
argInHelp("<num>"),
683+
type(int.class),
684+
defaults(123, 45)
685+
);
686+
687+
assertThat(optCfg.toString()).matches(
688+
"OptCfg\\{" +
689+
"storeKey=FooBar, " +
690+
"names=\\[foo-bar, f\\], " +
691+
"hasArg=true, " +
692+
"isArray=true, " +
693+
"type=int, " +
694+
"defaults=\\[123, 45\\], " +
695+
"desc=The option description, " +
696+
"argInHelp=\\<num\\>, " +
697+
"converter=[^,]*, " +
698+
"postparser=[^,]*" +
699+
"}"
700+
);
701+
}
667702
}

0 commit comments

Comments
 (0)