Search code examples
javabase64apache-commons-cli

Passing a Base64 string as an option to Apache Commons-CLI parser


I built a Java webstart application which needs to receive a Base64 parameter as a command line argument.

I'm using Apache Common-CLI to parse the arguments from inside the JNLP. There are just two arguments, one of which is a JSON, and inside this JSON there is a Base64 string.

The problem is that the Base64 string is padded at the end with "=" (equal char/equal sign), and the parser is having trouble recognizing the string as part of the argument, instead trying to evaluate the "=" as a key=value separator for the argument and it's trowing the following exception: org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option

Is there a way to escape this char, or make the parser ignore it?

The CLI option is built using the following code:

Option appletBehaviourConfigJSONOption = Option.builder("J")
            .longOpt("appletBehaviourConfigJSON")
            .hasArg()
            .argName("JSON")
            .desc("JSON config")
            .build();
clioptions.addOption(appletBehaviourConfigJSONOption);

CommandLineParser cliparser = new DefaultParser();
CommandLine cmd = null;
try {
    cmd = cliparser.parse(clioptions, args);
} catch (ParseException e) {
    e.printStackTrace();
}

And it's being called from a JNLP:

<argument>-appletBehaviourConfigJSON { "appletBehaviourSignatureType": "HASH", "appletBehaviourHashList": [{"id": "1234", "hash": "ZjQzZDM1NTJiYzBhYmZmMDBlNTc0NjIyZDExMDhhM2Y5MmVlOWJjZAo="}, {"id": "5678", "hash": "ZjQzZDM1NTJiYzBhYmZmMDBlNTc0NjIyZDExMDhhM2Y5MmVlOWJjZAo="}], "appletBehaviourCookies": [{"name": "JSESSIONID", "value": "2edee5627c84937f707bdd390b1c"}, {"name": "STICKY", "value": "ASD123213123adsf"}], "appletBehaviourPostURL": "http://example.org", "stampSelection": "WHITE", "setStampAll": true}</argument>

When I change the '=' to 'a' on both hashes, the parser works as expected (but the option now has an invalid Base64 of course).


Solution

  • It seems commons-cli does not handle blanks between name and value for options the same way as an equal sign.

    Therefore try to use an equal-sign instead of blank as in

    -appletBehaviourConfigJSON={...