Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: added toString method to Cmd and OptCfg class #18

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/github/sttk/cliargs/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +25,20 @@ public class Cmd {
private final List<String> args;
private final Map<String, List<?>> 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.
*
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/github/sttk/cliargs/OptCfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
* <p>
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/github/sttk/cliargs/CmdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, List<?>>();
opts.put("o0", new ArrayList<Integer>(List.of(123, 456)));
opts.put("o1", new ArrayList<String>());

var cmd = new Cmd("foo", args, opts);

assertThat(cmd.toString()).isEqualTo(
"Cmd{name=foo, args=[a0, a1, a2], opts={o0=[123, 456], o1=[]}}"
);
}
}
35 changes: 35 additions & 0 deletions src/test/java/com/github/sttk/cliargs/OptCfgTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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("<num>"),
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=\\<num\\>, " +
"converter=[^,]*, " +
"postparser=[^,]*" +
"}"
);
}
}