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

Add a combination of adding/mapping arguments #31

Open
ColinHebert opened this issue Oct 26, 2017 · 1 comment
Open

Add a combination of adding/mapping arguments #31

ColinHebert opened this issue Oct 26, 2017 · 1 comment

Comments

@ColinHebert
Copy link
Contributor

Not sure whether it's something that should be part of the core of argparser but I came across a usecase which consists of multiple arguments (flags) being collected to obtain a set of switches (and allowing this set of switches to be validated).

I monkey patched it as this:

fun <E, T : MutableCollection<E>> ArgParser.addMapping(
        map: Map<String, E>,
        help: String,
        initialValue: T): ArgParser.Delegate<T> {
    val names = map.keys.toTypedArray()
    return option<T>(*names,
            errorName = map.keys.joinToString("|"),
            help = help,
            isRepeating = true) {
        val result = value.orElse { initialValue }
        result.add(map.getValue(optionName))
        result
    }.default(initialValue)
}

fun <E> ArgParser.addMapping(map: Map<String, E>, help: String): ArgParser.Delegate<MutableCollection<E>> =
        addMapping(map, help, mutableSetOf())

fun <E, T : MutableCollection<E>> ArgParser.addMapping(vararg pairs: Pair<String, E>, help: String, initialValue: T): ArgParser.Delegate<T> =
        addMapping(mapOf(*pairs), help, initialValue)

fun <E> ArgParser.addMapping(vararg pairs: Pair<String, E>, help: String): ArgParser.Delegate<MutableCollection<E>> =
        addMapping(mapOf(*pairs), help, mutableSetOf())

Here's an example of the usage:

    private val dumps: MutableCollection<DumpType> by commandParser.addMapping(
            "--heap" to DumpType.HEAP_DUMP,
            "--thread" to DumpType.THREAD_DUMP,
            "--database" to DumpType.THREAD_DUMP, help = "Type of dump to process (more than one type is allowed)")
            .addValidator { if (value.isEmpty()) throw InvalidArgumentException("At least one dump type must be selected") }

What do you think?

@xenomachina
Copy link
Owner

That is definitely an interesting example! I'm just not sure how much this adds over simply having a separate flag for each dump type, and then validating that at least one is set later on.

value.isEmpty() is definitely nicer than !(heap || thread || database) (especially when it's possible that more of these may be added later), but is it enough to warrant adding this to the API?

(I was actually somewhat on the fence about the existing mapping method.)

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

No branches or pull requests

2 participants