remkop / picocli

Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
https://picocli.info
Apache License 2.0
4.92k stars 423 forks source link

Add support for custom interpolation lookup strategies #1777

Open ahrytsiuk opened 2 years ago

ahrytsiuk commented 2 years ago

As a developer, I want to add custom interpolation lookup strategies for option default value.

Currently, interpolation strategies are hardcoded in the picocli.CommandLine.Model.Interpolator class. And have the following options:

It would be nice if I can add/register my own strategy. For example when developing Spring Boot application - I want to be able to use the standard Spring Boot configuration mechanism to populate option values.

Let's imagine the following code sample:

       @CommandLine.Option(
            names = "--server-url",
            required = true,
            description = "Server base path URL",
            defaultValue = "{spring-env:server.base-url}"
        )
        public void setBaseUrl(String baseUrl) {
            this.baseUrl = baseUrl;
        }

I expect the default value for this option to be populated from the Spring Boot Environment, if it wasn't specified in the CLI arguments.

And of course, I have to provide the implementation for such a strategy by myself.

remkop commented 2 years ago

@ahrytsiuk Interesting idea. Will you be able to provide a pull request for this with thorough tests and documentation?

ahrytsiuk commented 2 years ago

@remkop sorry for the late response. Yes, I can provide a PR. However, I want to check with you a direction on how to implement this.

I was thinking about this:

WDYT?

remkop commented 2 years ago

Yes, that looks good. Some other things I noticed (there may be others):

remkop commented 2 years ago

FYI: For testing the difference between adding a lookup to a CommandSpec vs adding it to a CommandLine instance, there are similar tests in SubcommandTests.