Hey everyone, New to kotlin/java world I found th...
# getting-started
t
Hey everyone, New to kotlin/java world I found this package and want to use it but I don’t exactly understand how am I supposed to pass these options because using —args does kot work https://github.com/Kotlin/kotlinx-cli I created an option called stage and tried to call it in the following way but didn’t work ./gradlew apprun —args=‘—stage=prod’ Any ideas 💡 ?
1
l
You're passing the args to gradlew this way, not your program. Try adding an extra -- in front of the args (with a space between)
v
What does "didn't work" mean? What happens?
plus1 1
j
@Landry Norris no the
--args
gradlew argument is actually meant to pass the args to the application
v
@Landry Norris no, with
--args
the part after it is indeed given as argument to the class run by the
run
task.
a
maybe it’s a weird Slack text rendering thing, but the command you pasted
Copy code
./gradlew :app:run —args='—stage=prod'
doesn’t use a regular
-
dash, it uses
which is an em-dash or maybe a minus?
t
Yes that’s because of slack rendering
1
v
It's also using typographic single-quotes, that's most probably just a copy&paste error for not using code formatting.
t
I’m still getting the error that’s stage is unknown option So am assuming maybe this isn’t the correct way to pass arguments to cli?
v
Can you show the exact error please, including lines around. By manually describing the error you maybe swallow important information.
Maybe also show a build
--scan
if you can
t
sorry, my bad let me do that
Copy code
./gradlew :app:run --args='--stage=prod'

> Task :app:run FAILED
Unknown option --stage=prod
Usage: producer-service options_list
Options:
    --stage, -s -> stage of the application { String }
    --help, -h -> Usage info


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:run'.
> Process 'command '/Library/Java/JavaVirtualMachines/corretto-17.0.7/Contents/Home/bin/java'' finished with non-zero exit value 127

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at <https://help.gradle.org>

BUILD FAILED in 857ms
9 actionable tasks: 4 executed, 5 up-to-date
a
yeah, that’s odd… try a space after
--stage
?
Copy code
./gradlew :app:run --args='--stage prod'
There’s not any examples of how to correctly pass a kotlinx-cli args, which doesn’t help https://github.com/Kotlin/kotlinx-cli/issues/94
1
thank you color 1
j
kotlinx-cli
is not really developed at the moment, so you won't find much help around it. I would really suggest you give a try to Clikt instead, which is actively developed and has a ton of useful features (and nice design overall)
plus1 2
I also believe kotlinx-cli expects
--key value
with a space instead of
=
, as @Adam S suggested
👍 1
v
But you probably need to use
./gradlew :app:run --args=--stage --args=prod
otherwise you get one argument supplied including the space instead of two arguments
j
@Vampire I don't think so. The docs seems to suggest that the whole string is parsed into a list of arguments. See in the Application Plugin doc:
Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments
foo --bar
, you can use
gradle run --args="foo --bar"
(see
JavaExec.setArgsString(java.lang.String)
).
And then in JavaExec.setArgsString:
Parses an argument list from args and passes it to setArgs(List).
The parser supports both single quote (') and double quote (") as quote delimiters. For example, to pass the argument foo bar, use "foo bar".
Note: the parser does not support using backslash to escape quotes. If this is needed, use the other quote delimiter around it. For example, to pass the argument 'singly quoted', use "'singly quoted'".
v
Oh, ok, then I remembered wrongly, I thought it is a list. You are right, you need to use
--args="'foo bar'"
to give it as one argument, so it should indeed be
--args="--stage prod"
t
you guys are absolutely correct, needed the space instead of equals sign
--args="'foo bar'"
I would have never guessed it thanks a lot
👌 1
🎉 2