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

How to customized PropertySource on some subcommand #20

Open
tchiotludo opened this issue Jan 1, 2020 · 5 comments
Open

How to customized PropertySource on some subcommand #20

tchiotludo opened this issue Jan 1, 2020 · 5 comments

Comments

@tchiotludo
Copy link

I've an application with subcommands like that :

@Command(name = "topcmd", subcommands = {SubCmd1.class, SubCmd2.class}) 
class TopCommand implements Callable<Object> { 

    public static void main(String[] args) {
        PicocliRunner.execute(TopCommand.class, args); 
    }
    //...
}

I want to be able to add some PropertySource (custom configuration) for only 1 subcommands (for example SubCmd2.class will add a configuration "myapp.myconfig": "foo" only when running te command from SubCmd2).

I don't think it's possible for now ?

The only solution I see is to used this signature when init Picocli :

public static <C extends Callable<T>, T> T call(Class<C> cls, ApplicationContext ctx, String... args) {

instead of simple on PicocliRunner.call(App.class, args); in order to provide some configuration directly in ApplicationContext but this will need to parse the String[] args to determine the current command, that is not really easy and can be buggy since it's not parse by picocli.

@guillermocalvo
Copy link

@tchiotludo I'm not 100% sure I get what you're trying to achieve 🤔

You want to define a config property myapp.myconfig but only if SubCmd2 is running. Is that correct? What's preventing you from defining this property and then making other subcommands simply ignore it? Why is it so important that the property is not defined unless a specific command is running? I'd like to understand what's the actual use case, and why it cannot be done some other way.

@tchiotludo
Copy link
Author

In my special case, I need to inject custom datasources properties for a specific command, this will configure an h2 database for a command automatically.

You could find an implementation here :

@sdelamo
Copy link
Contributor

sdelamo commented Aug 2, 2023

If remkop/picocli#2079 got implemented, I think we could do something to simplify it.

@remkop
Copy link
Contributor

remkop commented Aug 3, 2023

@tchiotludo is this solution feasible for you?

@tchiotludo
Copy link
Author

@remkop it's exactly what I do here.

In a better world, I would like to have a better integration in micronaut, maybe an annotation @Property(name = "foo.bar", value = "stuff") that will auto inject the configuration parameter on that command only like the Junit integration?

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

No branches or pull requests

4 participants