jbangdev / jbang

Unleash the power of Java - JBang Lets Students, Educators and Professional Developers create, edit and run self-contained source-only Java programs with unprecedented ease.
https://jbang.dev
MIT License
1.43k stars 159 forks source link

Allow "apps" to pass options to Jbang #1442

Open quintesse opened 2 years ago

quintesse commented 2 years ago

See this discussion https://github.com/jbangdev/jbang/discussions/1441#discussioncomment-3508381

An idea I had is that we could perhaps do something like this (assume an app called "myapp" exists):

$ myapp some args for myapp
$ myapp --debug -Dfoo=bar -- some args for myapp

Meaning that if an "--" argument is present all arguments before it are passed to Jbang while all arguments after it are passed to the app.

If it necessary to pass "--" to the app you can always do:

$ myapp -- an arg with -- for myapp

The question is if we want to make this a standard feature of the jbang run command or that we add a special option for this behaviour, eg the following two lines would basically be equivalent:

$ myapp --debug -Dfoo=bar -- some args for myapp
$ jbang run --app myapp.java --debug -Dfoo=bar -- some args for myapp

I'm leaning towards the latter option because it preserves complete backward compatibility with any existing scripts.

NB: the option could be flagged as hidden so as not to make the UI/help more complex

maxandersen commented 2 years ago

this would require us to add custom code to the app...not following how this would work when its compiled to native ?

maxandersen commented 2 years ago

i.e.

$ myapp --debug -Dfoo=bar -- some args for myapp

Here jbang is not necessarily involved so we can't adjust.

$ jbang run --app myapp.java --debug -Dfoo=bar -- some args for myapp

not following - how is thatdifferent from run today?

maxandersen commented 2 years ago

jbang is not engaged at the time the parsing kicks in...I don't see a clean way of having a app set these without using the env var trick.

We could make it part of jbang app install though so the launch script gets it baked in.

quintesse commented 2 years ago

.not following how this would work when its compiled to native ?

It would just not work when compiled to native. It's something that would only work when run using jbang. Which is okay because if it's compiled to native none of the jbang or java options are relevant anyway, It's just a nicer way to be able to pass options to jbang. Otherwise installing an app comes with disadvantages/restrictions over running it using jbang run, which seems like a pity.

NB: remember that JBANG_JAVA_OPTIONS jbang run .... only works nicely on Linux and Mac, you can't do this on Windows. You'd always have to use set and then don't forget to use unset afterwards or it will affect all jbang invocations. So having something like this would be nice.

quintesse commented 2 years ago

We could make it part of jbang app install though so the launch script gets it baked in.

True, I thought of that as well, but then you could just as well add the options to the script using //JAVA_OPTIONS, so I don't think that would solve the issue of not being able to easily pass jbang/java options when using an installed app.