diff --git a/src/main/java/com/github/sttk/cliargs/Cmd.java b/src/main/java/com/github/sttk/cliargs/Cmd.java index 2301145..cdf5bbf 100644 --- a/src/main/java/com/github/sttk/cliargs/Cmd.java +++ b/src/main/java/com/github/sttk/cliargs/Cmd.java @@ -12,6 +12,7 @@ import static java.util.Collections.emptyMap; import java.util.List; import java.util.Map; +import java.util.StringJoiner; /** * Is the class which contains a command name, command arguments, and option @@ -24,6 +25,20 @@ public class Cmd { private final List args; private final Map> opts; + /** + * Outputs a string of the content of this object. + * + * @return A content string of this object. + */ + @Override + public String toString() { + return new StringJoiner(", ", "Cmd{", "}") + .add("name=" + this.name) + .add("args=" + this.args) + .add("opts=" + this.opts) + .toString(); + } + /** * Is the constructor which takes the fields of this class. * diff --git a/src/main/java/com/github/sttk/cliargs/OptCfg.java b/src/main/java/com/github/sttk/cliargs/OptCfg.java index 14a947f..fbea2cf 100644 --- a/src/main/java/com/github/sttk/cliargs/OptCfg.java +++ b/src/main/java/com/github/sttk/cliargs/OptCfg.java @@ -23,6 +23,7 @@ import java.math.BigInteger; import java.util.List; import java.util.ArrayList; +import java.util.StringJoiner; import com.github.sttk.exception.ReasonedException; /** @@ -101,6 +102,27 @@ public class OptCfg { */ public final Postparser postparser; + /** + * Outputs a string of the content of this object. + * + * @return A content string of this object. + */ + @Override + public String toString() { + return new StringJoiner(", ", "OptCfg{", "}") + .add("storeKey=" + this.storeKey) + .add("names=" + this.names) + .add("hasArg=" + this.hasArg) + .add("isArray=" + this.isArray) + .add("type=" + this.type) + .add("defaults=" + this.defaults) + .add("desc=" + this.desc) + .add("argInHelp=" + this.argInHelp) + .add("converter=" + this.converter) + .add("postparser=" + this.postparser) + .toString(); + } + /** * Is the constructor that takes the all field values as parameters. *

diff --git a/src/test/java/com/github/sttk/cliargs/CmdTest.java b/src/test/java/com/github/sttk/cliargs/CmdTest.java index abd1dfd..6da6e39 100644 --- a/src/test/java/com/github/sttk/cliargs/CmdTest.java +++ b/src/test/java/com/github/sttk/cliargs/CmdTest.java @@ -111,4 +111,19 @@ void testGetArgs_returnedListIsUnmodifiable() { fail(); } catch (UnsupportedOperationException e) {} } + + @Test + void testToString() { + var args = new ArrayList<>(List.of("a0", "a1", "a2")); + + var opts = new HashMap>(); + opts.put("o0", new ArrayList(List.of(123, 456))); + opts.put("o1", new ArrayList()); + + var cmd = new Cmd("foo", args, opts); + + assertThat(cmd.toString()).isEqualTo( + "Cmd{name=foo, args=[a0, a1, a2], opts={o0=[123, 456], o1=[]}}" + ); + } } diff --git a/src/test/java/com/github/sttk/cliargs/OptCfgTest.java b/src/test/java/com/github/sttk/cliargs/OptCfgTest.java index 2db16e5..d87de7b 100644 --- a/src/test/java/com/github/sttk/cliargs/OptCfgTest.java +++ b/src/test/java/com/github/sttk/cliargs/OptCfgTest.java @@ -664,4 +664,39 @@ void testConstructor_withNamedParam_full() { assertThat(optCfg.converter).isEqualTo(c); assertThat(optCfg.postparser).isEqualTo(p); } + + @Test + void testToString() { + var c = new IntConverter(); + Postparser p = i -> {}; + + @SuppressWarnings("unchecked") + var optCfg = new OptCfg( + desc("The option description"), + isArray(true), + names("foo-bar", "f"), + storeKey("FooBar"), + converter(c), + postparser(p), + hasArg(true), + argInHelp(""), + type(int.class), + defaults(123, 45) + ); + + assertThat(optCfg.toString()).matches( + "OptCfg\\{" + + "storeKey=FooBar, " + + "names=\\[foo-bar, f\\], " + + "hasArg=true, " + + "isArray=true, " + + "type=int, " + + "defaults=\\[123, 45\\], " + + "desc=The option description, " + + "argInHelp=\\, " + + "converter=[^,]*, " + + "postparser=[^,]*" + + "}" + ); + } }