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

Provide map facade or factory to OptionSet #35

Closed
binkley opened this Issue Oct 26, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@binkley
Collaborator

binkley commented Oct 26, 2013

I'd like to get a Map<String, String> or Map<optionSpec, String> from OptionSet. I manually coded one, am unhappy with the unconverting back to string and the need to use non-obvious methods:

    /** @deprecated Jopt-simple should implemnt */
    @Deprecated
    static Map<String, String> mapOf(final OptionSet options) {
        final Map<String, String> map = new HashMap<>();
        SPEC:
        for (final OptionSpec<?> spec : options.specs()) {
            final Collection<String> flags = spec.options();
            for (final String flag : flags)
                if (1 < flag.length()) {
                    map.put(flag, options.hasArgument(spec) ? String.valueOf(options.valueOf(spec))
                            : TRUE.toString());
                    continue SPEC;
                }
            throw new IllegalArgumentException(format("No usable flag: %s", flags));
        }
        return map;
    }

Tests:

public class MainTest {
    private static Map<String, String> none() {
        return emptyMap();
    }

    private static Map<String, String> only(final String flag, final Object value) {
        return singletonMap(flag, String.valueOf(value));
    }

    @Test
    public void withNothing() {
        final OptionParser parser = new OptionParser();
        final OptionSet options = parser.parse();
        assertThat(mapOf(options), is(equalTo(none())));
    }

    @Test
    public void withoutValue() {
        final OptionParser parser = new OptionParser();
        parser.accepts("debug");
        final OptionSet options = parser.parse("--debug");
        assertThat(mapOf(options), is(equalTo(only("debug", true))));
    }

    @Test
    public void withValue() {
        final OptionParser parser = new OptionParser();
        parser.accepts("debug").withRequiredArg();
        final OptionSet options = parser.parse("--debug=3");
        assertThat(mapOf(options), is(equalTo(only("debug", 3))));
    }

    @Test
    public void withAlternates() {
        final OptionParser parser = new OptionParser();
        parser.acceptsAll(asList("x", "debug"));
        final OptionSet options = parser.parse("-x");
        assertThat(mapOf(options), is(equalTo(only("debug", true))));
    }
}
@pholser

This comment has been minimized.

Show comment
Hide comment
@pholser

pholser Oct 27, 2013

Collaborator

I see #37 also. Would you rather OptionSet offer a Map<String, String> or Map<String, Object> (as the pull req suggests)?

Collaborator

pholser commented Oct 27, 2013

I see #37 also. Would you rather OptionSet offer a Map<String, String> or Map<String, Object> (as the pull req suggests)?

@binkley

This comment has been minimized.

Show comment
Hide comment
@binkley

binkley Oct 28, 2013

Collaborator

I provided Map<String, Option> to be more flexible for those wanting typed values, though I feel wasteful passing them through String.valueOf to put them back to strings.

I want to avoid stepping on other uses to satisfy my immediate need (integrating command line options with properties or property-like configuration).

Thoughts?

Collaborator

binkley commented Oct 28, 2013

I provided Map<String, Option> to be more flexible for those wanting typed values, though I feel wasteful passing them through String.valueOf to put them back to strings.

I want to avoid stepping on other uses to satisfy my immediate need (integrating command line options with properties or property-like configuration).

Thoughts?

@pholser

This comment has been minimized.

Show comment
Hide comment
@pholser

pholser Oct 28, 2013

Collaborator

Maybe both OptionSet.toMap() --> Map<String, String> and OptionSet.toTypedMap() --> Map<String, Object>?

Collaborator

pholser commented Oct 28, 2013

Maybe both OptionSet.toMap() --> Map<String, String> and OptionSet.toTypedMap() --> Map<String, Object>?

@binkley

This comment has been minimized.

Show comment
Hide comment
@binkley

binkley Jan 2, 2014

Collaborator

Pull request for OptionSet.asMap() accepted.

Collaborator

binkley commented Jan 2, 2014

Pull request for OptionSet.asMap() accepted.

@binkley binkley closed this Jan 2, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment