Skip to content

Commit

Permalink
- add Arguments.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
xzel23 committed Jan 23, 2024
1 parent 18fd6ab commit e877f1c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ the parameter.

## Changes

### (to be released)

- add Arguments.toString()

### 12.0.4

- fix runtime error (cabe)
Expand Down
25 changes: 25 additions & 0 deletions utility/src/main/java/com/dua3/utility/options/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -206,6 +208,29 @@ public Iterator<Arguments.Entry<?>> iterator() {
return parsedOptions.iterator();
}

@Override
public String toString() {
try (Formatter fmt = new Formatter()) {
fmt.format("Arguments{%n");
for (Entry<?> entry : parsedOptions) {
if (entry.option instanceof Flag) {
fmt.format(" %s%n", entry.option.name());
} else {
fmt.format(" %s %s%n", entry.option.name(), joinQuoted(entry.getParams()));
}
}
if (!positionalArgs().isEmpty()) {
fmt.format(" %s%n", joinQuoted(positionalArgs()));
}
fmt.format("}");
return fmt.toString();
}
}

private static String joinQuoted(List<? extends Object> args) {
return args.stream().map(arg -> "\"" + arg + "\"").collect(Collectors.joining(", "));
}

/**
* An entry represents a single option given on the command line together with the parameters given in that option
* invocation, converted to the option's argument type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public void testFlag() {
""";

assertEquals(expected, cmd.help());

Arguments args = cmd.parse("-p", "hello", "Bob");
String expectedToString = """
Arguments{
--print
"hello", "Bob"
}""";
assertEquals(expectedToString, args.toString());
}

@Test
Expand Down Expand Up @@ -96,6 +104,14 @@ public void testChoiceOption() {
""";

assertEquals(expected, cmd.help());

Arguments args = cmd.parse("--product", "MACCHIATO", "--size", "VENTI");
String expectedToString = """
Arguments{
--product "MACCHIATO"
--size "VENTI"
}""";
assertEquals(expectedToString, args.toString());
}

@Test
Expand Down Expand Up @@ -176,6 +192,14 @@ public void testSimpleOption() {
""";
assertEquals(expected, cmd.help());

Arguments args = cmd.parse("-n", "Eve", "--age", "30");
String expectedToString = """
Arguments{
--name "Eve"
--age "30"
}""";
assertEquals(expectedToString, args.toString());
}

@Test
Expand Down

0 comments on commit e877f1c

Please sign in to comment.