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 an option to intercept executed Commands #2363

Open
bartoszpop opened this issue Jan 28, 2025 · 3 comments
Open

Add an option to intercept executed Commands #2363

bartoszpop opened this issue Jan 28, 2025 · 3 comments

Comments

@bartoszpop
Copy link

bartoszpop commented Jan 28, 2025

Currently, it is not possible to override default option/parameter values transparently for a user.
We use jBang and Camel jBang to run Camel routes. Internally, both these tools use Picocli to provide CLI experience.
It would be very useful to be able to intercept the executed command and override the default values if not explicitly provided as command line arguments. I could use picocli.CommandLine.IDefaultValueProvider but the problem is that I don't have control over 3rd projects which use Picocli, hence I can't customize the picocli.CommandLine instance. The proposed solution is to use Java System Property or Java SPI (java.util.ServiceLoader) to set a listener class.

@remkop
Copy link
Owner

remkop commented Jan 29, 2025

Two ideas come to mind:

  • contact the JBang maintainers and request that they set the picocli built-in PropertiesDefaultProvider as the default provider on the top-level command. That will allow you to configure default values.
  • create your own application class with a main method that creates the picocli.CommandLine instance with the JBang @Command-annotated class, and configure that commandline instance with your desired default provider (which may be the picocli built-in PropertiesDefaultProvider). Then, instead of running JBang and Camel JBang, you run your custom application class.

Thoughts?

bartoszpop added a commit to bartoszpop/picocli that referenced this issue Jan 29, 2025
@bartoszpop
Copy link
Author

bartoszpop commented Jan 29, 2025

Hi @remkop, thank you for your feedback.

contact the JBang maintainers and request that they set the picocli built-in PropertiesDefaultProvider as the default provider on the top-level command. That will allow you to configure default values.

I raised a separate pr (camel#16907) to customize parameters with Java System Properties using the ${sys:} syntax which is already supported by Picocli but it was rejected.

create your own application class with a main method that creates the picocli.CommandLine instance with the JBang @Command-annotated class, and configure that commandline instance with your desired default provider (which may be the picocli built-in PropertiesDefaultProvider). Then, instead of running JBang and Camel JBang, you run your custom application class.

Unfortunately, that would be cumbersome to maintain because Camel JBang provides tens of different commands and we would need to keep it in sync. Also, we use JBang with other 3rd-party scripts.

In my opinion, a more general approach within Picocli would be advantageous, offering greater flexibility and more options for customization. PFA a pr (#2364) with the proposed solution

@bartoszpop
Copy link
Author

@remkop when is the next Picocli release planned?

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

2 participants