1

I have a java command line application written using Picocli, and I am trying to pass to it a string parameter, but every time I try the bash strips the quotes and I end with three parameters.

javaCLI install "Extras lib v0.1.4.cpkg"

This is the output I receive in bash

positional parameter at index 0..* (package_file) should be specified only once
Usage: javaCLI install package_file

I tried using escape single (\') and double quotes (\"), escape spaces (\ ), even both single and double quotes (with and without ) but none of them work. The easy solution is to rename the package to extras_lib_v0.1.4.cpkg but I will have the same problem with other methods.

PySerial Killer
  • 428
  • 1
  • 9
  • 26
  • 2
    What is "javaCli"? A shell script you wrote yourself? – Joni Apr 25 '20 at 21:10
  • `javaCli install Extras*.cpkg` (If you can't beat 'em, join 'em). You can also try `find . -name "*.cpkg" -exec javaCli install {} \;` – Elliott Frisch Apr 25 '20 at 21:14
  • do you want the double quotes to be passed to the java app, or are you just using those as delimiters? – Z4-tier Apr 25 '20 at 21:32
  • 2
    I bet the problem is not in `bash` but in `javaCli`, so I join the question of the first comment: what is `javaCli`? – Poshi Apr 25 '20 at 23:14
  • The error message is correct and accurate if not helpful. "Usage: javaCLI" you have used `javaCli`. Voting to close as typo. – Elliott Frisch Apr 26 '20 at 03:55
  • javaCLI is a custom application written in Java using picocli.CommandLine library. What @Elliot suggested helped with methods using paths to files but not with methods using strings. Is this something can be worked around or should I ask the author to fix the issue? – PySerial Killer Apr 26 '20 at 07:55
  • The explanation "javaCLI is a custom application written in Java ..." is very high-level. More specific details would be useful to help solve this issue. It would be useful to know if `javaCLI` a is bash script, or a native image compiled with GraalVM, or something else. – Remko Popma Apr 26 '20 at 13:10

1 Answers1

2

My guess is that javaCLI is a wrapper script that calls java and that the quotes are stripped by this wrapper script.

The error message "positional parameter at index 0..* (package_file) should be specified only once" tells me that the install subcommand has a single @Parameters-annotated String field named package_file, so it expects only a single positional parameter, but was invoked with multiple parameters.

You can confirm that the java application incorrectly received 4 arguments instead of the desired 2 by setting system property -Dpicocli.trace=DEBUG. This will cause picocli to print some details, including exactly what command line args it received.

I am guessing that the javaCLI wrapper script passes the parameters like this:

java -cp myjar.jar:picocli-4.2.0.jar com.xxx.MainClass $@

If that is the case it may be possible to fix the issue by ensuring the quotes are preserved when passing arguments to the java executable. Thanks to https://stackoverflow.com/a/39463371/1446916 for the idea of using printf to preserve the quotes:

# attempt to ensure each argument keeps its original quoting
ARGS=$( printf "%q " "$@" )

java -cp myjar.jar:picocli-4.2.0.jar com.xxx.MainClass ${ARGS}

If my guesses are incorrect please provide more details in the question.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114