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

"Dynamic parameters" support? #166

Closed
sschuberth opened this issue Apr 28, 2020 · 4 comments · Fixed by #170
Closed

"Dynamic parameters" support? #166

sschuberth opened this issue Apr 28, 2020 · 4 comments · Fixed by #170

Comments

@sschuberth
Copy link
Contributor

Is there support for dynamic parameters in clikt, or something that comes close to it? Note that in the -Da=b -Dc=d example there's no space between -D and a, which generally is something that is not support in clikt, AFAIK.

@ajalt
Copy link
Owner

ajalt commented Apr 28, 2020

That's not currently possible with Clikt, since it would require changes to the parser.

Can you share your use case and (if you know of any) existing apps that use dynamic options?

@sschuberth
Copy link
Contributor Author

sschuberth commented Apr 28, 2020

E.g. Gradle uses that type of option to pass system properties or project properties on the command line, and I believe Maven has the same (at least for -D). My goals was to resemble that feature in my own project.

@ajalt
Copy link
Owner

ajalt commented Apr 28, 2020

So looking into Jcommander and gradle's -D (and -P) options, I think Jcommander's choice to call them "dynamic options" is a bit misleading.

In gradle, all of the following are equivalent:

-Dfoo=bar
-D foo=bar
--system-prop=foo=bar
--system-prop foo=bar

So it's not that the option is dynamic. It's parsed the same as any other option, it's just that the option's value has an = in it, which is used to create a map entry.

You could do that easily with transformAll now if you use a different separator than =.

This works for all of the above examples except the first one:

option("-D", "--system-prop").transformAll { all ->
    all.associate { v -> v.split("=").let { it[0] to it[1] } }
}

-Dfoo=bar is currently parsed as a long option, but it would be easy to fix that.

So we could do something like option().keyValue() that implements the above transformation pretty easily. Let me know if you have a suggestion for a better name.

There are some potential additional features that could also be added:

  • Omitting values (-Dfoo=bar -Dbaz)
  • Value conversion
  • Customizable delimiter (e.g. foo:bar instead of foo=bar)

@sschuberth
Copy link
Contributor Author

I think Jcommander's choice to call them "dynamic options" is a bit misleading. [...] So it's not that the option is dynamic.

Absolutely correct, and I agree the name is misleading (and somewhat promises more than what it actually does).

-Dfoo=bar is currently parsed as a long option, but it would be easy to fix that.

That would be great!

So we could do something like option().keyValue()

I wonder if it makes sense to generalize pair() instead by taking a delimiter which would be set to = in this case.

There are some potential additional features that could also be added:

All of these would be nice to have! Omitting values is a common thing e.g. in Maven with -DskipTests (there's no need to set it to true as the presence alone already expresses that). Value conversion is probably not that important, that could be done in regular code later if needed. But being able to customize the delimiter would be beneficial, I believe.

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

Successfully merging a pull request may close this issue.

2 participants